Good variable names (Was: Re: Best way to make a list unique?)

Roy Smith roy at panix.com
Tue Mar 8 13:33:48 CET 2005


"Diez B. Roggisch" <deetsNOSPAM at web.de> wrote:
> inserted = set()
> res = []
> for e in listA:
>    if not e in inserted:
>        res.append(e)
>        inserted.add(e)
> listA = res

I'm going to go off on a tangent here and put in a plea for better variable 
naming.  I'm looking at the above code, and can't figure out what "res" is 
supposed to be.  Is it short for "rest", as in "the rest of the items"?  
Residual?  Result?  Restore?  Any of these seems plausable.  Not knowing 
which makes it much harder to understand what the code does.  When you say 
"res.append(e)", are you building up the result that you're going to 
return, or are you building up a list of elements that got eliminated (i.e. 
residual) because they are duplicates?

Yes, I did eventually figure it out.  It didn't even take that long, but 
this is a trivial little piece of code; it shouldn't have taken any time at 
all to figure it out.  All you saved by using "res" instead of "result" was 
9 keystrokes ("ult", in three places), but it cost every one of your 
readers extra brainpower to figure out what you meant.  To quote one of 
Aahz's better signatures, "Typing is cheap.  Thinking is expensive." :-)



More information about the Python-list mailing list