This means you consider consequences beyond "but it works on my machine" so you're a better programmer. Ergo, higher salary.
This means you consider consequences beyond "but it works on my machine" so you're a better programmer. Ergo, higher salary.
On the other hand, I find 2-space-indented code unreadable; in fact, 4-space wide indentation is the only size I find to be readable. I know plenty of people for whom that isn't the case. Using tabs lets the reader set the indentation level.
¯\_(ツ)_/¯
Edit: All the anti-tab arguments I'm reading below boil down to alignment. Don't align folks; there's good reasons not to align regardless of your indent style.
They're fine for solo projects i guess, if you only ever use one editor.
Edit: Also, in the long run the best thing you can do to your code is to make it easily readable. You will forget what you did. Not to mention your colleagues that have never seen it. So time spent aligning parameters to make code more readable is well spent, if you ask me.
How is this[1] more readable than this[2]?
[1] https://github.com/HearthSim/python-hslog/blob/5a6ad9a29a7d4...
[2] https://gist.github.com/jleclanche/ef40058731061e26d218e2a4c...
Let the lines get long enough and the latter is actually quite a bit harder to read; try to follow the variable all the way to its value and you might actually jump and be one line off.
I deeply care about readability and code style, that's why I'm replying here :) And in all the codebases I have read and written, alignment has consistently been harmful to readability and maintenance.
someFunction(firstParam, secondParam,
thirdParam, fourthParam);
And this: someFunction(firstParam, secondParam,
thirdParam, fourthParam);
I've always found the second much easier to read, especially when there are multiple levels of nested expressions, and it's a style that has been encouraged on every project I've worked on.I agree that aligning '=' is actively harmful in the example you give, and that it's harmful in most situations where it's used. I've encountered very rare instances where it's helpful, though. Sometimes you have a sequence of values defined by expressions that follow a pattern:
enum someEnum {
FIRST = BASE + MASK & (1 << 0),
SECOND_VALUE = BASE + MASK & (1 << 1),
TH = BASE + MASK & (1 << 2)
}
Aligning the expressions can make it more clear what the pattern is and how it differs from value to value: enum someEnum {
FIRST = BASE + MASK & (1 << 0),
SECOND_VALUE = BASE + MASK & (1 << 1),
TH = BASE + MASK & (1 << 2)
}
Often there are other ways to handle the issue, such as hiding the expression behind a macro or a function call; when that's more readable, that's the way to go. But in cases where writing the expression explicitly ends up being more readable, I find alignment to generally be a win in this case.You haven't looked very far! Most open source projects these days enforce code style through some forms of tooling.
But saying that "Any coding standard that relies on humans to do something right is going to fail" is basically giving up and saying "do whatever". I mean, why even ask people to indent consistently if it's doomed to fail? It's a mixed indent party for everybody!
Sure, but I've never seen a tool that was able to enforce that no-one was doing alignment.
> But saying that "Any coding standard that relies on humans to do something right is going to fail" is basically giving up and saying "do whatever".
Coding standards that you can implement in your enforcement tool are fine. But any rule you can't automate is not worth bothering with, IMO.
> I mean, why even ask people to indent consistently if it's doomed to fail? It's a mixed indent party for everybody!
Use a language that enforces indentation, or use a tool to autoindent. Trying to get people to indent consistently by hand is a waste of time.
Examples of autoformatters:
YAPF for Python
Gofmt for Go
Rustfmt for Rust
Also, most people who do have several years of experience under their belt will tell you: don't align. Mid-line alignment is a waste of a maintainer's time to have to update alignment blocks every time you add/remove variables... and statement-alignment is unreadable[1].
[1] https://github.com/pennersr/django-allauth/blob/a44134688f5b...
On a large code base with multiple committers, I've never seen this rule followed consistently. In fact, I think this shows a poor engineering decision: "Let's make a rule where everything looks OK 95% of the time, except when you change tab widths (which is, presumably, why we made the rule in the first place.)"
The crux of the issue is poor tooling. Aligning correctly is a pain because most editors suck terribly at it. For instance neither emacs nor vim align correctly when using tabs by default (they use as many tabs as they can and then pad using spaces, instead of indenting only with tabs and aligning only with spaces). If they did it would be painless to indent and re-indent.
When some of the most venerable code editors out there can't even seem to get it right I think there's no point in fighting it. Learn to stop worrying and love the space, I know I did. Or alternatively use a language like go which provides a standard tool to indent everything, make it mandatory pre-commit and never look back.
But I never found a single editor that handles spaces correctly. I currently use vscode with vim bindings and whenever I deal with a space-indented codebase I want to tear my hair out: I inconsistently have to hit backspace sometimes once, sometimes multiple times to get rid of an indent level in insert mode. And don't get me started with yaml, a really painful experience to edit it.
If you try this, it fails for people who use a different tab size than you, which is the whole supposed point of using tabs.
But yes, the correct answer is don't align. Utter waste of time.
If it fails, then you have not understood and you are not using your tools correctly. If your alignment depends on tab width, then you are not using tabs for indentation and spaces for alignment, you are using tabs for indentation and alignment and spaces for alignment, which of course breaks when you change your tab width.
I believe older versions of Xcode did that by default with objc (maybe still; didn't check lately) if you press enter in the middle of a function argument list. Really annoying.
The other thing you need to do is stop people from checking in code that doesn't follow project conventions.
https://github.com/ioquatix/rack-freeze/issues/2 https://github.com/ioquatix/latinum/pull/1
>deliberately have my editor set up to change tab width based on editor width (responsive indentation width)
What editor are you using that has this setting?
Firstly, I've explored using responsive tab width in https://github.com/ioquatix/jquery-syntax which is nice when presenting code on a mobile phone.
Secondly, I've got hard coded tab width on my desktop with a huge screen vs my laptop with a smaller screen, and this works for all editors, including my primary editors Atom and vim.
Finally, I've played around with using responsive CSS within Atom, but due to recent changes in the text rendering my current CSS doesn't appear to be working.
Trivially easy to set up your editor correctly once (if you even need to) and now everyone gets their preferred intenting size
If anyone can post a sizable production code base that uses tabs that DOESN'T look like shit with a different tab width, I'm certainly open to being proven wrong.
Here and there's the problem. Too many people with editors that go round replacing N spaces with tabs everywhere to have sane alignment in a code base with >1 developer...
Notice there are a couple examples in that code where multiline method calls are meant to line up, and if tabs are not set to 8 they don't line up.
I do it myself but it requires a bit of maintenance and I wouldn't trust teammates with it. There will always be some guys who don't understand the idea and mess everything up.
In a team environment, now, I wouldn't chose anything but 4 spaces. Speaking from experience.
- with anything but 2, 4 or 8 spaces, some guys with incorrectly set up editors will mess everything up.
- tabs don't work well for alignment, and as long as you allow the tab character, someone will make a mess.
- 2 spaces is not enough to properly distinguish between blocks.
- 8 spaces can look very nice if your code is very clean. It won't be the case of your project.
1 tab will render to whatever the next programmer prefers, it's only courteous.
Many languages have a coding style preference. Go is super opinionated, Python in the middle, with C being as conflicted as it gets. Whatever you use, follow the style the language or team is a proponent of.
Unless the project's settings are to use spaces.
Respect .editorconfig files
So in the long run, the indentation WILL get fucked up and you WILL miss something when fixing a bug in code that you or someone else wrote 6 months ago. And your 10 minute bug fix will take 3 hours. Multiply both values by a constant factor as necessary.
It's simply not worth the trouble.
Huh? Things indented to 1 tab will align with everything else on 1 tab. Things aligned to 2 will align with everything else at 2 tabs.
>you have to use a space or three somewhere
Why? Tabs will indent with other tabs.
>you never know if it's spaces, tabs or a combination of them in existing code
Well they should never be combined for indentation. Unless you mean using tabs for indentation and spaces for alignment, but this wouldn't create any issues. Using tabs for spacing is objectively poor form.
I open space- and tab-indented files in my IDE, they are all converted to tabs. It is always consistent.
Um... I always know because I always set my IDEs to show those characters. You don't?