I like tabs for indentation and no special alignment. There's no need to align stuff if you keep your methods trim and refactor things that feel like they need to be next-line aligned.
Agreed on the alignment thing. Why would I spend time hitting the space bar over and over again just because I renamed a variable and now the equals signs all "need" to be five more spaces out? Keep your code orderly, to a fault even, but aesthetics has no place in code formatting.
I took a data scientist certification course where they recommended 8 spaces to discourage excessive nesting....dumbest thing I ever heard. Lost all credibility in my opinion.
Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.
In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep. Heed that warning.
You can use tabs, but it please take care of using them in the right place: only for "tabulate", not to "align" code .. that will only work on your screen.
Agree that spaces should be used for alignment, but disagree with your interpretation. The .css call shouldn't be aligned, it should be indented. And the semi colon should be at the same level of indentation as the first line.
yeah, completely agree with your interpretation! this makes future diffs/patches not mess with surrounding lines
that said, in my example I was trying to come up with a quick example of where to put spaces before tabs .. can you think of a better example than mine?
Spaces should never, ever be before the first non-whitespace character on a line. Tabs should never, ever be after the first non-whitespace character on a line.
Put another way, all lines should match the following regex:
^[\t]*\S[^\t]*$|^$
(i.e. a blank line, or 0 or more tabs followed by 1 a non-white-space character followed by 0 or more non-tab characters)
Like many people, I used 4 spaces for years, but 2 spaces has definitely been winning in JS culture. It's been the standard at the last few gigs I've consulted for and is also standard on many OSS projects.
After using it for a while I like it. It's especially nice for nested ternaries:
I fall in the camp that nested ternaries are bad and should be refactored to make more sense to the casual reader so setting breakpoints on indented ternaries will never be in my code.
I find use cases for ternaries exceedingly rare. I only use them in trivial cases, like:
var foo = (useA) ? a : b;
Even then, I'm a lot more likely to do:
var foo = b;
if (use A) {
foo = a;
}
Pretty much only really use them when the case is trivial and there's an aesthetic benefit to keeping the whole assignment on one line (e.g. it's just one in a long list of things being assigned).
3
u/brotherwayne Mar 27 '15
Mostly?