There are a lot of code style guides on the internet, and a lot of them annoy me. The reason is that they are often very prescriptive and don’t bother to make any justification of the style that they tell you to use. This isn’t too much of an issue if you are working for a specific organisation and they want all of their code formatted in a specific way (unless there is a good reason not to format it this way), however this isn’t a reason that anyone else should style their code this way. It also isn’t OK to tell people they must style their code in a specific way because it’s what you’ve always done.
For instance I was looking at a style guide recently which
else statements must be formatted like this:
why? what’s wrong with this:
I can see that there is something wrong with this:
else statement are now separated, making
it non obvious that they are part of the same construct.
Generally I feel that if there is no reason you should choose
one or the other then why are you making a rule about it. I
honestly can’t say that my understanding of other people’s
code is in any way inhibited by the silly extra new line
they want to add after the
Similarly if other people indent by 4 spaces instead of the two that I feel is plenty, it’s not going to inhibit my reading of their code, or make their code any less clear, so why have guidelines about it?
##So what guidelines should there be?
There are loads of obviously stupid things to do such
as more than one line break between
statements. These probably don’t need rules written
down about them because it’s unlikely that anyone will do
So we really need style guidelines for the occasions where people are doing something that makes code difficult to read or causes bugs. This is probably why everybody gets uptight about style guides, and why it’s so important to justify the styles you choose. Like every other programmer I’ve met, I can’t imagine that there is anything wrong with my code style.
I guess given the nature of this post I really should say something controversial at the end. So…
Don’t write lines of code that go on for hundreds of characters, after a point somewhere around a hundred characters they become unreadable. Also some people program on smaller windows or screens than you. Sure they can use text wrapping, however you can do a much better job of line breaking your code than text wrapping does.
Also the asterisk goes here:
it’s a unary operator that works on the variable which is why this works:
if you put the asterisk in the wrong place then this:
probably won’t do what you expect. Only
be a pointer to an integer. The other two will just
I probably should stop now before the dot syntax is mentioned.