Wednesday, June 29, 2011

Variable names are overly important

As you might or might not have read in my previous post (Project Euler Problem 14) I value a good variable name above a great many other things in life.  That might seem a bit exaggerated, but really it isn't. If you need proof, then break out some of your old code (by old I mean more than 12 months without having looked at it) and try to remember what you were doing and why.  It is not nearly as easy as you might think. The problem is, projects come and go for programs pretty rapidly.  Those past projects, no matter how brilliant they were at the time, are simply that. They are past projects.  Most programmers don't take the time, when they stop work on a project, to document what exactly they were planning for it, working on, struggling with, etc.  Most projects are abandoned for something bigger and better. It is the nature of working in a cutting edge field. There is always that allure to the bigger and better. That is where the value of variable names can really shine through (if properly implemented with other good practices that I will mention at the end of this).

decimal x = 100.00m;
decimal y = 2.34m;
x*=y;

What are those three (perfectly legal) C# statements doing?  Could be anything really and if a method has a chunk like that with some error checking and a return statement you are going to be left in the dark.

decimal capital = 100.00m;
decimal interest = 2.34m;
capital *= interest;

What are those three (perfectly legal) C# statements doing?  Well as you can plainly see they are implementing a gross simplification of interest calculations. The example might not be the best, but the principle stands.  With proper variable names the logic of the code can make more sense.

Aside from just helping improve the logic, well thought out variable names can also cut back on the number of  comments that you have to write. In the second example that I gave above I could have added in comments stating that I was calculating interest, but they are not at all needed. The vast majority of people that can program have had enough math that they would recognize that.  I would even go so far as to say that it is more often true, rather than not, that properly improved variable names will reduce the amount of commenting that is needed.

This practice of using well thought out variable names can actual lead to better comments that are actually read instead of just skipped over. If you are replacing good naming conventions with comments that means that the person reading your code has to take a break from the code and read the comments to see what this particular variable is supposed to do or supposed to represent.  I think it is pretty obvious why this is a bad thing.

Aside from good variable names, the readability of your code can be improved by including comments where needed to clarify what is not evident from the code itself, keeping method size down so it can all be viewed on the screen at once, and experience. I know the last one is kind of mean, but it is true. Sometimes only experience can teach the student what the teacher knows.

Until next time...

Enjoy!

0 comments:

Post a Comment