Why is there so much poorly indented code out there?
The more I browse the code to open source projects in languages that aren't Python, the more I realize that it seems a lot of programmers don't believe in proper indentat开发者_JAVA技巧ion. (I won't mention any projects specifically to avoid having anyone take this question too personally.) Usually code is indented, but in a way just different enough from the standard style that it drives me crazy, especially in old/crufty code. I've noticed that when I write in C-like languages, I tend to indent correctly as religiously as when I'm writing in Python, with the exception of debugging code that I actually want to stick out like a sore thumb. Given how easy it is with a modern IDE to fix incorrect indentation, what are some rationales for not religiously keeping indentation in sync with braces?
Most cases of this problem that appear in the source base I work on most of the time are because of intermixed tabs & spaces. In one person's editor, with tab stops set every 4 characters, it looks fine, but on another person's system with 2 or 8 space tabs it looks awful. You can rearrange those numbers however you want; some files are messed up regardless. Changing the spacing is a giant pain for merging down the road, so it just never gets fixed.
it's easy to have 'poorly indented' html if you're coding in php, i'd say its more important for the php to be neatly indented than the html it spits out...
Two points in addition to Carl's observation:
- "Proper" indentation is a matter of convention and language structure.
- A check in to source control that consists only of changing the indentation of a bunch of lines is likely to be viewed very poorly without prior consultation.
Why don't people dress correctly? Why don't people use the right amount of garlic when they cook? Why does everyone listen to weird music? Your question assumes an authority that defines correctness which I've yet to see.
Besides that, I do like code that is consistently formatted across the file. If you use 1 tab or 3 spaces I don't really care as long as you use it consistently. Same with braces on the same line or a line on their own, don't care, just be consistent.
I personally use VS which out of the box auto-formats everything for me and I'm completely happy with the default rule set.
It's widely accepted that coding style is a matter of 'personal choice' and that one style is as good as another. I don't think this is necessarily true. An article recently written for the accu (only available online to members) suggests that various speed reading techniques can be applied to the layout of code to make it easier to read and understand. There are undoubtedly masses of data on the way humans take in information, but this article was the first I've seen applying it to code layout.
There may well be considerable information demonstrating that certain layouts of code are more suitable for humans to read and understand, but none of the (many) arguments I have seen refer to information like this, and generally involves defending the styles people have become used to.
Maybe simply those programmers' idea of "proper indentation" is different than yours? Maybe YOUR code looks like an ugly mess of f***ed up indentation to them? Seriously, each programmer seems to have his own idea of "proper" indentation, and a curious inclination to bash other people's indent styles as "wrong". Indentation has been a prominent topic of flame-wars from the beginning of Internet (and earlier). No need to add more fuel to the flame.
That all being said, which style you use is relatively unimportant. It is much more important to use consistently the same indent style throughout the project. The most common mess starts when each programmer on the team tries to use and force his style on the rest of the team. The result is an ugly mess of mixed and unfitting indent styles that drives people crazy. That is why it is important to set up coding standards for every project that has more than a few programmers on it. I repeat - what coding standards are chosen is unimportant, consistency counts.
精彩评论