Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Is there a cleaner way to write this code below, which includes derferencing references, that themselves have to be dereferenced?

by stevieb (Canon)
on May 05, 2022 at 19:50 UTC ( [id://11143603]=note: print w/replies, xml ) Need Help??


in reply to Is there a cleaner way to write this code below, which includes derferencing references, that themselves have to be dereferenced?

Excellent feedback so far. The only thing I'd add is to lose the comments.

Let the code (ie. the proper naming of variables and function names) do the saying of what is happening. Comments should only be used to describe *why* something is happening. A comment in code should be a rather rare occurrence. If I see a comment in code, it's to point out why the following code is doing something that goes against the norm.

For example:

### execute command ### get the data $dbHandle=$db->prepare($dbCommand); $dbHandle->execute(); my $data = $dbHandle->fetchall_arrayref();

It's readily apparent that execute() and fetchall_arrayref() are performing the "execute command" and "get the data".

I know it's instinctive when first writing code to pepper it with comments, but trust me, if you practice without them, you'll become more efficient as a code reader and writer more quickly. It will force you to come up with more descriptive names for various items within your code.

Example:

my $var1 = 0; my $var2 = 1; # Draw an axis with a pencil and a ruler function($var1, $var2);

vs:

my $pencil = 0; my $ruler = 0; draw_axis($pencil, $ruler); sub draw_axis { my ($pencil, $ruler) = @_; if (! $pencil) { # On rare occasions, a pencil might be broken, so do this $pencil++; } }
  • Comment on Re: Is there a cleaner way to write this code below, which includes derferencing references, that themselves have to be dereferenced?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Is there a cleaner way to write this code below, which includes derferencing references, that themselves have to be dereferenced?
by misterperl (Pilgrim) on May 05, 2022 at 19:54 UTC
    I like your "style" sir - I do that but I realize I could do MORE of that. Making perl "english-like" and avoiding loops make life so much more readable.. AND using strict and bareblocks to minimize scope..

    I ++'d you :)

Log In?
Username:
Password:

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

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

    No recent polls found