New Fun Blog – Scott Bilas

Take what you want, and leave the rest (just like your salad bar).

Constants: They Go On The Right

without comments

All you people who insist on putting constants on the left side of expressions are living in the dark past. It’s time to change your ways.

It’s more than just an old habit made pointless by modern compilers, it’s actually a bad idea as well. It reduces readability by going against all of the left-to-right conventions that are hard wired into every other part of our brains.

It just makes you code like an old man.

Let’s Code Like An Old Man

What I’m talking about typically looks like this:

if (SOME_CONSTANT_NAME == someVariable)
{
    // do some stuff
}

That’s a constant over there on the left side of the test expression.

I forget where I first saw this recommended. Perhaps it was Code Complete. It’s pretty old, though even today it still comes highly recommended in books on coding styles. Books usually written by old folks. Perhaps you work somewhere with a coding standard maintained by geezers that requires this.

But in the old days, it was necessary! Constant-on-the-left avoids this mistake:

if (someVariable = SOME_CONSTANT_NAME)
{
    // uh oh...
}

That’s the famous missing ‘=’. Bane of beginner programmers everywhere who never saw more than one equals sign in a row in any math class they ever had. Not to mention sleepy programmers trying to get one last bug fix done before they crash.

To the compiler, leaving off the second equals sign translates to this, more or less:

{
    someVariable = SOME_CONSTANT_NAME;
    if (someVariable)
    {
        // hmm...
    }
}

The basic problem is that assignments are expressions, the value of which is the lvalue (here, the left side of the assignment).

So to avoid this problem, people started putting the constant on the left side. Because it’s a constant, an accidental (or purposeful!) assignment will fail and get caught at compile-time. Awesome.

But It’s No Longer Necessary

I say this is an old man coding style because I typically only see it with more experienced people who were around the time of Code Complete coming out and picked up a good habit. Kids coming out of school rarely do it on their own. They go into their first job, run into an old man and get told to do it this way. Then when they get old they teach it to the new kids. It’s a never-ending cycle!

Well, as of five years ago at least, it’s no longer necessary.

Most of the new languages we use today, like C# or Lua or Java or Ruby or INTERCAL don’t have this problem. Some don’t even offer a ‘==’ operator to screw up with. Some offer ‘===’!

And the older languages, well at least C/C++, have compilers that figure out 99% of the time that you screwed up and warn you about it. For the example above, Visual C++ dictates in its robot-speak:

warning C4706: assignment within conditional expression

(Note that this is assuming you have warning levels turned up. Anyone running their compiler at anything less than the maximum warning level is just asking for bugs.)

So, in 2009, you get the benefits that you had in 1995. The compiler is looking out for you, once again. You just don’t have to put the constant on the left any more.

But, hey, you’re used to this old habit and why not keep it up? It’s just a different style, right? And it’s certainly not a bad thing to keep doing.

Oh yes it is!

Yes, It’s Actually A Bad Habit

Here’s why we should all stop doing this. Well, besides the fact that it’s pointless in 2009. It all comes down to this:

We Read Left To Right

Engineers are left-to-right people. We really have to go out of our way to not do things this way. (Well, except Israelis. Israelis: put your constants on the left.)

In math class, x = 10 or y < 5. Equations flow left to right. People have to be taught not to do it, just so they can selectively pick up this old constant-on-the-left habit.

What’s on the left is more important than what is on the right. It comes first as we’re scanning text. Everything is aligned there. It’s where the screen begins. Resize that window to make it more narrow and what disappears? Not the left side. Put that namespace-qualified long constant name on the left and that’s all you’ll see.

So which is more important? The fact that you’re comparing NULL to something, or that something is being tested against NULL? That ‘something’ is doing program flow control. It is the pivot point. It is the decision maker. It holds the state. It’s easily the more important part in the expression. If you have to work to get to see it, then it’s not as readable.

Therefore, the lesser important component – the constant – should go on the right.

Well now, I’m glad to get that off my chest.

May 10th, 2009 at 8:14 pm

Posted in c++, programming, style

Leave a Reply

Want to paste some code into your comment? Just wrap it in [code] [/code]. Also, please note that off-topic or overly commercial comments will likely be removed at my discretion.

Switch to our mobile site