Posts in the Software Development category

Variable Names in C code...

Someone over at the MSDN blogs recently posted a mini-rant about people using inscrutable variable names in short apps...

He mentioned that some people use things like i, and others use random letters like x. Believe it or not, there is actually some structure and reasoning behind these choices of variable names, and a certain amount of historical convention to it.

Reasons for using i, j, k as loop variable names:



These are mathematical (or engineering) index variables. Typically you'll see them used in matrices. i indicates the row, j the column, and k? Well, you get the idea.

Reasons for using x, y, z as loop variable names:



These are used when you're iterating over space - x indicates going across a row of pixels / space, and y indicates going vertically down that row of pixels / space. z, of course, goes into the page.

The only other version I've seen is l,m,n - used on the Sinclair Spectrum, because hitting the L key the first time gives you the token for the BASIC FOR command, whereas hitting it the second time gives you L - which is handy as you can rationalize it as "loop variable".

Using i, ii, iii is, however, incorrigible and encourages errors - any repeated single-letter name is going to give you trouble, as you can easily misread it.

Using i, ii, iii, iv, v, vi, vii, viii, ix, x, however, will lead to you being cudgeled to death with a sundial by a software engineer.

(more...)

Hungarian Notation Lite®

Since bitching (or otherwise) about Hungarian notation appears to be a common past-time right now, I thought I'd shove my oar in and deliver my 2 cents.

If you're trying to write code that looks clean, is readable, and yet conveys that little bit of information that you might need to make your day go by that little bit smoother, just exactly what are you going to do? I mean, without vowing to discard all vowels from your variable names?

Personally, I have some simple rules.

Interfaces start with I

Classes start with C (ok, so I break this one quite a lot...)

Member variables are always prefixed with m_

Flags start with a b for boolean

Pointers to anything start with a p.

Template class hierarchy mixin classes (aka Policy classes) all end in Impl. (That one comes from ATL's CWindowImpl... I just cribbed it).

Generic Template classes (eg. containers) tend to end in T. But this isn't a hard and fast rule.

Dialog classes end in Dlg.

That's about as far as I go usually. I gives you enough information not to make the most basic of mistakes, without tying you down to a framework or system which gets hairy if your code changes a lot or you do some refactoring.

It's funny. The more I think about this, the more I'm certain that it doesn't really matter what you do, as long as you do it with a modicum of consistency. Well, that and remembering that if you get hit by a bus tomorrow, there will be someone going through your code line by line shaking their head in confusion if you get too obtuse.

Edit:

I forgot a couple:

g_ - indicates a global variable. Sometimes I waver between going as far as saying You Must Type global_ instead of g_, but it depends on the project. (For example, embedded software engineers hate me if I try to enforce the long form of this one).

s_ - indicates a static variable.

str - prefix for a string - both a C++ STL string and a C style pointer-to-zero-terminated string.

c - prefix for something that is const.

(more...)