Wich indent style do you use?

category: general [glöplog]
My personal preference in coding style has more to do with readability than anything else. Putting brackets on separate lines makes it really easy to manually match them, as they are on the same level of indentation.
added on the 2006-08-28 12:35:45 by kusma kusma
I usually write my code using Notepad these days. Sometimes I also use edit.com (yes, the MS-DOS Editor) since it displays line numbers, which is important for debugging. The fact I don't always use it reflects my good programming skills which hardly make bug occur ;))
added on the 2006-08-28 12:36:37 by Adok Adok
ah and btw, I always try to leave away { } when possible.. e.g. in if/for/while-clauses when there's just one call/expression in it. But I sometimes think I'm nearly the onle one who does that. So who else does?
added on the 2006-08-28 12:37:03 by jua jua
m1cro: I also do that. But Steve Maguire recommends writing { .. } nevertheless since it makes the code more legible. This may be important if you're working in a team.
added on the 2006-08-28 12:38:35 by Adok Adok
Same as kusma. And yes m1cro, I guess we all do that. Although it would be more consistent to not do it that way. About editors: Textpad for simple HTML/PHP/JAVA hacking (I hate eclipse :)), a proper IDE for everything else.
added on the 2006-08-28 12:40:28 by tomaes tomaes
m1cro: haha, that pic made my day :)
added on the 2006-08-28 12:50:12 by kusma kusma
two spaces, (every eight of them automagically converted to tabs for clarity), first style, no spaces between things except where can't get away with it.
and yes, { -s everywhere. Tend to suck less when you decide to add another line there...

But hey, isnte super-leet style indented strictly to the left, with no fancy whitespace space-wasters? n00bs should experience pains of hell trying to hack it...
added on the 2006-08-28 12:59:30 by FooLman FooLman
FooLman: Maybe. OTOH it wouldn't be that difficult to write a program that automagically formats C code, either.
added on the 2006-08-28 13:01:26 by Adok Adok
I never leave {} away, this is why:


... work on something else for a while, then remember you were supposed to do something else too if blah was true ...


... oh shit i forgot to add the {}, now the code doesn't work the way i wanted it to ...
added on the 2006-08-28 13:03:27 by Sverker Sverker
I'd write

if(blah) do_a_thing;

And so such a mistake wouldn't happen to me...
added on the 2006-08-28 13:05:28 by Adok Adok
I always align everything very nicely in a visually pleasing way and then some lame reformatter tools mess it up.
Code: int bla(int blub, int blib) { for(int i = 0; i < 10; i++) printf("%d\n", i); ret(0); }
added on the 2006-08-28 13:20:53 by gg|rhg gg|rhg
BB Image
added on the 2006-08-28 13:44:06 by LiraNuna LiraNuna
I am using the first one because I can see which } is right down in the same column with the { up there, so I can check easilly in multiple branches and not forget a } or something. And TABs (of 4 spaces size) even though when I open a source file with notepad instead the compiler to make a fast change, it's all fucked up :P
added on the 2006-08-28 14:27:17 by Optimus Optimus
2. Do you leave spaces between variable names and operators? (i.e. do you write "a + b" or "a+b"?)

I usually do this. But depends on the operand and how I want to look at the formula. For example when I have a*b+c*d I prefer a*b + c*d (space only for +) instead of a * b + c * d (all spaces). Because I know multiply comes first before addition. I like to separate formulas like this, some parts that are in priority or also specific parts of the formula that do something diferrent than the rest parts to be without spaces, and the operators which separate those inner parts to be spaced. I also put space in equality sign. c = sin(x/45.0)*63 + cos(y/35.0)*43 (See also that the sine details are not spaced and I have usually only the addition to space and separate the diferrent sines, because the whole sine and cosine thing is a special part on it's own I want to see as two diferrent separate entities).

3. Do you leave spaces between array name and index? (i.e. "a [0]" or "a[0]")?

First time I hear someone does this.

4. Do you leave spaces between function name and bracket? (i.e. "f (" or "f(")?

And this.

5. Do you leave spaces before ";" at the end of a code-line?

And especially this.

(except the tab key itself, that's useful ofcourse)

Hehe. Yeh,
1) In norton and total commander
2) To show off the map in early doom clones
3) Piou Piou

I always try to leave away { } when possible

I do that too. Though, someone tried to add something in my code and did the same mistake as tribao mentions, taking him half an hour to understand what's going on. In my code though, either as if(blah) do a thing; or if (blah) ((next line AND tab)) do a thing; I was never mistaken.
added on the 2006-08-28 15:13:53 by Optimus Optimus
Sorry for messing up the layout :)
BB Image
added on the 2006-08-28 15:50:26 by bartman bartman
BB Image
added on the 2006-08-28 16:25:34 by StingRay StingRay
OTOH it wouldn't be that difficult to write a program that automagically formats C code, either.

There is already one: GNU Indent.
added on the 2006-08-28 19:10:21 by slack slack
ALT-F8 formats the code automaticly under VC++6.
added on the 2006-08-28 19:13:26 by nystep nystep
Well, this is my style, whatever ppl will think about it:
added on the 2006-08-28 19:32:56 by nystep nystep
My style is prety random.
added on the 2006-08-28 19:55:25 by xernobyl xernobyl
leet desktop :D
added on the 2006-08-28 20:17:23 by Zest Zest
A lot of people seem to set their tab settings to 4 or even lower but personally I prefer just the good old fashioned 8.
It forces you to reduce the number of nested levels in a function if you want to prevent line wrapping, which will make it easier for other people to understand what the function is actually doing.
added on the 2006-08-28 21:13:00 by sparcus sparcus
I use 4 spaces and java style {


but no matter what you prefer, if you use tabs correctly, anyone viewing the source will be able to read it perfect with their own favorite setting...
added on the 2006-08-28 21:14:39 by thec thec