views:

54

answers:

3

Hi all.

I'm currently having my peer code review with my colleague and have had this question on the subject. Is there any preferable syntax for coding, say, if clause statement for JavaScript compilers(JavaScript Engines)? I personally prefer the pattern A when I review the code myself, but do the compilers have their preference too? In other words, are below 3 patterns take the same number of steps for the compilers to complete their interpretation?

//Pattern A: with spaces, indentation, and curly braces.
if( a === b ) {
    return true;
}

//Pattern B: trimmed spaces and removed indentation
if(a===b){return true;}

//Pattern C: removed curly braces, spaces and indentation
if(a===b)return true;

Thank you

A: 

Presumably the second would be faster (though almost certainly not perceptibly), because there are less characters to run through.

That said, presumably you're gzipping your JavaScript anyway, so it doesn't matter, because this isn't what the JavaScript engine is going to see. If you're not doing that, do that, it'll make much more difference to download speed, which is probably where the bottleneck is anyway.

Dominic Rodger
+5  A: 

The implementation does not mind. The frontend (the tokenizer if my limited compiler knowledge serves me right) will produce the same intermediate representation for all of these, and the performance difference due to the character count will be ridiculous, not worth discussing at all.

The first style, or any style with judicious line breaks and consistent indentation, is preferrable as it is by far the most human-readable. As Abelson & Sussman put it:

Programs must be written for people to read, and only incidentally for machines to execute.

delnan
To add, the only noticeable performance issue is that version A will be more bytes to download than version C.
Marcel Korpel
A: 

Do not waste your time worrying about compiler efficiency for braces and whitespace. It is far, far more important to make the code readable and maintainable.

If efficiency is important, then use one of the tools to minimize the code after you've written it, but always maintain, update and debug with the non-minimized version.

Carter Galle