Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Perl Best Practices for naming variables

by mascip (Pilgrim)
on Jun 22, 2012 at 16:25 UTC ( #977874=note: print w/ replies, xml ) Need Help??


in reply to Perl Best Practices for naming variables

That's an important point, and subjective too.
I like my code to look as much as possible like english : i like when i just read it and it says what it does. Without having to remember that some letters are just here to give technical information, so "you have to try to imagine that they are not there."

I tried to use _ref. Then i tried using _hash and _list, but it didn't work for me : the code becomes less readable. I don't like:

$age_of_hash->{Sam};
Sam is not a hash.

Then i tried prefixes instead of suffixes, and i liked it better : i know that $h_ corresponds to %, and it doesn't get in the way so much. The beginning of a variable name feels (to me) like the right place to say its type.

$h_age_of->{Sam}
feels better.
I even thought of using capital letters :
$H_age_of->{Sam} $L_items->[2]
I think i like these.
It doesn't make the "forgetting an arrow mistake" as obvious as suffixes, but it's better than nothing, and it doesn't get in the way as much.

Thank you for speaking about Data::Alias. I will give it a go. The % and @ symbols make for more readable code, I was wondering if it was possible to keep them. Every little help is good to take.

~ ~ ~

Another thing i have remarked, is that i don't always know what there is in a hash, or what keys i'm supposed to use with it. For example :

my %data_from; # File? Measurement? Experiment? etc. my %height_of; # Furniture? Object? Person? etc. my %results_list_for; # the keys could be almost anything
One way to deal with this is to make names more explicit. Or to add this information at the end of the hash, like this for example :
$data_from_measurement{measurement->label()} $height_of_furniture{$old_furniture} $results_list_for_file{$file_path}
But it feels quite heavy to me.
What i like about it, though, is that it tells me which type of key is expected in the hash, and what the hash could contain, too. It does tell me a lot. I might use it in some occasions...

Maybe that a different style is better in different contexts... Spinach for thoughts.

It take lots of effort to make something easy to read, and people often don't notice it, but it does help them a lot.


Comment on Re: Perl Best Practices for naming variables
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-09-22 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls