Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day hakonhagland,

"It sometimes bugs me that it is so difficult to write Perl code that is readable (easy to follow) when working with references."

About a dozen or so years ago, I supervised a number of junior programmers who also seemed to have this problem. I won't go into details beyond saying this caused no end of problems and time spent on debugging exceeded time spent coding.

I introduced a local coding standard, that required a prefix on all variable names whose values were references. There may have been some special cases, but these were the main ones:

  • $rs_name : scalarref
  • $ra_name : arrayref
  • $rh_name : hashref
  • $rc_name : coderef
  • $rg_name : globref
  • $ro_name : object reference

The concept was simple and mostly fixed the problem. Using the wrong operation on a variable was usually easy to identify (e.g. $ra_name->{...}, $rs_name->method(...), $rh_name->(...), and so on). Subsequent reading of the code, for maintenance or debugging, was made easier.

I should also point out another policy that the 'name' part had to be meaningful and, as far as possible, self-documenting. This typically meant that, if a wrong letter (identifying the reference type) was used, it would be picked up by strict (whose use was also mandatory).

While this was fine for that situation and environment, it doesn't really suit my personal style and I no longer use it: I much prefer to use the smallest possible, lexical scopes where these sort of problems generally don't occur. However, if you think this, or something like it, will help to improve your coding, perhaps give it a try and see if it works for you.

"Let's denote this new syntax by Optional Postfix Reference Declaration Syntax (OPRDS)."

I didn't like this idea at all. With postfix dereferencing, $var remains the variable and ->@* is an operation on that variable. With your OPRDS, $var->@ seems to be a separate variable and operation (in your OP); subsequently, in one of your responses, you use func( $var->@ ), where $var->@ now apparently represents the entire variable. You also seemed to get confused with "func( $var->@* ) ... the function will receive $var->[0] ...": in fact, the function will receive @$var.

You may have had typo(s) in that response, but I found myself scrolling back and forth to understand what was going on: the very problem you're attempting to avoid: "... scan source code nearby in order to determine ...".

— Ken


In reply to Re: Improve readability of Perl code. Naming reference variables. by kcott
in thread Improve readability of Perl code. Naming reference variables. by hakonhagland

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-26 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found