Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Last active November 8, 2015 00:45
Show Gist options
  • Select an option

  • Save AndrewRayCode/cf534f8f0c5c6dd5f8e6 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewRayCode/cf534f8f0c5c6dd5f8e6 to your computer and use it in GitHub Desktop.
/**
* Reasons to avoid comma separated lists in Javascript
* ----------------------------------------------------
* TL;DR use:
*
* var a = 1;
* var b = 2;
*
* instead of:
*
* var a = 1,
* b = 2;
*/
/**
* 1. Increased coupling when adding variables
*/
var a = 1,
b = 2;
// To add a variable to the end of the above list, I have to modify b = 2; to become b = 2,
var a = 1,
b = 2, // I had to modify this line
c = 3;
/**
* 2. Increased coupling when removing variables
*/
var a = 1,
b = 2;
// Same thing here. If I want to remove b = 2, I have to also modify a = 1
var a = 1; // Had to modify this ending to be a semicolon
/**
* 3. Higher chance of git conflicts
*/
var a = 1,
b = 2;
// If committer A adds a new variable, and committer B adds a different variable, there
// is a high chance the new lines will conflict
// Comitter A change:
var a = 1,
b = 2,
committerA = 3;
// Comitter B change:
var a = 1,
b = 2,
comitterB = 4;
// Git will most likely conflict trying to merge these two lines, and cannot resolve the
// conflict automatically, because one of the lines will need the semicolon replaced with comma
var a = 1,
b = 2,
committerA = 3, // needs to change from original commit
comitterB = 4;
/**
* 4. Indenting is harder to manage and understand
*/
// With comma lists indenting becomes an issue
var a = 1,
b = function() { // Indenting already starts with an extra tab
// Should I be indented this much?
};
// This is not an issue with individual var lines
var a = 1;
var b = function() {
// I have only one level of indentation
};
}
@MattMcFarland
Copy link

There is another scenario you're forgetting... I use this one all the time:

var
  a = 1,
  b = 2,
  c = 3;

@AndrewRayCode
Copy link
Author

@MattMcFarland that has all the same problems as listed above. To remove line c you still have to modify line b and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment