quote:Original post by twanvl
But the compiler doesn't look at indentation
Yes, that's true, and that's why I said I would personally prefer the second option in my post. As soon as there's more than a single row in the if statement, brackets makes it easier to read, and less chances of getting logical errors. In the example, the whole inner part is part of the if statement, so that's more than one row.
The same thing can be said about Useless Hackers example
quote:
if (mark >= 50)
if (mark >= 80
//do stuff
else
//do stuff
There's more than a single row below the first if, so I would use brackets, for the outer if, and for the outer else too, for symmetry again. But not for the inner if, as there's only a single row below it. Like this
if (mark >= 50)
{
if (mark >= 80
//do stuff
}
else
{
//do stuff
}
That said, I sometimes cheat and use only indentation, like my first example in my previous post, if everything is fully symmetric to start with, like the original example in this thread is. I can't remember when the last time I made an error because of this is, only that's it's very long time ago, so I would say it's quite safe to do it, if you are experienced and know the rules.
The reason why I sometimes cheat, and why I don't like unneccessary brackets for the inner if statements is that it's easier to read code if it's less lines, and you don't need to scroll up and down.
What I really have problem with is code like this, from twanvl's post
quote:
if (mark >= 80) {
// do stuff a
} else if (mark >= 50) {
// do stuff b
} else {
// do stuff c
}
It's really hard to catch misplaced brackets(for me). Generally I have problems with a coding style that puts the { on the same row as the block starts. Somehow I have hard time to see the blocks clearly that way. Also it's the same thing here with catching missing or misplaced brackets, if you forgot the first bracket, it's very hard to see that without compiling.
[edited by - fredizzimo on March 25, 2004 10:15:13 AM]