Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Improve readability of Perl code. Naming reference variables.

by hakonhagland (Scribe)
on Jan 21, 2017 at 08:48 UTC ( [id://1180072]=note: print w/replies, xml ) Need Help??


in reply to Re: Improve readability of Perl code. Naming reference variables.
in thread Improve readability of Perl code. Naming reference variables.

Hello kcott!

Interesting to hear about your experience teaching students. I am sure the style you introduced might indeed help improve the situation you described. But once you have declared a variable with a prefix, it is no longer optional to remove the prefix. This is why I don't like the idea of a prefix that is part of the variable name. A prefix as a part of the sigil would seem like a better idea. Then it could be made optional.

For example, consider a function called with three references. A scalar reference, a hash reference, and a string reference;

sub func { my ( $rs_str, $hr_desktop_info, $ha_files ) = @_; $$rs_string = update_string_ref(); for ( keys %$hr_desktop_info ) { ... push @$ha_files, $file; } .... }
I seems to me like the prefixes will introduce too much noise in the source code. In this case, it might be better if only the first line in the function documented the type of the reference, and then subsequent lines could omit the variable name prefix:
sub func { my ( $rs_str, $hr_desktop_info, $ha_files ) = @_; $$str = update_string_ref(); for ( keys %$desktop_info ) { ... push @$files, $file; } .... }
Of course, the above code is not yet possible. And further it could not easily be made part of Perl in the future. But maybe a new type of prefix could be used, for example $>$, $>%, and $>@ ?
sub func { my ( $>$str, $>%desktop_info, $>@files ) = @_; .... }
On the other hand, I can see the clash here with the Perl special variable $> (The effective uid of this process). So this syntax might be difficult to implement.

Regarding the last point of your reply. Yes, I agree that if I call func( $var->@* ), the function will indeed receive @$var. But I assumed a function definition on the form

sub func { my ( $var ) = @_; ... }
Now, the function would "receive" $var->[0] ( in the sense that $var in the function will be equal $var->[0] of the caller). But I think this (minor) issue of whether the function receives the whole array or only its first item is just a distraction from the main topic of the discussion. So I will not go further into the issue.

Replies are listed 'Best First'.
Re^3: Improve readability of Perl code. Naming reference variables.
by kcott (Archbishop) on Jan 21, 2017 at 20:06 UTC
    sub func { my ( $rs_str, $hr_desktop_info, $ha_files ) = @_; $$str = update_string_ref(); for ( keys %$desktop_info ) { ... push @$files, $file; } .... }

    I rather feel that $$str, %$desktop_info and @$files make it pretty clear, not only that your dealing with references, but also what type of references they are.

    If you're having problems reading that, I suggest you do what ++stevieb has already alluded to and put the prefixes in a comment. Something like:

    my ($str, $desktop_info, $files) = @_; # rs, rh, ra

    Update (minor typo fix): s{deck}{desk} in ..., %$decktop_info and ....

    — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-25 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found