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.
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 ;))
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?
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.
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.
m1cro: haha, that pic made my day :)
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...
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...
FooLman: Maybe. OTOH it wouldn't be that difficult to write a program that automagically formats C code, either.
I never leave {} away, this is why:
if(blah)
do_a_thing;
... work on something else for a while, then remember you were supposed to do something else too if blah was true ...
if(blah)
do_a_thing;
do_this_other_thing_too;
... oh shit i forgot to add the {}, now the code doesn't work the way i wanted it to ...
if(blah)
do_a_thing;
... work on something else for a while, then remember you were supposed to do something else too if blah was true ...
if(blah)
do_a_thing;
do_this_other_thing_too;
... oh shit i forgot to add the {}, now the code doesn't work the way i wanted it to ...
I'd write
if(blah) do_a_thing;
And so such a mistake wouldn't happen to me...
if(blah) do_a_thing;
And so such a mistake wouldn't happen to me...
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);
}
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
Quote:
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).
Quote:
3. Do you leave spaces between array name and index? (i.e. "a [0]" or "a[0]")?
First time I hear someone does this.
Quote:
4. Do you leave spaces between function name and bracket? (i.e. "f (" or "f(")?
And this.
Quote:
5. Do you leave spaces before ";" at the end of a code-line?
And especially this.
Quote:
(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
Quote:
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.
Sorry for messing up the layout :)
\o/
Quote:
OTOH it wouldn't be that difficult to write a program that automagically formats C code, either.
There is already one: GNU Indent.
ALT-F8 formats the code automaticly under VC++6.
Well, this is my style, whatever ppl will think about it:
http://cube.lomal.la/~nystep/vcshot.png
http://cube.lomal.la/~nystep/vcshot.png
My style is prety random.
leet desktop :D
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.
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.
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...
}
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...