I've seen code like this:
if(statement)
do this;
else
do this;
I don't like that, I think this is cleaner and more readable
if(statement){
do this;
}else{
do this;
}
Is this simply a matter of preference or would one way be recommended over the other?
I agree with most answers in the fact that it is better to be explicit in your code and use braces. Personally I would adopt a set of coding standards and ensure that everyone on the team knows them and conforms. Where I work we use coding standards published by IDesign.net for .NET projects.
My general pattern is that if it fits on one line, I'll do:
If there's an else clause, or if the code I want to execute on
true
is of significant length, braces all the way:Ultimately, it comes down to a subjective issue of style and readability. The general programming world, however, pretty much splits into two parties (for languages that use braces): either use them all the time without exception, or use them all the time with exception. I'm part of the latter group.
Having the braces right from the first moment should help to prevent you from ever having to debug this: