Variable Naming [was Re: [] catching $a, $b unnecessary my and maybe other things]

Shlomi Fish shlomif at
Mon Jun 7 03:43:47 PDT 2004

On Monday 07 June 2004 09:15, Yuval Yaari wrote:
> Shlomi Fish said:
> > On Sunday 06 June 2004 20:48, Yuval Yaari wrote:
> >> I said:
> >> > 1) I thought it's obvious. I've read about it so many times.
> >> > Personally, I hate people who give names like $a and $b (though I
> >>
> >> did it a few times and noticed that problem - but don't tell
> >> anyone). But it's a well known... bug (?) in strict because of
> >> functions such as sort.
> >>
> >> > 2) Did you try use diagnostics; ?
> >> > Also, use strict; use warnings; and/or use diagnostics; do not
> >>
> >> replace re-reading your code, or debugging it.
> >>
> >> > Though they usually solve all of my problems :)
> >>
> >> use diagnostics; didn't work.
> >> Keep us updated if you find something...
> >>
> >> As for what I said above, I don't really hate people who use $a and
> >> $b, I just prefer normal names for variables (and for crying out loud,
> >> your editor should auto-complete it for you so you write once and M-/
> >> later [sorry for being emacs-specific]).
> >
> > Well, naming variables is something that requires some thinking. For
> > once, I  disagree that giving variables long names is a healthy
> > practice, just because  your editor has auto-completion. You can't rely
> > on the editor having that  (joe, classic vi, etc.), and extremely long
> > variable names make the code more  difficult to read because less code
> > can be fit on one line.
> A programmer should use an editor with auto-completion.

People should use an operating system that doesn't crash three times a day.

People should use a standards-compliant browser that doesn't have more 
security holes than Swiss cheese. (oh wait - most of them don't).

The Technion should teach a shell that wasn't shown to be considered harmful, 
and isn't loathed by any half-witted UNIX hacker. (oh wait - it doesn't).

Seriously now, not all editors implement auto-completion, just as not all 
editors have built-in functionality to read mail, browse the web or talk on 
the IRC. (;-)) Auto-completion is not yet considered a feature that every 
editor must have, much less a feature that all programmers use, or wish to 
use. Maybe some years from now this situations will change, but at the 
moment, I and many other people do not use auto-completion, or use editors 
that do not.

I don't think auto-completion is editor-bloat or creeping feuturism. It's a 
useful feature. However, it's still the programmer's choice to use it or need 
it. gvim, which I use daily, has many features that I don't utilize, even 
though they may be useful. So is auto-completion.

> Your editor is your power tool.

"power tool"... not again! A programmer's editor is one of his most commonly 
used tools. Whether it is a "power" tool, is debatable. Some people can draw 
well with a computer carying a host of very feature-ful, and sometimes very 
costy, computer graphics programs. Others can draw very well by using a 
pencil, or editing the SVG/EPS/whatever file directly. Would you call a 
pencil a power tool?

Similarly some people use Emacs or some complex IDE for their editing tasks, 
while others are using joe, classic vi, or some other feature-impaired 
editor. A code a programmer produces should be editable by all editors, not 
just those that have particular features. (auto-completion in our case)

> Names should be 20 chars long, but should describe it.

This sentence does not make sense. It's probably:

"Name should not be 20 chars long, but should describe it."

If so, then I agree.

> Also, if you find that your name isn't enough, but some comment near the
> first decleration.

Did you mean "put some comment". Yes, you're right about that. Albeit comments 
are not a substitute for meaningful variable names.

> > While I agree that it is not a good idea to name one's variables "a" and
> >  "b" (especially in Perl due to this known limitation), I sometimes use
> > $i and  $j as indexes, because it's a well-acknowledged convention that
> > they are  such. In Matlab, OTOH, using "i" and "j" is possible, but
> > would cause you to  lose the imaginary numbers unit (as in the complex
> > number 5+6*i), so one has  to resort to "a" and "b" or some similar
> > convention.
> I once read, and I agree with it ever since, that if the scope of your
> variable is 1 screen (visuaully, that is) or less - you can name it any
> way you want.
> I too named some vars $i, $j, $tmp, $idx - but you could see their
> decleration and demise (I usually add undef($var) on purpose so I'd know
> when a variable should not longer be used, or else "strict" comes in) in
> the same screen.

<nod />

> > Generally a variable name should be long enough to convey its meaning,
> > but not  long enough to take a lot of screen real-state, and be hard to
> > write. You can  hunt me down and shoot me if you want, but I never tried
> > to enable  auto-completion in gvim, albeit when working in MS Dev
> > Studio, I found it  relatively useful. (albeit not something I couldn't
> > live without).
> Auto-completion is a must.
> Not only because I'm lazy (I am, that's why I use Perl!) - but it makes a
> lot of [even all] typos go away.
> My editor auto-completes Perl functions aswell as already written words.

See, what I said earlier about auto-completion. And you can say that I'm lazy 
enough to remember another keystroke, or to try to enable it in gvim. I'm 
also much less reluctant to switch to a different editor, because I'm used to 
gvim, (in my particular, highly non-conventional configuration).

As for typos, "use strict" catches most of them. I sometimes confuse between 
two distinct variables. (with completely distinct names) In this case, 
neither auto-completion, nor strict help.

> > Generally, using abbreviations for variable names is a good idea. They
> > make  the code flow better.
> I disagree.
> Maybe if you put a comment on the 1st decleration.

Note that I meant abbreviations, not acronyms. By this I mean using "num" 
instead of "number", "perm" instead of "permutations", etc. As for acronyms 
(="rashey teivoth"), they are usually Evil as variable names, unless it's a 
well-established one. You can use "$tcp_port" instead of 
"$transfer_control_proto_port" for example.

> >> I also know a russian programmer who named his variables
> >> $NaxuySukaBlat, etc...
> >
> > You mean he used Russian for his variable names? Ouch. It reminds me of
> > an  implementation of Freecell that contained a solver I encountered
> > once. It was  written for gtk, GPLed and all, but when I looked at its
> > source code it was  completely in French! (comments, variable and
> > function names - the works). I  did not try to make sense of it.
> That's not just russian - you should know by now what these things mean :)

I have no idea what "Naxuy Suka Blat" means.

> >> But at least strict would work for him in such case :)
> >
> > Strict, yes, but not non-Russian speaking reviewers and
> > fellow-programmers.  And compatibility with them is more important than
> > using strict.
> Of course, and I was just kidding.

Heh heh.
/me taking jokes too seriously as always

> I'm going to be late for work if I don't stop reading mail in the
> morning...

Heh heh. I read some of this discussion, before I needed to go (walk, 
actually) to a job interview in the morning. I was still able to arrive about 
10 minutes early.


	Shlomi Fish

Shlomi Fish      shlomif at

Quidquid latine dictum sit, altum viditur.
        [Whatever is said in Latin sounds profound.]

More information about the Perl mailing list