Wednesday, February 22, 2017

Just Don't Write Ambigous Code

I've stopped putting semicolons in my javascript, at least for my own projects. It makes code so much easier to read. But wait?  Don't we need semicolons in cases where the code is ambigous?

Sure. But why write ambigous code? Unless you are competing to write the longest one line program, there is no point.

If I need a semicolon to make my javascript work, then my code isn't clear. And when I come back to it in 6 months, I'm going to be Say What? Semicolons are like a yellow flag saying 'Why are you doing it this way'.

Actually, like nost best practices, the real best practice is don't blindly follow the best practice, instead you should think.

There are actually some places I still use a semicolon. For loops for one. And switches:

switch(op) {
 case 0: doZero(); break
 case 1: doOne(); break

And of course, if maintaining existing code where the style guide calls for semicolons, please respect it.

Here is an actually informative posting on the subject: