You are not logged in.

#1 2009-02-05 16:04:57

pogeymanz
Member
Registered: 2008-03-11
Posts: 1,020

Anyone who write in C/C++

What are your indenting habits? I always use Emacs as my text editor and I have reached the epiphany that I don't like the way it does indenting. Do you have a certain way that looks nice and easy to read? I was recently criticized for having ugly code.

Please post samples.

Offline

#2 2009-02-05 16:18:22

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Anyone who write in C/C++


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#3 2009-02-05 17:05:06

pogeymanz
Member
Registered: 2008-03-11
Posts: 1,020

Re: Anyone who write in C/C++

Is that always considered good code habit or is it Judd's preference? The people with whom I work disobey #2 except for else statements.

Wow... I do 4 and 5 totally wrong.

Offline

#4 2009-02-05 17:54:35

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Anyone who write in C/C++

pogeymanz wrote:

Is that always considered good code habit or is it Judd's preference? The people with whom I work disobey #2 except for else statements.

Wow... I do 4 and 5 totally wrong.

It is not code habit, it is indentation style / habit as you first said. As such, it is totally personal and subjective, and there is not really good or bad ones.
I just gave you one example of an indentation style, the one used by pacman. It is very likely Judd's preference indeed.
Anyway, when you contribute to other projects, you should/must follow the indentation style and convention of the project.
For your own project, you are obviously free, but you might want to choose one not too different than the other styles you are already used to smile


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#5 2009-02-05 18:34:46

bwalk
Member
Registered: 2007-03-21
Posts: 177

Re: Anyone who write in C/C++

-kr

Offline

#6 2009-02-05 19:02:29

quetzyg
Member
From: /home/quetzyg
Registered: 2006-08-03
Posts: 129

Re: Anyone who write in C/C++

According to that coding style, I do it wrong in #1, #2, #5 and #7.


ZzZz...

Offline

#7 2009-02-05 19:04:44

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Anyone who write in C/C++

I personally find #2 to be a pain in the ass.  I often print code out to read on paper and when the {} line up vertically it's much easier to read code.  When using an (good) editor this isn't an issue because of pair matching.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#8 2009-02-05 19:14:40

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: Anyone who write in C/C++

shining wrote:

when you contribute to other projects, you should/must follow the indentation style and convention of the project.

+1

For your own projects, just use a common coding style, e.g. the one that the particular programming language's designers recommend. There's no need to create your own. I like the "K&R" brace style for C, which Dennis Ritchie, the language's creator, advocates (it's named after him and Brian Kernighan, the authors of the C Programming Language). Opening braces for function definitions go on their own line, indented to the same level as the return type of the function. Closing braces should line up with their opening counterparts. Opening braces for all blocks of code inside the function should be on the same line as the keyword that they modify. Closing braces should line up with the first non-whitespace character on the line that holds the opening brace. An example follows. Note that I'm using four spaces rather than the two-spaced tabs from Judd's guideline.

int main(int argc, char **argv)
{
    int i;

    for (i = 0; i < 100; i++) {
        if (i % 2 == 0) {
            printf("The number %d is even.\n", i);
        } else {
            printf("The number %d is odd.\n", i);
            printf("Ignore the fact that the `if' block uses unnecessary braces for a one-line block\n");
        }
    }
    return 0;
}

Bjarne Stroustrup, the creator of C++, recommends the same brace style for C++. He says to define classes like C structs with the opening brace on the same line as the declaration. This allows you to differentiate top-level functions and classes programmatically. Languages with one primary implementation often have an official coding style. Java's is here and Python's is here, for example.

Offline

#9 2009-02-05 19:17:24

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: Anyone who write in C/C++

IMHO #2 should be THE standard. Printed or not, it's easer to count code for matching closes if every "beginning" construct is +1 and every "ending" is -1. #3 is not something I do because of this. Example:

if (a ==b) {
    // stuff
    while (true) {
        // stuff
    }
}

Counting for closure of blocks gives:

+1   if (a ==b) {
       // stuff
+1     while (true) {
           // stuff
-1     }
-1 }

= 0

I will never understand the:

if ()
{
    // stuff
}

style because it's two opening blocks (kinda), and one closing. That's lopsided!

All of this is my $0.02.

(Oh, and I do not follow #7 either. My personal preference is if you can't see "!something()" just as easy as "something() != x" then you probably won't understand some other important things, and that is bad. Again, just my opinion, and not attacking anyone else's opinions.)

Last edited by mrunion (2009-02-05 19:18:55)


Matt

"It is very difficult to educate the educated."

Offline

#10 2009-02-05 19:34:05

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: Anyone who write in C/C++

mrunion, you're missing the part of Judd's #2 where he says to treat keywords like functions by not putting a space between the keyword and the parentheses. I much prefer `while (/* ... */)' to `while(/* ... */)'.

To add onto my last post, there are really several aspects to coding style. Different people have different preferences for indentation, brace style, and naming conventions. When contributing code to a project you didn't start, use the project's existing coding style. When creating your own project, use whatever you like, but be consistent.

Last edited by dsr (2009-02-05 19:34:30)

Offline

#11 2009-02-05 22:09:31

shining_grin
Member
From: a little town near Milan
Registered: 2008-05-02
Posts: 23

Re: Anyone who write in C/C++

+1 for K&R indentation, I think it makes the code more readable.

Offline

#12 2009-02-06 00:25:19

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: Anyone who write in C/C++

@dsr -- I do agree with that. If it's someone wlese project, then you follow their lead.


Matt

"It is very difficult to educate the educated."

Offline

#13 2009-02-06 02:32:52

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Anyone who write in C/C++

mrunion wrote:

IMHO #2 should be THE standard. Printed or not, it's easer to count code for matching closes if every "beginning" construct is +1 and every "ending" is -1. #3 is not something I do because of this.

IMO, it's easier to count blocks when I can rely on having { as the first non-whitespace character on the line, and having some extra vertical space between blocks makes them easier to pick out too. I don't see the point in squishing everything together.

Offline

#14 2009-02-06 04:21:20

Killa B
Member
From: United States
Registered: 2008-10-28
Posts: 42
Website

Re: Anyone who write in C/C++

My personal style is similar to the Allman style.

I always put braces on separate lines. I always put spaces around assignment and comparison operators, except when starting for loops. I always put spaces after commas. I always put a space between statements and the parentheses. I never put a space between functions names and the parentheses, and I never put spaces between array names and indices.

I also make liberal use of linebreaks, trying to group related lines whenever possible. I set my tabs to 3-space width.

There should be some standard piece of example code, so people can just space it to their liking. But, since there isn't AFAIK:

#include <stdio.h>

int main(int argc, char *argv[])
{
    char hex[] = "0123456789abcdef";

    printf("Let's count to f!\n");
    int i;
    for (i=0;i<16;++i)
    {
        printf("%c!\n", hex[i]);
    }
    printf("Yay!\n");

    return 0;    
}

Here's a function from an actual program, because examples are always better when they're real.

Offline

#15 2009-02-06 04:45:25

dsr
Member
Registered: 2008-05-31
Posts: 187

Re: Anyone who write in C/C++

What are the commonly cited advantages for Allman's lack of spacing in for loops?

Offline

#16 2009-02-06 07:33:14

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Anyone who write in C/C++

This is how I generally write C code.

void test() {
    return 2;
}

int main(int argc, char *argv[]) {
    
    int z;
    
    if ((z = test) == 2) puts("hi");
        
}

In development mode, I tend to space things out so it's easier to see everything:

switch(command[0]) {
    
    case 'J':
        mode = "joined";
        break;
    
    case 'P':
        mode = "left";
        break;
    
    case 'Q':
        mode = "disconnected";
        eventchannel = NULL;
        break;
    
}

If I were to diff my own practices against the Pacman HACKING file, this would be how it'd go:

#2: I don't use braces when I'm only doing one thing at a time in an if or while block. I tend to embrace 'printf("\033[7m"); for (i = 0; i < 80; i++) putchar(' '); puts("");', which should probably change soon.

#3: As you saw in my example above, I put opening braces for functions on the same line as the function.

#4: I tend to always use // instead of /* */ as comments.

#5: return 0; ftw...

#6: I've never seen a need to quibble with this, although if I do I'll consider what I should do. Array size calculation is one area this guideline would fall over itself in, I think.

#7. I tend to do (!strncmp(...)).

So there. I'm pretty much... 150% unqualified to work on pacman. big_smile

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#17 2009-02-07 22:17:00

Killa B
Member
From: United States
Registered: 2008-10-28
Posts: 42
Website

Re: Anyone who write in C/C++

dsr wrote:

What are the commonly cited advantages for Allman's lack of spacing in for loops?

I don't think it's an Allman thing, it's just the way I learned. I've been writing my code this way long before I even heard of Allman.

Offline

#18 2009-02-10 02:06:09

Lexion
Member
Registered: 2008-03-23
Posts: 510

Re: Anyone who write in C/C++

int funct(void *arg)
{
        printf("hi"); // I use tabs for indenting
}

switch(x)
{
        case 1: /* do something */
                break;
}

urxvtc / wmii / zsh / configs / onebluecat.net
Arch will not hold your hand

Offline

#19 2009-02-10 06:33:52

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Anyone who write in C/C++

Oh, right. I use tabs too.


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#20 2009-02-10 15:40:55

aquila_deus
Member
From: Taipei
Registered: 2005-07-02
Posts: 348
Website

Re: Anyone who write in C/C++

pogeymanz wrote:

What are your indenting habits? I always use Emacs as my text editor and I have reached the epiphany that I don't like the way it does indenting. Do you have a certain way that looks nice and easy to read? I was recently criticized for having ugly code.

All code are nice and easy to read as long as there is some form of indent (ps: some early php code has NONE indent) The only reason that you're criticized is that some people don't like others' coding styles.... *sigh*

Don't rely on simple indent features in emacs, vim, or most other editors, get a real code beautifier that can do all formatting and in batch process when needed.

Last edited by aquila_deus (2009-02-10 15:41:43)

Offline

#21 2009-02-11 21:20:17

Basu
Member
From: Cornell University
Registered: 2006-12-15
Posts: 296
Website

Re: Anyone who write in C/C++

Just pick a style and stick to it. Same if you're working on a team. Change if it is really causing problems (ie. you're regularly putting code in the wrong place because you can't easily figure out where things start and end).


The Bytebaker -- Computer science is not a science and it's not about computers
Check out my open source software at Github

Offline

#22 2009-02-11 21:41:14

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Anyone who write in C/C++

aquila_deus wrote:

Don't rely on simple indent features in emacs, vim...

Wait. What?  Vim for sure has more than simple indent features,  I'm sure emacs is the same.


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#23 2009-02-11 21:56:02

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Anyone who write in C/C++

rson451 wrote:
aquila_deus wrote:

Don't rely on simple indent features in emacs, vim...

Wait. What?  Vim for sure has more than simple indent features,  I'm sure emacs is the same.

I am quite sure many people would disagree with what aquila_deus said so I didn't even bother commenting smile


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#24 2009-02-12 07:14:55

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Anyone who write in C/C++

I disagree also.

Of course, using a code beautifier every so often to reinforce the coding style you choose would probably be a great idea, but not indenting at all, or relying on your editor to intent for you, is a sure sign of disaster.

Learn to indent manually so you aren't tied to your editor. Turn your editor's indent feature off and do it yourself for a day. Or a week. Then you should be set.

That's another thing, too, albeit slightly offtopic: don't tie yourself down particular text editor or text editor configuration. You're using a portable operating system, so try to be portable yourself.

Learn the basics of all the popular text editors - at the very least how to exit out of them. I personally dislike both *vi* and *emacs; this might change one day. However, at the moment, I find vim so different to what I've used all my life that I find it a visual nemesis, and my brain turns to jelly whenever I look at lisp. roll

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#25 2009-02-12 07:48:34

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: Anyone who write in C/C++

dav7 wrote:

That's another thing, too, albeit slightly offtopic: don't tie yourself down particular text editor or text editor configuration. You're using a portable operating system, so try to be portable yourself.

Uh, that is a very weird statement.
I would rather say : You're using a portable operating system, so try to be work portable code yourself
instead of : You're using a portable operating system, so don't rely on a text editor to indent the code for you
I really don't see the link in the latter smile


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

Board footer

Powered by FluxBB