Programming Style

We firmly believe that Anyone Can Program, getting good, solid results without having to be a rocket scientist.

The difference between a professional programmer and somebody who is just learning, tends to be that the professional programs are a little neater, cleaner, and maintanable. Basically, the professional has a better style, after years of experience.

In addition, one would hope that the professional program will be a little more efficient and less buggy. But that doesn't always happen.

The intent of this page is to give you some tips for programming style.

skip list of on-page links

Make The Program Readable

Many elements of programming style concern making the program easy to read and understand.

This benefits you when you come back later and try to fix, expand, or reuse an old program.

It also benefits others when you share your work with them. It's great to share your programs with others, but you're not doing them a favor if you give them something that they can't figure out!

For most of our examples, we will use hypothetical code intended to center content in a display that is 18 inches wide.

X = (18 - W) / 2
Here is a version that is a little more readable:
CenteredPosition = (DISPLAY_WIDTH - ContentWidth) / 2

Use Comments

Every programming language has a way that you can put words into the program that aren't translated into something that is actually run by the computer. This allows you to write notes and comments for future reference.

Sometimes you want a big "block comment" that describes the overall task to be accomplished by the next few lines:

' Wait for the Trick-or-Treater.
'
' To be sure that the kid is really there, make sure that we see
' the KidPresent several times in a row (ideally a full second)
' before actually starting.
SStartAgain:
  KidWait = 0
SLookAgain:
  IF KidPresent = No THEN SStartAgain  ' if no kid, start at the beginning
  KidWait = KidWait + 1
  IF KidWait < 20 THEN SLookAgain      ' keep looking until we see kid many times

You can also put a comment on the end of a line, explaining what it does:

  PULSOUT MonsterRoar, 1               ' 10 µs pulse to sound chip; make monster roar

In either case, whenever you come back and change the program later, you must update the comments. If you change the code without changing the comments, the commentary can devolve into lies that mislead you in the future.

Use Meaningful Variable Names

In the ancient days of computer programming, variable names were limited in length, sometimes to a single letter.

You often ended up with lines like X = (18 - W) / 2, and if you left the program and came back to it a month later, you could spend hours trying to figure what you had originally intended.

Things are a lot better now, with most programming languages allowing longer names for variables. Unfortunately, old habits die hard and some programmers still use short names. Yes, I suppose it saves a little on typing, but it makes the code hard to read.

I suggest that you use longer names and take advantage of that length to make them more meaningful.

Isn't CenteredPosition a bit more indicative of the result that we are getting from the calculation? Isn't ContentWidth helping you to figure out what is going in to the math?

Avoid Magic Constants In The Code

This little bit of advice suggests that you avoid scattering constants throughout your program and instead create symbolic names with the values.

It also helps if you name these symbolic values in such a way that they are clearly distinct from variables. E.g., DISPLAY_WIDTH is a symbolic value that shouldn't change, while ContentWidth is a variable that will probably have a different value for every line.

There are two reasons why this is often a good idea:

Don't Over-Design

The professional programmer has a serious handicap: Geeks love things that are high-tech, flexible, and powerful.

As a result of this, professionals sometimes over-design a program. This takes many forms:

Programmers who follow "agile" development methods usually adhere to the rule, "Do the simplest thing that could possibly work."

Optimize Last, If At All

There are certainly times when you have to squeeze the last bit of power out of a computer that lacks resources. Perhaps you have to optimize for less space (to fit the program in small memory). Or perhaps you have to optimize for speed (so that the program can run the show without delays and gaps.

But these situations are usually rare.

Most of the time, it is better to just get the darn program working, and then to tune it up later - if you need to. Because often the simple approach works just fine and you don't need to further optimize it.

Side note to professionals reading this:

You might be thinking, "If you completely disregard efficiency, you may paint yourself into a corner by writing a program that can't be optimized later." Yes, that is possible.

But not all programs really need to be efficient. For those that do, you should know in advance. If you are monitoring a nuclear explosion in realtime, you will have stringent performance metrics laid out in advance. If you are operating a talking skeleton, the code doesn't have to be that fast.

Lastly, when you suspect that performance will require optimization, you can plan for the optimization and not implement it until it is needed. "Eventually, we'll need to speed up data retrieval by putting a cache here. Let's get it running first, then add the cache when the program does so much work that it starts to slow down."

Don't Overdo This Advice

The advice on this page is intended as general advice that will be tempered in time as you gain experience. It is certainly possible to overdo it.

Here are some examples of how things could get silly of you go overboard:

Related Pages

Skip this list.
Our series on programming controllers:
privacy policy | write to us | tip us
©Copyright 2006 by The Wolfstone Group. All rights reserved.
You must read and abide by our terms of service.