Created
February 14, 2012 20:01
-
-
Save littlefyr/1829742 to your computer and use it in GitHub Desktop.
Better js declaration with single var
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In http://dropshado.ws/post/17210606192/varry-var-var Dave DeSandro declares that he is going to go to 1 var per variable. His reasons inclue: | |
* Easier to add more variables, no need to change any trailing comma or semi-colon. | |
* Eliminates unnecessary git commit diffs, where a line has changed only because you changed a trailing semi-colon or comma. | |
* Easier to read (IMO). var is a reserved keyword, so it jumps out when syntax highlighted. | |
* Eliminates awkward tab spacing underneath the var. It only lines up if your tabs are 4 spaces wide. | |
* [Per JFSIII] Helps collaboration, other devs might miss the change of semi-colon and accidentally add a global variable. | |
The following format resolves 3 of the 5 issues (but everyone knows that real JS is written using spaces not tabs. | |
The third item is a two edged sword. If you have only one var block in each scope, vars are going to be relatively rare, making it easy to spot when someone drops a var in the middle of the code. In the end, its six of one, half dozen of the other. | |
*/ | |
var firstName = 'Ryan' | |
, lastName = 'Gosling' | |
, age = 31 | |
, isAlive = true | |
; | |
/* | |
After the first variable, no need to worry about semi-colons or commas. If you really want more visibility on the ; add a comment | |
*/ | |
var firstName = 'Ryan' | |
, lastName = 'Gosling' | |
, age = 31 | |
, isAlive = true | |
; //var |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment