Swift Style: Are you a 4-denter?
ericasadun.com
ericasadun.com
Of course, the same thing can happen with spaces, but it generally easier to fix. You can have a "no tab anywhere" policy, and easily enforce it. You can't have a "no space anywhere", or even a "no spaces at the start of lines", for example:
while(1) {
if (a == b && b == c && d == e &&
f == g && h == i) {
....
We need to tab-indent the 'if', but do tab+4 spaces the 'f == g'. That's very hard to automatically check and enforceDefining my terms a bit: By "indentation" I mean whitespace at the beginning of a line without any dependency on character widths. Extra whitespace in the middle of a line is "column alignment", and so is whitespace at the beginning of a line when the number of spaces depends not only on an indentation level, but also on matching other text in the previous line.
In your example, the "if" line uses indentation only, but the line beginning with "f == g" uses a mix of indentation (to match the indentation of the "if" line) and column alignment (the extra spaces to line the "f" up with the "a" above).
One good way to think about it is to imagine (or actually test) what the code would look like in a proportional font. If it still looks properly formatted, you're probably just using indentation. If the formatting gets messed up (e.g. the "a" and "f" no longer line up), you're using column alignment.
Avoiding column alignment has many benefits. If you ban it from your code, so many formatting issues go away completely.
I've written about this on HN a few times, so I'll leave the details to one of my previous posts:
Along that line of thinking, the code should probably be reviewed by at least one other person before being officially committed, so one would hope that would be the place to find and fix such errors. Personally I've always felt the benefit of letting everyone set their own indentation level far outweighs the off chance of someone misindenting code (and I've never seen it cause a problem so far, not that it means much).
As I said I have no issues with this, but if you really wanted, a quick regex for this would be something like: Not allowed: " +\t" Not allowed: "\t +\t" (this is redundant, but you get the idea)
I got used to 2 spaces in C++ in a big shop overrun with Style Guide fundamentalists and really long API names, but it's just not enough to convey logical nesting at a distance.
I've seen code, can't remember where, that used 4 space indentation for loops and 2 spaces for conditionals, so that indentation depth more accurately reflected runtime cost.
With spaces, you not only repeat yourself needlessly, but you also lose the semantic difference between indentation and token separation (which is why converting spaces to tabs always leaves you with a few incorrect sections, whereas tabs to spaces does not).
But, again, it's tech, so the least sensible answer is usually the most popular. I wonder if it just makes some people feel manlier to write cryptic-looking camelCase with a crapload of redundant space characters.
That's why I'm a Visual Basic Rockstar Ninja.
I think most devs prefer spaces to tabs (yes, literally typing in redundant spaces, while also losing some semantic content) Maybe it's because you always get consistent spacing that way, but maybe - just maybe - it's because of a puritanical urge to make programming harder.
Everyone in technology is aware of how important it is to keep things clear and simple, but deep in the soul, a lot of us just want to be Neo, typing bizarre hieroglyphs that only we ourselves are clever enough to decipher.
I know you said that on purpose ;)
In any case, I am also a proponent of tabs for indentation, spaces for alignment, and in fact only recently did I succumb to using spaces for Python projects, for the sake of consistency with the rest of the ecosystem. I was merely pointing out how "many people use it, therefore it's probably bad", just like "many people use it, therefore it must be good" are never good arguments.
There's your answer. 3.
4 in C when I code for fun or the lead says 4.
In fact 4 is a pretty decent visual aid and not to big for my use of 80 car/lines 25lines max per functions/method that I respect religiously and loosely) since 1998. I think being able to see the whole code without scrolling on a VT is kind of preparing yourself for the worst case (debuging in prod with vi/emacs or notepad) where attention is the key point under stress. Prepare yourself for the worst, the worst may happen.
In python, well, I pray that the lead does not mix tabs and space. Because, what a beautiful idea to have made indistinguishable characters significant. That is one of my huge beef with python as well as the inability to control the scope of variables (hence preventing strictures).
So I have strong opinions, but the lead wins when the lead is a benevolent dictator that proved to be right very often (like linus, theo, guido...).
The one who lead the project to its success makes the rules and as long as the project is going a way I like, I follow the guidelines happily.