Wibbly Stuff

The war against curly braces

Coding guidelines, there are numerous of them. Everyone prefers a different coding style. After all, it comes down to personal preference, right? One of such practices is the omission of curly braces. It's everywhere, even SASS has a syntax which doesn't have curly braces!

So, what's wrong?

Nothing. It's all personal preference. But I personally find it hard to read and understand such code. It decreases readability, and I've to put more effort into reading, than understanding the code. There is a cost to everything. Right?

Consider the following example,

if (condition) doStuff();
else doOtherStuff();

Writing like this only makes the code hard to read. There is no clear visual separation between different type of content, i.e. - the condition, and the function. Also if (condition) doStuff(); doesn't immediately tell us that the clause is not complete yet, and there is still the else clause.

Consider the alternative,

if (condition) {
    doStuff();
} else {
    doOtherStuff();
}

Everything is pretty readable and clear here. Yeah, it's a bit waste of space, but I think readability is a bigger concern. And talking of more typing, when all these editors come with autocomplete now-a-days, it's not an excuse.

The thing is, do some favour to the people who collaborate with you and leave the work of making your code ugly to tools like UglifyJS. I couldn't ask for more.

No comments :

Post a Comment