Saturday, December 31, 2016

Notes on coding style

My thoughts on using TABS for indentation:

This was taken directly from StackExchange and written by @FishToaster.

Now I make no claim on the belief that this is the "One True" indentation method as that has caused flame wars since coding was invented. But this is MY preference (and @FishToaster's reasoning makes sense and agrees with my beliefs, so...)


Now, of course, consistency matters more than either one, and a good IDE makes the differences negligible. That said, the point of this thread is to be a holy war, so:

I prefer tabs:

  • They're a character specifically meant for indentation
  • They allow developers with different preferences in indentation size to change how it's the code looks without changing the code (separation of data and presentation for the proverbial win!)
  • It's impossible to half-indent something with tabs. So when you copy code from some website that used 3 spaces into your 4-space indented file, you don't have to deal with misalignment.

My thoughts on single vs double quotes:

Now here is another case where flame wars exist. 

My preference is using double quotes. 

For one the JSON spec requires double quoted strings for names and values, and since I run a lot of JSON through JSONLint it's easier to be consistent when I'm writing regular code if I always have to remember to use double quotes. 

About the ONLY time I don't use double quotes is if I'm using backticks (`) in JavaScript where I can actually include variables within a string and have the interpreter convert inline.

For Example:

var n = "Mike";
var buf = "Hello " + n + ". How are you today?";
can be converted to:
var n = "Mike";
var buf = `Hello ${n}. How are you today?`;
I find it's a lot easier to build strings with variables with backticks than with a bunch of additions or such.