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

Yuval Yaari yuval at windax.com
Mon Jun 7 13:14:56 PDT 2004


Mikhael Goikhman said:
> On 07 Jun 2004 09:19:47 +0300, Yuval Yaari wrote:
>>
>> Also, long names should be auto-completed, or else you're using the
>> wrong editor.
>
> This is starting to be a non-constructive discussion, but maybe you want
> to hear an alternative opinion.

Migo, I wouldn't say it if I wasn't 100% positive that both emacs and vim
have this feature.
Other editors probably support it too.
Also, you may be unaware of this feature in your editor.

Good thing I didn't mention how your editor should support folds, or you'd
kill me :)

> I would not use the auto-completion of variables and functions even if
> someone implemented it in my editor of choice. Seems so much
> error-prone. When I see a dozen of unrelated (or related) variable names
> as completion choices, the first thing I want is to see the context they
> are used in, so I would need to review a lot of unrelevant code just to
> determine the correct variable to insert. Such way of working seems more
> confusing to me than helpful and one should still type to resolve
> conflicts. I would rather just find the relevant context manually and
> copy/paste the correct variable to avoid any kind of typos. Since most
> of variables are local, they are usually visible, so, again, the
> auto-completion is not needed.

It is implemented in your editor of choice for sure.
Are you copy-pasting the X windows way or using your editor keys (just
interested because I sometimes take advantage of having both
"clipboards")?

Sorry, this is not the case for me unless I make a small script.
You should know that a mod_perl handler subroutine can be pretty large, or
a foreach loop can contain a lot of lines...
After bragging on how I never work on big projects, that is.

I do stuff things into subroutines, but it's not always the right way to go.
I consider making someone skip between subroutine to subroutine just to
understand a pretty obvious (or well commented), yet long, foreach loop
wrong.
Again, that's my opinion, but I am very open to hear yours.

> In general, the smarter is the editor the less people want to think.
> While an editor may help to search quicker, to present nicier (both
> issues are subjective as well), I don't think choosing the correct
> variable to insert in any particular place is such automatical task.

I think editors should be much more than that, but that's my personal
opinion so I wouldn't argue with you.
Basically, you shouldn't be forced into your editor and you should feel
comfortable with it.
I switched many editors before using the current one (which I already
revealed but just so I won't piss anyone off, I'll shut up for now).

>> Not getting into editor wars, but this is so basic, I guess even
>> notepad.exe should have this feature.
>
> I doubt there is an editor that even colorizes (or otherwise parses)
> Perl code correctly in all non-trivial cases.

True. But they do a damn good job anyway! :)
Usually they fall for __END__ and __DATA__ or POD or weird things like that.
I think the main 2 linux editors, emacs and vim (sorry if I'm wrong, I'm
not trying to piss anyone who uses any editor) work well with these
things.

>> I don't copy paste, I always auto-complete, and it works well.
>
> Just wait for a large project with a lot of variables/functions to see
> how inconvenient this feature is. Anyway, I am glad you use it, I am
> less glad you call editor "wrong" based on something questionable. :)

Your editor supports it, you might not be aware of it.

Weird to hear about large projects from someone who just mentioned that
all his variables can be easily found in the same scope.

Maybe I'm a bad/sloppy programmer, but my variables aren't usually
decalred in the same viewable screen, and though I do sometimes work with
split screens - the last thing I'd want to do is trace the last line I
used that variable and copy-paste it.

Copy-pasting your variables is less error-prone, I agree.
But that would cut my work flow and line of thinking, I guess.

Auto-completing variables has become something I do unconciously
(spelling?) - just like you might complete command in bash (or zsh in your
case :)).
As I said, I can see how it can be error-prone, but 99% of the times, I
get what I want.
My large projects don't have more than 3-6 variables that might have a
similar name (read: more than the same 2-3 first letters).

When this happens, you should usually use a hash, e.g:
my $CarColor = "blue";
my $CarBrand = "BMW";
my $CarGear = "manual";
Would be better off as:
my %Car = (
    'Color' => 'Blue',
    'Brand' => 'BMW',
    'Gear' => 'manual',
);

Sometimes you /can/ get into a situation where 2 variable/function names
are similar, but that only makes you type your mighty-auto-completion-key
once or twice more.
Compared to looking above (sometimes opening a split window, or pressing
page up or whatever), I would say pressing that magic key between 1 to 3
times is more effecient.
I think looking above would cut your work-flow or your line of thought.
Again, this is my PERSONAL opinion.

Also, when the variable can be seen on the same screen or one screen
above, auto-completion would, in most cases, give you the correct result.

So you basically type what you want, press the magic-auto-completion-key
(which should be easy for you to type) until you see what you want, and go
on typing.
It sounds error prone, but after 1-2 weeks you get the hang of it.
Not to mention that I've been using this for over 1.5 years now.
I hardly ever got into a situation where I mis-completed a name.

Oh, and yes, some projects were large.
And some of the files I work on were written more than 1 year ago - so you
really don't have to memorize all your variable names to use this feature.
Unless, of course, $NaxuySukaBlat looks logical to you :)


  --Yuval

P.S: Your editor is your power tool. I am always looking for an editor
that would be more comfortable for me (see my Eclipse mail from today).
You got too attached to your editor (I know the feeling...) and you should
really use the one that gives you the most features for the fastest
development speed (not just syntax highlighting and search features but
also debugger integration, Perl syntax checking, mine has a
Devel::SmallProf output integration, etc).
I would switch editors for a better Perl debugger integration.
Or a side list with all my variable names so I would know at any given
time what variables I used without looking for them.

Do you guys even use anything besides your editor and perl (as opposed to
Perl) for your Perl development that you don't think that your editor is
your power tool???
Oh, grep/cut/wc -l should also be able to integrate into your editor so
don't mention these :)





More information about the Perl mailing list