in reply to Re: Naming convention for variables, subroutines, modules, etc.. and variable declaration
in thread Naming convention for variables, subroutines, modules, etc.. and variable declaration

I concur almost exactly.

Whilst not decrying the practice of using a temporary "naming var" for clarity completely, they can often be avoided by other good naming choices.

In your example

foo( $badgers->{lookup} ); ## or foo( $some->{lookup_badger} ); ## or dealwithBadger( $some->{lookup} );

depending upon the circumstances.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^2: Naming convention for variables, subroutines, modules, etc.. and variable declaration
  • Download Code

Replies are listed 'Best First'.
Re^3: Naming convention for variables, subroutines, modules, etc.. and variable declaration
by fenLisesi (Priest) on Nov 24, 2006 at 12:28 UTC
    my %favorite_critter_of = ( andy => 'badger', larry => 'camel', vito => 'cat', ); put_on_cover( $favorite_critter_of{ andy } );

    Cheers.

Re^3: Naming convention for variables, subroutines, modules, etc.. and variable declaration
by jbert (Priest) on Nov 24, 2006 at 11:49 UTC
    Fair comment. It was a fairly bad example on my part. Certainly your underlying point that good naming is always important is something I heartily agree with.

    Thinking about this more, if the expression is used more than once, I nearly always put in a naming var. I guess I then consider the code used in the expression to be repeated code, and effectively factor it out into one place and name it. On a small scale, this is pretty much the same as factoring out common code into a subroutine.

    And since I'm often error checking/logging/throwing exceptions in production code, many/most/all such expressions would get a second usage in the error handling and hence get a name.

    Thinking about my 'naming vars' versus 'variable scope' worry a bit more, I suspect that the reason this doesn't count as "more scope" to me is that I'm not using the variable as a variable. It's an additional name...it doesn't need the full privileges and rights of a variable. I guess I think of it as a read-only alias.

    I guess I could use alias (isn't this recommended in PBP?). But I'm not sure really how perlish that is and whether it raises the bar of code comprehension too high. Maybe I just need to modernise :-)