Sunday, June 23, 2013

Coding Standards - The Wright Way - Introduction

Much is said about coding standards.  First thing to get out of the way: I think they're a Good Thing.  Second thing:  Most of the ones widely publicized are dreck.

What is a coding standard good for?  well, if you've got a number of people working collaboratively on something, either one-shot or ongoing projects, it is useful if everybody codes the same way - it makes everything easier to understand by everybody, which makes everything more efficient.  Even if you're the only one who does the programming, and you do it all for yourself, sticking with a good coding standard is good for just you, because when you come back to that code in six months or six years, it will still make sense to you and it won't be painful to make your eyes look at it.  At least not unnecessarily painful.

What should a good coding standard do?  First and foremost, it should promote clarity and legibility.  Second, and nearly as important, it should promote interchangeability of code.  Third, it should as much as possible improve the comprehensibility of code by adding as much implicit documentation as reasonably achievable.  Fourth, it should not be unduly intrusive or inordinately easy to screw up.  Fifth, and least important, it should be as convenient as possible while remaining compatible with the preceding four goals.

What is the One Best Coding Standard?  The God Standard, I suppose.  The one that god used when he wrote the universe.  I have no idea where you can find it, or if it would be applicable in contemporary programming languages, but experience is an excellent teacher, and I'm gonna bet that god has more than anybody.

As I said in the introduction, I don't believe in the One True Path.  No such critter, no such place.  So I'm not going to foist the One True Way on you here.  I'm going to spell out a few things that I've learned over the years that have proved, over many years, to be useful in the fulfillment of all the above criteria.  It may be that you don't agree with some of it, or all of it.  I don't particularly care.  What I would challenge you with, though, is this:  Before you discard any or part of this out of hand, ask yourself honestly what your reason is.

I don't do anything capriciously.  I've been doing this for over 30 years, and I'm gonna bet that means I've got a lot more experience than you.  Experience isn't the be-all and end-all, but it does have advantages that you can't get anywhere else.  You see a lot of things with a lot of experience.  You use a lot of things, too.  Over time, you find out which things work well and which things don't.  You sometimes stumble upon things that work very well that maybe nobody has ever thought of before.  If you ask yourself that question honestly, you're probably going to answer yourself with one of the excuses I used to use all the time:

A) "I know what I'm doing."
B) "I was taught this way, so I'm not going to change."
C) "I don't see an immediate benefit, so it's not worth the trouble."
D) "Who is this guy anyway?"

None of them are valid reasons.  All of them are excuses. All of them are BS.  I used to use them myself.  Over time, I grew up - learned to stop lying to myself.  Learned to be a little more open-minded and a little less narrowly focused.  Learned to look at things less immediate than coffee-time.

Having said that, I see a lot of 'coding standards'. I'm forced to use some of them.  Most of them are crap.  Most of them default to one of three reasons for existing:

1) Somebody with a university degree researched the topic thoroughly for a year or two and wrote a paper.  Since it came from a university, it must be good - right?
2) Somebody else does it that way, and they do a lot of programming, so it must be good - right?
3) Somebody called themselves an 'expert' and said it was the best way, so it must be good - right?

As you may have guessed, all of the above are not reasons at all.  They're rationalizations - no more than that.  Maybe the coding standard you use has a lot of peer support.  Here is a clue for you.  Lots of teenagers are sure they know everything, and lots of other teenagers agree with them.  Lots of teenagers are still idiots.  Lots of them don't realize that until they're in their 30's.  Some of them never learn.  The hubris of the young is a universal constant.  The fact that lots of people do it that way means only one thing - it's handy.  Convenience has never equated to quality.  Ever.

So, on to the meat and potatoes, as it were...


Copyright ©2013 by David Wright. All rights reserved.

No comments:

Post a Comment