Fog Creek Software
Discussion Board




WHO NAMES A NAMESPACE "SMITTY"?!?!?

I hate:
1) "My" anything
2) Single letter variables (except loop counters)
3) "Cute" names (I have in fact seen variable names in Klingon)
4) Modules named after the developer.

Sorry, just venting. Move along now, nothing to see here.

Philo

Philo
Tuesday, June 10, 2003

I have seen developers map an single character array over a Fortran common block, so they could type less.

U(50) was the tip speed in x-direction, U(30) was the blade length, U(172) was the air temperature, etc. Imagine tens of thousands of lines of code with only U() elements... And they wondered why 99.9% of their time was spent debugging.

Jan Derk
Tuesday, June 10, 2003

.. who names a namespace "SMITTY"?

Obviously someone who can't spell: http://dictionary.reference.com/search?q=smitty

M and H aren't even _near_ each other on the keyboard..

(sorry, couldn't resist)

Nice
Tuesday, June 10, 2003

Oh.

I use "mySomething" for references to things that objects own. Usually that live outside my namespace. It's so I know it belongs to this object. As opposed to a passing reference which is just "aSomething"

And "the" as parameters, so members don't clash with parameters. I do this because typing the "theSomething" parameters seems to happen less than typing "m_something" members would.

It sort of means the code reads in nice sentences which helps me think about it -  "delete myParser;"

Katie Lucas
Tuesday, June 10, 2003

Jan,

We had an anlayst who used Case tools, but did not know how to change the names from the default.

Just as VB gives controls names like TextBox1, this Case tool would give processes names like CS0171.

The anlayst then insited that all functions be given the name that matched her diagram.  So all of the procedures had meaningless codes for names.

Complete nightmare.

Ged Byrne
Tuesday, June 10, 2003

Thinking of variable names is one of the hardest parts of coding I reckon.

That's why I like Perl, default variables mean less variable names to think of.

Matthew Lock
Tuesday, June 10, 2003

Klingon!?? ROFL! Look on the bright side, at least Klingon is written in Latin-1, would be hell on your source control otherwise ....

Richard
Tuesday, June 10, 2003

I saw a program that used vegetable names for variables and fruits names for the functions
if banana(potato,carot,leak)
    apple(potato,carot)
else
    raisin(potato,leak)

Application Specialist
Tuesday, June 10, 2003

<M and H aren't even _near_ each other on the keyboard..>

How keyboardist!! (They are very close on wy keyboard.)

Tom
Tuesday, June 10, 2003

I worked for a company for awhile where a programmer frequently used variable names like Blue, Green, Yellow and Fuchia for no useful reason.

The president of the company said he was "artistic".

I said he was a "moron".

Tim Sullivan
Tuesday, June 10, 2003

"Klingon!?? ROFL! Look on the bright side, at least Klingon is written in Latin-1, would be hell on your source control otherwise ...."

All .NET compilers accept UTF-8 source code files.  While I'm not aware of a Klingon section in the Unicode standard you might quite well define your variables in polytonic Greek!

(If the compiler accepts polytonic Greek variables, anyway.)

Chris Nahr
Tuesday, June 10, 2003

There goes a story from one university where one student was told to use longer and more mnemonic names to his variables. So, he started to name them "long_and_mnemonic_variable_number_one", "long_and_mnemonic_variable_number_two", etc ...

Evgeny Goldin
Tuesday, June 10, 2003


Now I think of it, I'm always changing my style of naming variables. How unprofessional.

Leonardo Herrera
Tuesday, June 10, 2003

I remember giving somebody a task to investigate the design of plugin modules to our control panel app (kind of similar to MMC snap-ins, actually) and he ended up writing it as a bee keeping system with variables like beeX and beeY.

He didn't last long.

Better Than Being Unemployed...
Tuesday, June 10, 2003

[Modules named after the developer]

I don't mind this so much if it's open source or their own personal toolbox. But I have to agree with the loathing for "my" variables. You own nothing on the machine. it owns you! Think not? Why are you looking at it right now? You can't look away can you!

trollbooth
Tuesday, June 10, 2003

I worked with a developer who used to use sexual innuendos in his code,  using class names like "Shaft" and :balls" and funtion names like "probe()".  Needless to say, after he left we junked all of his old code simply because noone liked to look at it  (and we were afraid a customer might run into a Shaft.probe() exception too..)

Colin Evans
Tuesday, June 10, 2003

I'm largely with the original post.

Here are a few of my tips

1. For scratch variables (like loop indexes, coordinates when drawing, width/height of some window when painting it or calculating it) use double letters. Instead of i, j, k, x,y,z, w, h use ii, jj, kk, xx, yy, zz, ww, hh

The advantage is if later the scratch variable becoes meaningful, it is easy to change.


2. Don't leave letters out when naming variables. I hate it when people leave out letters like

SalaryEarnedThisWeek  becomes SalryErndThsWk

As I can never remember (and can't tell by scanning the code unless I go back to the original definition and copy/paste) whether it's
SalryErndThsWk, SlryErndThsWk, SlaryErndThsWk, etc.

Either spell it out in full: SalaryEarnedThisWeek
or spell it out in full with a shorter name if you don't like typing: SalaryThisWeek


3. Never use plurals for variable names.

Some people like to name arrays (or other variables) with an 's.  When you have a few variables like this, and some not, and you come back to the code after a while, it is real hard to remember which names are pluralized, which aren't.  So... don't pluralize any.

S. Tanna
Tuesday, June 10, 2003

"3. Never use plurals for variable names."

What would you name a collection of Employee objects, for example?

John Topley (www.johntopley.com)
Tuesday, June 10, 2003

S Tanna: I'm not sure I understand your first point about scratch variables.  Why is a double letter name any easier to change than a single letter name?

Mike McNertney
Tuesday, June 10, 2003

I spent about 20% of my coding time changing varable, class and method names.. It improves a lot the quality of my code.

I am very fond of the VisualStudio's keystrokes:

ctrl-h, [alt-c, alt-w] alt-a

(Replace All, Match case, Match whole word) to replace names without side effects all the time. It's very safe and very convenient. It has become a essential part of my coding.

Although I think it's kind of simple, there is even a refactoring about this:
http://www.refactoring.com/catalog/renameMethod.html

Sergio
Tuesday, June 10, 2003

"Why is a double letter name any easier to change than a single letter name?"

If you do a search/replace for "i" or "j", you'll get a bunch of false positive results. OTOH, if you're searching for "ii", you'll probably only find places where it's used as a variable.

I don't user either method, however. I usually use "count" or "index" as my looping variable (depending on why I'm looping).

RocketJeff
Tuesday, June 10, 2003

Why are programmers allowed to be lazy when naming loop counters?  "i" is a completely useless name.  Is it that hard to qualify the "i" with what it actually is indexing, e.g. "iShape"? 

SomeBody
Tuesday, June 10, 2003

> S Tanna: I'm not sure I understand your first point about scratch variables.  Why is a double letter name any easier to change than a single letter name?

Global replace

Try global replacing (say) i in any source file with a nice name. It is next to impossible not to hit some other i's (say in other variable names, function names, etc)

S. Tanna
Tuesday, June 10, 2003

> What would you name a collection of Employee objects, for example?

Employee[]
Employee[1]
Employee[2]
etc

I don't know how you read it aloud, but it also sounds better the way I do ("Employee Array", "Employee 1", "Employee 2") than if I had used Employees[] ("Employees Array" - not too bad, "Employees 1", "Employees 2" - yuck)

S. Tanna
Tuesday, June 10, 2003

Philo!

Thanks man,

myAnything is just so damn disgusting.

Nat Ersoz
Tuesday, June 10, 2003

<Why are programmers allowed to be lazy when naming loop counters?>

Often the loop counter is just noise, required by whatever language you're using. If I'm iterating through N shapes, and treating each shape independently, what I really want is 'for shape in shapes', not something excessive and wordy like 'for(iShape=0;iShape<shapes.size();++iShape)'.

So to cut down on the noise, I make the loop counter short. That's my rationale, anyway. It's not laziness.

i and j are also traditional subscripts used in maths.

Tom
Tuesday, June 10, 2003

I would contend than when something really is scratch variable, like some throw away loop index, a shorter name is MORE readable.

1. if you use a long name, when the variable is just throw-away garbage/noise/syntatical requirement of the language,  is adding too much emphasis/importance to such an element.

2. if you have several nested loops, in each case the index being throw-away garbage/noise/syntatical requirement, long names for each makes it more not less confusing.

3. if you have several nested loops, in each case the index being throw-away garbage/noise/syntatical requirement,  you are probably more concerned about which loop index you are dealing with (i.e. code dealing with a loop index wants to know it's dealing with the inner/outer most index, etc) than what you're counting throw.  With ii, jj, kk (or i,j,k) this is obvious. With long names, it's not.

4. I can not recall any case, where any programmer I have worked with (or me personally) has been confused because the loop index was called ii, jj, kk (or i,j,k).  It there is no problem with a short name for this case, why fix it.

S. Tanna
Tuesday, June 10, 2003

"Try global replacing (say) i in any source file with a nice name. It is next to impossible not to hit some other i's (say in other variable names, function names, etc)"

???

So your editor/IDE can't do "whole word only" search & replace?  That has been standard since... uh, since about ever, or at least the last 15-20 years which amounts to the same thing. :-)

Chris Nahr
Tuesday, June 10, 2003

Katie,

I've written books with Smalltalk  :)

apw
Tuesday, June 10, 2003

The problem with using a short, common name like 'i' is that the C/C++ scoping rules makes it almost inevitable that it'll get reused in an unintended manner. 

I've seen it happen where a programmer used 'i' in an outer loop, followed by another programmer coming along and adding an inner loop using 'i'.  This worked fine until another programmer came along and decided that the second declaration of 'i' must be wrong and removed it. 

Granted, this is a good example of why you shouldn't hire bad programmers, but unfortunately bad programmers do get hired and even the best programmers occasionally screw up.  Removing a variable declaration should cause the program to not compile.  It shouldn't cause the program to behave in a completely different manner. 

This particular issue occurred due to the differences between Visual C++ 6.0 'for' loop scoping and Standard 'for' loop scoping.  If you don't do a lot of porting between compiler's that have different 'for' loop scoping rules, it might be less of an issue. 

I don't see the use of i, j, etc. in math as a particularly good argument for using it in programming.  If anything, it's a good argument for why you shouldn't use 'i' given the common confusion caused by its use in math as an index, a unit vector, and the square root of -1.

SomeBody
Tuesday, June 10, 2003

>> What would you name a collection of Employee objects, for example?
>
>Employee[]
>Employee[1]
>Employee[2]
>etc

I was actually referring to collection objects, such as Java's ArrayList.

John Topley (www.johntopley.com)
Tuesday, June 10, 2003

Using i,j,k is a coding convention. Not using them breaks that convention. While one of my mantras is "a foolish consistency...", I don't see this one as foolish.
Variable names are labels to help the next guy who reads the code. Since it's a convention, then anyone who has any business being in the code, should immediately recognize

for(int i=0;i<value;i++)

as a simple loop with a counter. More importantly, if they see

dataGrid.Columns[i]=value;

they should know they're in a loop. However

dataGrid.Columns[iColumn]=value;

does *not* automatically communicate the loopness of the variable. So by *not* using i,j,k you're losing community readability of code.

There's nothing wrong with breaking conventions, so long as you have a strong reason to do so. IMHO "adding readability at the cost of readability" is debatable.

My $.02

Philo

Philo
Tuesday, June 10, 2003


"iColumn" makes much more sense than the ambiguous "i", especially in complex code. No competent programmer would have a problem attributing "loopness" to "iCount" in a well written program.


good:
for(iRow = 0; iRow < MaxRows; iRow++)
    for(iColumn = 0; iColumn < MaxColumns; iColumn++)
        dataGrid.Columns[iColumn]=value;

yucky:
for(j = 0; j < MaxRows; j++)
    for(i = 0; i < MaxColumns; i++)
      dataGrid.Columns[i]=value;

for(j = 0; j < MaxColumns; j++)
      dataGrid.Columns[j]=value;

The first example is much easier to follow and will be much more reliable to maintain.

njkayaker
Tuesday, June 10, 2003

I fail to see how 'i' is more clearly a loop than 'iColumn'.  I've seen 'i' used frequently outside of a loop (for example, in function parameter lists taking an index or as a variable taking the result of a search function).  I hope you don't trust that every 'i' is in a loop. 

When I see 'i', I think 'index'.  When I see 'iColumn', I think 'index of column'.  I fail to see how 'index' is more useful information than 'index of column'. 

Pick which line has an error in it:
(a) dataGrid.Columns[i]=value;
(b) dataGrid.Columns[iRow]=value;

SomeBody
Tuesday, June 10, 2003

While the argument about naming counters has the whiff of irreconcilable dogma about it, I would like to offer two points. 

The case for using single-letter variable names is stronger when the loop is only a few lines and can all be taken in with one glance.  The case for using descriptive names is stronger when the loop comprises more lines of code, especially if the start and end of the loop cannot be seen simultaneously in the editor. 

Names like Counter, iRow, iColumn are no better than i,j,k because they are artificial (except possibly for a spreadsheet) in that they bear no relation to the data.  If you have an array of the monthly expenses for each employee, then looping over Month and Employee (or something similar) provides insight to someone looking at code in the loop.  However, using iRow and iColumn tells the reader nothing more than i and j would.  In other words, it is obvious what "expenses[Employee][Month]" refers to, but "expenses[iRow][iColumn]" is no more informative than "expenses[i][j]". 

jcr
Tuesday, June 10, 2003

You're correct that it matters more for large loops than small loops, but the problem is that small loops often become large loops.  Using 'i' introduces a maintence burden when you expand the loop since you're either stuck with 'i' or you have to replace 'i' with a more appropriate name (thus increasing the possibility of introducing a bug in code that previously worked).

I agree that in most circumstances, 'iColumn' would be a useless name.  However, in this example, it was being used to iterate columns in a datagrid and thus makes perfect sense.

SomeBody
Tuesday, June 10, 2003

Er, so does noone else use each loop counter in one place: the loop, and where you alias the current object?

Like so:

for(int i=0;i<employees.size();++i) {
  Employee *emp=employees[i];
  //do stuff with emp
}

When I mentioned 'for shape in shapes' above, this is what I was getting at. You want to hide the looping construct so you can concentrate on what you are doing, and so the reader can see what you're doing in the body of the loop -- having to refer to the loop counter all the time, be it 'i' or 'integerCurrentEmployeeLoopIterationIndexCounter' is a distraction.

Does anyone else use this? If you don't, why not -- is it a bad idea do you think? I use this kind of thing all the time, personally. (Which is probably why out of 219 for loops in my current project only about 15 have a loop counter that doesn't match [ijxy] :)

Tom
Tuesday, June 10, 2003

"irreconcilable dogma" (jcr): not at all.

"i" is highly ambiguous and is easily confused with j, k, etc.

In short, simple code, it's OK but there is really no reason that a better name can't be used. Single letter loop iterators are used out of habit, precedence (i.e. "it's been done before), or lazyness.

Using iRow to iterate months has no justification, clearly. No one is suggesting doing that.

In the example shown, "iColumn" is a very reasonable name to iterate Columns[].

njkayaker
Tuesday, June 10, 2003

someone said:
"Variable names are labels to help the next guy who reads the code"

which is exactly what I think.

then someone else said:
"I've seen 'i' used frequently outside of a loop"

my god Id shoot the swine who did that.
'i' is a terrible variable to use anywhere _except_ inside a loop, it carries no meaning and its job as a label to help the next guy is unfulfilled.
The advantages of using it inside a loop are:
short, lets programmer focus on the code that actually does so.
and that works nicely except for more complicated code...I tend to agree that sometimes variables such as iColumn, or iEmployee etc can give make the code more readable, where thats true they should of course be used.

bottom line:
i should _never_ be used outside a loop and should only be used inside a loop if it improves readability.
(its my belief that it often does)

FullNameRequired
Tuesday, June 10, 2003

I'd be happy to shoot the swine who did that (and other things like it) but unfortunately that's illegal :)

SomeBody
Tuesday, June 10, 2003

For what its worth:

I believe the origins of using i, j, k for loop variables came from Fortran. They were automatically defined as integers.

OTOH, I may be wrong...

ShowingMyAge
Tuesday, June 10, 2003

Just when I thought I'd seen it all some guys come along to protest the use of i as a loop variable. Oh well...

What's next, maybe a campaign against the hard-to-read "+" sign instead of writing a clear self-documenting Add function?  Add(2,3) is so much better than 2+3!

After all, "+" is only used out of habit, has no justification, and the fact that it's the standard mathematical operator doesn't mean anything since, you know, SOMETIMES it can mean something else in mathematics!

Who's with me on this righteous crusade to rid the world of the evil of the "+" operator?

Chris Nahr
Wednesday, June 11, 2003

"Who's with me on this righteous crusade to rid the world of the evil of the "+" operator? "


<g> I believe I can make a half-decent case on the evils of overriding the standard operator symbols in c++ if that would help?

FullNameRequired
Wednesday, June 11, 2003

Tom,

I do stuff like this:

for(int i=0;i<employees.size();++i) {
  Employee *emp=employees[i];
  //do stuff with emp
}

all the time.

It maps well to std:: containers too. Dereference the iterator at the top and then act on the object in the body.

I like it because the code following the loop initialisation reads more clearly and, in the case of std:: containers, it makes it easy to look into the object in the debugger.

Then again my code tends towards short functions in a explicit (some say verbose) style.

Len Holgate
Wednesday, June 11, 2003

Tom, I'll second Len.  I use that all the time.

On Employee[1] vs Employees[1], I can see what you mean by reading it "Employee 1", but the second still makes more sense to me.  I think it's because I read it as "Employees of 1" or "Employees index 1".  So to me, Employee[1] would probably suggest that you were indexing the property with index 1 of an employee (this actually works in Javascript). 

I always suffix my collections with an 's' or with 'List'.

One-Armed Bandit
Wednesday, June 11, 2003

Chris, your sarcasm doesn't accomplish much other than making it painfully apparent that you have no counter argument.

SomeBody
Wednesday, June 11, 2003

Chris, if we took your advice, we'd all be programming in Add(Add(C)).

Mr. Snrub
Wednesday, June 11, 2003

"Chris, your sarcasm doesn't accomplish much other than making it painfully apparent that you have no counter argument."

Since your "argument", if one can call it that, basically consists of "I find loops with i hard to understand" I thought it would be easy enough to figure out my "counter-argument": I find loops with i easy to understand. How about that?

Yeah, there are those really long and complex loops where I prefer to use "speaking" variables like row or iRow, depending on the type.  But generalizing these rare cases to a rule "never use i", especially when propped up with bogus claims like "editors can't search for i" or "sometimes mathematicians use i as the imaginary unit", is really just ludicrous. Redeclaration of a variable isn't a problem either because the compiler will complain.

Proficient programmers read and undertand a loop of a few lines as a single unit.  The name of the loop variable doesn't matter -- in fact, the shorter the better, because it makes it easier to read the whole loop at once.  Using long loop variable names implies you're reading word-by-word, as it were, and that's not appropriate except for complex/long/deeply nested loops.

Chris Nahr
Wednesday, June 11, 2003

That's odd.  In all of my posts, I can't seem to find myself ever saying "I find loops with i hard to understand" or anything like it.  Nor did I ever say "never use i".  I have no trouble understanding loops with "i". 

Assuming we're talking about C/C++ (as I clearly was), if your compiler complains about a redeclaration of a variable in nested scopes, you should report it as a bug.  I would think that a "proficient programmer" would know this.

I'm not sure why you have such a hard time reading "iColumn" versus "i".  I can read them both in one glance with no difficulty. 

SomeBody
Wednesday, June 11, 2003

I don't know where you got the impression that I was talking exclusively to you -- I wasn't. There were others posting in this thread besides you.

I'm currently using C# which in fact does complain about redeclaration in a nested scope, as per the language definition. For C++ you might use PC-lint if your compiler doesn't have a warning for this condition. Allowing variable hiding with no notice is a language design problem, not a variable naming problem.

Anyway, using iColumn instead of i won't help you here, nor with the obsolete VC6 for loop scoping, if a column index is required on multiple levels -- and that's not exactly unlikely if your code requires column indexing in the first place.

What it boils down is that you like to read iColumn because it makes you happy.  That's good for you but hardly a reason why anyone else should adopt this convention.

Chris Nahr
Thursday, June 12, 2003

I guess the only thing that is important is consistancy.

The for-each is cool.  VB has it, IIRC.  In c++, you have iterators or loop counters.

C++ and Java both lack a 'with' operator, which makes pascal so much more readable!

In Symbian style, iSomething is an instance variable.  So iColumn would confuse a symbian programmer if used as a loop counter.

i, j, k is just fine for loops.  Sometimes I give my loop counters more explicit names, like 'row' or 'col' or 'idx'.

I often see m_something as an instance variable name, and I just described the iSomething convention in Symbian.  Personally, I use _something.  Much lighter on the eyes imo.  I use _somethings for the count of a _something array.  So some typical code might be:

for(int i=0; i<_somethings; i++) {
  cout << i << '\t' << _something[i].name << endl;
}

Well, enough about me, nice to contribute to this long thread.

i like i
Thursday, June 12, 2003

*  Recent Topics

*  Fog Creek Home