Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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:

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.

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://977874]
[shmem]: more garbage in, more garbage out that would be
[stevieb]: I found that win10 broke a C# library I was using for one project while enhancing tests for a Perl dist, which breaks other Perl dists, and I'm about to throw my hands up on berrybrew. win2k12 broke one thing, win10 breaks something...
[stevieb]: ...unrelated which requires replacing a lot of code and a whole lib. I'm about to go nix only ffs
[shmem]: stevieb: what you're doing sounds afwully complex. Too much for me this evening to provide brighter insight ;-)
[stevieb]: I don't even own a Windows computer. Both my girl and I have a laptop each with Linux. I'm supporting Windows in some of my projects and I can't even guage whether it's worth it or not.
[stevieb]: shmem It's something I desired to have years ago, which is why I took over berrybrew. Cross-platform build/test automation locally, or over the network Test::BrewBuild
[shmem]: sounds good.
[shmem]: but I'm crumbling smaller stones. remember...
[stevieb]: I'm working on it to fatten it up and make it more reliant so I can finalize my Raspberry Pi automated build system for that software :) It's all well and fun, until I try to make it work with Windows lol

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-03-28 22:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (342 votes). Check out past polls.