Clean CodeI do not agree with all statements in Clean Code by Robert C. Martin. One of the sections I though is completely obsolete was a statement  about disinformative names:

“A truly awful example of disinformative names would be the use of lower-case L or uppercase O as variable names, especially in combination. The problem, of course, is that they look almost entirely like the constants one and zero, respectively.”

The corresponding example he gives is the following:

Disinformative names in Clean Code

So far I though it is obvious not to write such code, however, I came across similar code these days.

for (int o = 0; o < args.NewItems.Count; o++)
{
string s = args.NewItems[o].ToString();
...
}

What’s the problem here? The variable name o is used for a counter and initialized with 0. While this is already hard to read, o might indicate that we deal with an object here. So when having just a brief look over this code you might get the impression it iterates through a set of objects. This is further supported by the usage of the NewItems property here, as in .NET object references is quite commonly used to resolve e.g. a key/value pair within collections.

When using a counter variable without meaning one should use common names such as i or j that a commonly recognized as counter variables.

for (int i = 0; i < args.NewItems.Count; i++)
{
string s = args.NewItems[i].ToString();
...
}

This is only a slight modification but already improves the readability of the code.

Posted at Tuesday, December 22, 2009 2:54:07 PM (W. Europe Standard Time, UTC+01:00) 
Comments [0] #      | 
All comments require the approval of the site owner before being displayed.
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview
Copyright © 1995-2009 by Andreas Heil. aheil is a registered trademark of Andreas Heil. All rights reserved.
The opinions expressed herein are my own personal opinions and do not represent my employers' views in any way. Content and thoughts expressed on these pages and the weblog are subject to be changed. Out of date posts should not be considererd as my current thoughts and opinions.