People get into all kinds of gripey little snits about how to indent code. Whenever you start a project, it's pretty important to nail down indentation, because it's one of those personal preferences that becomes "a big issue" when there's a conflict. Usually, the indentation is a non-issue, but it's something to fight about instead of discussing the real underlying issues, like interpersonal communication problesm.
So, let's catalog some styles, and discuss:
if (a==b) {
do_this();
}
That's the standard Java style. It's pretty compact.
if (a==b)
{
do_this();
}
That's the standard C style, and it puts a little extra whitespace in there. It's my favorite style, because it is the easiest to read.
if (a==b)
{
do_this();
}
I think I saw that in Code Complete, a very good book about programming style, by Charles McConnell. I recommend the book, but not this indentation style. it's nice that the braces are aligned with the code... but it leaves the "if" way out there.
if (a==b) {
do_this();
}
Hmmmm... That's a variation of the previous one. I don't like it. It's not irrational, but, still.
if (a==b)
{
do_this();
}
else
{
do_that();
}
This is my preferred style. I hate the way the else sits there, but only when I'm typing it. When I come back to read the code, it seems nice and airy. It also works well with "else if (...)"
switch ($a) {
case 'a':
do_this();
break;
default:
do_that();
break;
}
This is a different statement. I didn't like putting the break to the left edge at first, but now like it, because it highlights that the program won't continue into the next block.
switch ($a) {
case 'a':
do_this();
break;
default:
do_that();
break;
}
That was my old style. I like how the case statements stand out, but, now, it's hard to just allow the code from one block to continue into a different block. You could do it, but it'd be hard to notice, and could lead to some nasty bugs.
switch ($a)
{
case 'a':
do_this();
break;
default:
do_that();
break;
}
This is like the spaced-out style I like, but I'm still not used to dropping the bracket onto the next line. Maybe it'll make sense, eventually.
a = 1;
cat = 2;
dog = 100;
I saw this in some Visual Basic code. It looked cool, but adding more text to the block looked tedious.
a = 1;
cat = 2;
dog = 100;
That's my style. Lazy.
a = 1;
cat = 2;
hotdog = 100;
A lot of people are into aligning the equals sign. It seems like a lot of work to me, especially if you have very_long_variable_names.
Every language has its own subtle rules, because every language has unusual features that may or may not translate well to the screen. For example, in Perl:
map {
code...;
} @array;
I like to use that, but if I were going to be more uptight:
map
{
code...;
}
@array;
That's not quite right, in my opinion. The code block isn't really just a code block -- it's passed as an argument to map. Perl lets you pass functions as arguments, and the map command will apply the function to the array. The function is defined in-line. Another form of map is written like this:
map(funcname, @array);
Another way to write it is to use the block again:
map { funcname($_) } @array;
Alternate uses influence the most wordy, indented style. You don't want the "big" style to be that different from the "small" style. You want them to look the same, if they are similar.