James Dean Mathias (Instructor)
Dept. of Computer Science
Utah State University

Office: Old Main 437
Phone:  (435) 797-2336
email: dean "dot" mathias (at) usu (dot) edu
Office Hours: MWF 1:00 p.m. to 2:20 p.m. (and whenever my door is open)

Emotional Code Investment

In my Operating Systems class I showed an example from Steve McConnell's Book "Code Complete" that illustrates the simple concept of giving loop variables meaningful names.  During a previous lecture I was coding an example during class and made a passing comment as I was writing a loop that I better use a variable name that has meaning and not something like, "i".

The example from the book is a nested loop.  I prefer to take Steve's discussion to the logical conclusion, and I wish he would too, give all variables meaningful names, even single level loops.  For example...

for (int i = 0; i<NUMBER_MONTHS; i++)    // Bad!
{
    ... // do something here
}

for (int Month=0; Month<NUMBER_MONTHS; Month++)    // Good!
{
    ... // do something here
}

The tension in the class over this was clear, some of them not so subtlety groaned, expressing their disapproval and mocking me.  The reason is obvious, they knew what was coming and had already decided in advance they didn't want to be confronted with a new, and better, idea.  No one could make an argument why "i,j,k" variable names are better than a name like "Month", and there is none.  In spite of the simple truth, over a simple matter, most of the students didn't want to accept this bit of wisdom and were actually hostile towards the suggestion that what they are doing can be improved.  What is so terribly painful about creating meaningful names?  It takes no additional effort and does make an improvement in the code.

Another example:  I require my students to use named constants in their code, i.e. no "magic numbers".  My graders are instructed to take points off if magic numbers are found in the student's assignments.  In spite of this, I estimate some 50% of the students receive points off for this on every assignment and these are only the students the graders catch, there are many who get away with it, because I have a hard time convincing my graders this is an important value!  What is so profoundly difficult about creating a named constant?  It takes a tiny bit of effort and results in more readable code, and more importantly, makes more maintainable code, there isn't a downside.

When making these very simple suggestions, you would think I'm asking students to renounce their religious faith and begin to embrace all that is evil.  Why is it so difficult to get people to make simple improvements in their coding practices?

The answer is simple, we develop a strong psychological investment into the things we repeatedly do, and have done in the past.  It is in our nature to be comfortable with what we know and desire to repeat those things.  We also have an automatic defense against anything that is new or unfamiliar, which works against us when we need to be considering new ideas.  Think about some great idea that you've had, you get excited about it, eventually you tell a friend and they make some criticisms over your idea.  You end up hurt and upset with your friend, not necessarily because the criticisms were invalid, but because you were emotionally invested in the idea and any criticism was going to hurt.  This is our nature, it is difficult to overcome when it works against us.

It is important that we are critical thinkers, this means several things (at least):

  1. Open to new ideas without automatically rejecting them.  The first response should not be a reflexive disapproval, instead, it should be an active mental engagement, considering the different possibilities for what it means.
  2. Willing to accept that what we've done in the past can be improved, or was plain wrong.  I can not emphasize strong enough how important it is to be self-critical and continually find ways to improve our own practices.  I've had people try to convince me otherwise when I've told them about decisions I've made in my life that were wrong.  Even our friends have a difficult time letting us say something personal was wrong, how difficult then is it for us to accept our own faults.
  3. Have the ability to reject bad ideas and find ways to improve upon the good ideas to make them even better.