Rails Development No-Nos
chadfowler.com
chadfowler.com
I hate 2-space indentation. It's weird. It makes the code look less readable and every other more or less popular programming language uses 4. I see no reason why Ruby code needs to look like a narrow, dense spaghetti because of this stupid "rule".
There is nothing in the language that justifies this awkward feature: Ruby isn't excessively nested and an average line length is short.
This language looks beautiful when properly indented with 4 spaces. I've heard from a couple of Pythonista friends that Ruby looked to much Perl-like to them, I suspect some of it can be attributed to this tradition of fucked up indentation.
Before reading open source Ruby code I always reformat it properly: just a hotkey in vim.
Unfortunately, I don't think all editors play nice.
Of course, if I didn't have a proper text editor that took care of the tab stops for me, it'd probably make me crazy to have to hit the space bar twice for indents.
Still, it was never as off-putting as the significant whitespace in Python, which any pythonista can tell you becomes very easy to live with after an easy and brief acclimation period.
What if the comments specify why you're taking a certain approach over another or something that was decided by other people? Comments are good for documenting not what the code says, but the reasoning behind that code. This is especially helpful if you haven't looked at it for awhile.
Every editor on earth supports both. Why the hell do I have to use spaces if the choice has -no effect whatsoever-.
Using tabs in the code shared between multiple people will produce annoying inconsistencies. Spaces will look identical everywhere.
There are two non-trivial issues in this debate though:
1) mixing spaces and tabs is undeniable evil. Everyone on your team needs to conform to one or the other. (And since I like spaces, everyone should use spaces ;-). )
2) In a layout-based syntax like Haskell's, you cannot set your tab width willy-nilly. The syntax defines a tab as equivalent to 8 spaces. Vary this in your editor at your peril.
(setq-default indent-tabs-mode nil)The proper way to code is to use tabs to indent, and spaces after to align. So, no spaces before tabs, only after, and tabs should only indent to the current indentation level of the code (obviously).
At this point, where you must dictate a fixed tab width that everyone must use, I fail to see any advantage to using tabs (other than a miniscule reduction in file size).
* Write tests
* Don't write obfuscated code
* Consistent indentation, using spaces
* Don't mix presentation with logic
etc. etc.