Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Coding style: truth of variable name

by GrandFather (Sage)
on Apr 19, 2020 at 00:44 UTC ( #11115760=note: print w/replies, xml ) Need Help??


in reply to Coding style: truth of variable name

In general the cost in any sense of introducing a new variable is trivial or nothing so go wild. Using appropriate variable names is a large part of good coding technique. Having a variable change its stripes easily leads to hard to understand code. The issue is added "cognitive load" - the reader needs to remember more stuff to understand the code.

Another thing to think about is how does changing the meaning of a variable affect debugging? If you introduce a new variable it means you have both versions available for inspection in a debugger at the same time so it can be much easier to see where unexpected results were introduced and why. For this reason I often break down complex expressions into multiple statements with appropriately named variables holding intermediate results. It makes writing, debugging and maintaining the code easier.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
  • Comment on Re: Coding style: truth of variable name

Replies are listed 'Best First'.
Re^2: Coding style: truth of variable name
by BillKSmith (Prior) on Apr 19, 2020 at 03:20 UTC

    I was about to post exactly the opposite advice, but your post changed my mind, at least conditionally. The key concept is "appropriate". Extra variable names only reduce the "cognitive load" if both variables have appropriate names and scopes. I once had a customer who insisted that we always use the extra names. Any advantage was lost because he also insisted that all variables be named 'parmxxx' where xxx was a serial number he assigned.

    I often use your technique of breaking up complex statements on "Schwartzian Transforms" (Refer to How do I sort an array by (anything)?). I almost never code them right the first time. I usually rewrite them in the idiomatic form after I am satisfied that they are correct.

    Bill
Re^2: Coding style: truth of variable name
by perlancar (Friar) on Apr 19, 2020 at 01:19 UTC

    Thanks for the debugging perspective, I didn't consider it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11115760]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2020-09-21 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (126 votes). Check out past polls.

    Notices?