News for hackers. Stuff that matters.
Re:Best Practices of Variable & Method Naming by edw519 on Saturday February 25, 2012 @09:17AM (#3632956)
16. No variable name should be fully self-contained within any other variable name:

Wrong: Cust & CustCtr

Right: CustID & CustCtr

You should be able to do a global search with any tool and get every instance of that variable with no instance of any other variable.

This pretty much kills "1. 1 char loop counters", which is a good thing.

Re:Best Practices of Variable & Method Naming by Raphael_Amiard on Saturday February 25, 2012 @09:52AM (#3633031)
> This pretty much kills "1. 1 char loop counters", which is a good thing.

Why do you think this is a good thing ?

Re:Best Practices of Variable & Method Naming by mattdeboard on Saturday February 25, 2012 @10:05AM (#3633058)
"for node in nodes" is preferred, at least IMO, to "for i in nodes"
Re:Best Practices of Variable & Method Naming by mudetroit on Saturday February 25, 2012 @11:19AM (#3633220)
"for node in nodes" implies you are returning the node itself and not simply a counter. This would seem to fall more under his 1 word for loop variables idea.

* NB: I am not certain whether I agree with such rules or not, but that is another discussion point.

Re:Best Practices of Variable & Method Naming by mattdeboard on Sunday February 26, 2012 @01:10AM (#3634973)
You must be a functional programmer :) My specific example was Python-related; but yes, if I'm using a functional language (or comprehensions in Python) I'm much more inclined to just use i/j/k/x as the counter. But for loops in Python don't return anything of course except in `break` cases.
Re:Best Practices of Variable & Method Naming by inerte on Saturday February 25, 2012 @10:17AM (#3633083)
"i" as in a counter is almost a standard for any code in any programming language. "i", "j" and "k" (comes from math). If you have the loop counter as "node", then you're doing it wrong, but if you have an actual node, then it's ok.
Re:Best Practices of Variable & Method Naming by kstenerud on Saturday February 25, 2012 @10:41AM (#3633140)
Or, if you want to be sure that a search/replace of a name won't go awry, you could use names such as customerIdx, nodeIdx, accountIdx. It also helps keep things clear in the few cases where you find yourself with nested iterations (so that you don't accidentally call customers[j] when it should have been customers[i]).
Re:Best Practices of Variable & Method Naming by mattdeboard on Sunday February 26, 2012 @01:06AM (#3634967)
If I've got an iterable of Foo object instances, I'll call it "foos". When I iterate, I'll write "for foo in foos" without exception. Calling it i, j or k is fine, too, but I try to be as obvious as possible, because I believe that is a virtue.
Re:Best Practices of Variable & Method Naming by greggman on Saturday February 25, 2012 @01:02PM (#3633428)
I no longer use i as a counter because a few times in my career I've mistaken i for 1 or l.
Re:Best Practices of Variable & Method Naming by 83457 on Saturday February 25, 2012 @12:39PM (#3633375)
This is not necessary. Just search for regex \bVarName\b when doing a replace. If looking for function calls look for \bFuncName\( etc. The chances that a large mult-developer codebase will have overlapping variable or functions names is extremely high.
Re:Best Practices of Variable & Method Naming by rgc4 on Saturday February 25, 2012 @12:33PM (#3633363)
I agree that you should never use 1 character loop counters, but a lot of the lazy programmers out there don't agree. They've never had to do maintenance programming (or heaven forbid make old code actually work/bugfree/do something new) that has the following variable names:

i ii iii j ij jj ji jjj jij jjj

One letter loop counters and their ilk are always a bad idea. But saying so angers so many people because they can't possibly ever be wrong.

Re:Best Practices of Variable & Method Naming by huggyface on Saturday February 25, 2012 @04:02PM (#3633844)
You should be able to do a global search

I would offer that you should have tools that are smarter about your code, with a contextual awareness of what things are and how they are used. Many coding purported "best-practices" are based upon such a tool deficiency.

Let me give one example that is a serious peeve of mine -- C# allows you to define region. I have to interact with code that uses regions to define accessibility or type of members.

#region private members

#endregion

This was communicated as a "best practice" because it allows you to hide/show appropriate parts of the code.

Yet the primary tool -- Visual Studio -- has fantastic tools for exactly that use. Tools that don't muddy up the code with unvalidated, quickly out of date metadata (e.g. nothing stops me from putting a public in there).

This holds true for many, many coding conventions. We discarded hungarian notation for the same reason that many of these other naming guidelines are of dubious merit.