Archive for the ‘Coding Style’ Category

Commit Wars

Monday, December 15th, 2008

Quick show of hands…? How many of you have gotten into a commit war?? I think we all know what I'm talking about – some projects even have rules that exist solely to prevent commit wars from occurring (they generally take the form of limiting the frequency of commits for a given file).? These things generally start out innocently.? Some well-intentioned developer opens up a file and decides that a couple of variable names aren't as clear as they should be.? So he renames them.? Since he is already in there, he reformats a couple of lines of code to meet his personal preferences and commits the file.

The original developer is then informed of a defect in her code.? Nothing serious – a simple mistake in the index being used.? She opens the file and is confused by the naming scheme – it seems the new names conflicted with the index name, and resulted in the defect.? In addition, the code was reformatted in an odd manner.? A firm believer in “the one true brace style”, and already on edge for taking the blame for a defect introduced by someone else, she reverts back to the original check-in and reformats the entire file to fit her preference.

The first developer notes the change in the file and the loss of all his insightful and very carefully chosen variables names.? Now furious, he undoes the commit and sends a nasty note to the original developer instructing her to leave his changes alone.

Not one to take something like this lying down, the original developer begins to nit all of his code in reviews.? When he doesn't fix her complaints, she comes in afterwards and changes it anyway.

Preventing Escalation

In hind sight, commit wars are remarkably simple to prevent.? Simply by mandating a standard code format you can avoid the majority of commit wars.? In addition, it is critical to ensure that your reviews focus on real problems and not on simply stylistic or implementation preferences by the reviewer.?

Of course, the most crucial element to preventing a commit war is to create an environment and team where there is enough respect that the developers do not feel the need to look over each other's shoulders.? If no one can trust the rest of the team to do things right, then every little change is subject to enhanced scrutiny, and possible retaliation.

Finally, if every change requires a peer review, most of these problems will never make it past the review.? Too often, teams believe that only the major changes should be reviewed.? But by putting a lightweight review process in place to review every code change, you can avoid the sorts of unnecessary changes that create commit wars.? Of course, if you actually expect to get things one, you had better make sure that your reviews are quick and painless…

Constants First

Tuesday, September 23rd, 2008

Yep, I'm one of those people.? The kind of person who always puts my constants first in conditionals.? This was a learned habit, and not something that always comes naturally, but there are a couple of situations where if find it tremendously useful.? The prototypical situation is the ubiquitous null check:

? if (null != var)

? {

??? // Do Something…

? }

?

I have always liked this check for the simple reason that I only need to read the first two words to figure out what it is doing.? Since I usually am skimming code looking for important bits, I can quickly discard this check unless I am looking specifically for NULL reference issues.? I find it much quicker to read since I only rarely care about which variable is actually being checked for NULL.

Of course, that construct pre-dates modern compilers and comes from “back in the day” when you could make assignments inside an if statement and have it still be a valid conditional:

? bool myFlag = false;

?

? if(myFlag = true)

? {

??? // I want to do something if myFlag is true

? }

? // myFlag now equals true.

This used to be a perfectly valid statement.? In this case, the if statement will always get executed (myFlag would be assigned true, which is then passed to the conditional, which would logically evaluate to true).? Of course, today's modern compilers will flag the original as a warning, and since we all compile with the Warnings as Errors flag set (you do, don't you?), these types of errors will never happen…? But still, many a long night was spent looking for “=” where we meant to write “==”…? So a number of developers learned to write it backwards:

? bool myFlag = false;

?

? if(true = myFlag)

? {

??? // I want to do something if myFlag is true

? }

Since you can't assign anything to a constant, the compiler caught this for us.? Not perfect, because you could still accidentally assign variables, but it was better.? This was why I originally began putting constants first in my conditionals.? I got bit by this error once, and my technical lead at the time introduced me to this pattern, for which I am eternally grateful.

Of course, for some reason, this always seems to annoy someone.? They claim that since the compiler will catch it, and it isn't a “natural” construction, that I should write it the “correct” way (which is always with the constant on the right).? In most cases, I am actually indifferent to coding style.? I am a firm believer that as a software professional you must learn to read a wide variety of coding styles because otherwise you will never be as productive as someone who can read any style (or at least the popular ones).? However, in this particular case, I much prefer my method.? The reason is simple…? It allows you to avoid needless NULL checks:

??? public bool IsMe(string name)

??? {

????? if (name != null)

????? {

??????? return name.Equals(“Brian”, System.StringComparison.CurrentCultureIgnoreCase);

????? }

????? else

????? {

??????? return false;

????? }

??? }

This method simply checks to see if a given name is mine.? Pretty simple right?? Return true if the name passed in equals mine.? Oh and since we don't know who is calling this, make sure that the name isn't NULL, and if it is return false, since my name isn't NULL.

Simple.? But wouldn't it be easier to just write it like this:

??? public bool IsMe(string name)

??? {

????? return “Brian”.Equals(name, System.StringComparison.CurrentCultureIgnoreCase);

??? }

It does the exact same thing, only we don't need to bother with that pesky NULL check anymore.? The constant will never be NULL, so we can just test if the constant equals name, instead of the other way around.? It seems a little unnatural at first, but it avoids all sorts of unintentional errors (for example, how many people would have forgotten to check for NULL in the first example – I did the first time around…)??

Naturally, there are other ways to handle this and avoid having to test for NULL (String.Equals can handle null values), but this requires no framework support.? It will always work, and it has the added bonus that if you ever have to use a compiler that won't flag conditional assignments, you already use a defensive style of coding that won't expose you to unintended assignment errors.