in reply to Of variable {mice} and its name {man}.

I will agree, variable naming is very important, but it really depends upon scope. If i am writing a one-liner, i'll use the fewest characters possible, such as $f, @f, or %f. If i am writing a throw away script, i don't feel quilty for using foo, bar, baz, and qux here and there. If it is a script that i plan for others to use, i sit and think very carefully about the names. If i am working with a group, i will do my best to stick to whatever standards they have agreed upon.

Coming from Java, i used to name my variables like employeeRecord and thingWithNoName, but now i stick to the convention of using all lower case and separating words with the underscore - thing_with_no_name. This also keeps me from chosing long names since i hate reaching for the underscore key. ;)

When confronted with mental block on finding a good name, if i can't think of one in 5 minutes or less, i will pick something and move on. Leaving a comment that promises to rename the variable reminds me of why i did so. I really feel that names are important, but not so important as to get stuck in rut over them. This is similar to listening to someone's conversation when they pause for far too long trying to find the right word, completely losing their train of thought.

Final comment - plurals. tye finally convinced me to avoid using plurals for arrays and hashes, because they already indicate a colletion: @file, @line, %record. This keeps me from guessing between $file and @file, $record and %record. This is also a new practice for me, so i sometimes forget.


Consistency is golden
  • Comment on (jeffa) Re: Of variable {mice} and its name {man}.

Replies are listed 'Best First'.
Re: (jeffa) Re: Of variable {mice} and its name {man}.
by vladb (Vicar) on Jun 01, 2002 at 14:51 UTC
    jeffa, thanks for your reply ;-).

    I'm not strongly positive on whether to use plurals or singulars for arrays or hashes. I had to struggle with that one a couple times and never seem to be able to find a common ground. In the back of my mind, I too feel that avoiding using plurals in similar instances is the sane thing to do.

    "When confronted with mental block on finding a good name..."

    Of course, variable naming shouldn't be taken to extreme (as also pointed out by Zaxo from the CB conversation ;-). More often then not, I find myself in a similar position where I am not able to come up with a suitable name in a couple minutes. Often, I simply take the best knowledgable guess and proceed. Optionally, leaving a comment just the way you do.

    $"=q;grep;;$,=q"grep";for(`find . -name ".saves*~"`){s;$/;;;/(.*-(\d+) +-.*)$/; $_=["ps -e -o pid | "," $2 | "," -v "," "];`@$_`?{print"+ $1"}:{print" +- $1"}&&`rm $1`; print$\;}