|
James Dean Mathias (Instructor) Office: Old Main 437
|
|
|
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...
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):
|