Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Code style question -- code review

by Discipulus (Abbot)
on May 20, 2021 at 19:17 UTC ( #11132803=note: print w/replies, xml ) Need Help??


in reply to Code style question

Hello AlexP and welcome to the monastery and to the wonderful world of Perl!

Style is anyway a matter of taste. For what my opinion is worth I glanced your code and I'd say:

@EXPORT and @EXPORT_OK are different: the latter makes no sense if you already export some function.

Do you really need Exporter 5.74 and Scalar::Util 1.56 ? You must be as flexible as you can with users of your module: no need to ask them to upgrade to last version unless you know is the rigth thing to do. For example let say Scalar::Util included the blessed function from x.x version but you know your usage of it can trigger a bug up to x.y verion then and only then you ask the end user to have x.y version.

Many authors use names with a leading underscore for private functions, even if they are not private.

I prefere the and put at the end of the line and all and aligned: the same for or and for thernary like ? and : because somenting at the end of line is easier to spot.

In your sub get_by_single_key you have if .. if .. if and this, style apart, sounds wrong: if .. elsif .. else sounds more appropriate.

You set all return explicit and this is good and make the code more readable. Someone likes shorter statements like returns 1 if $x but this is a matter of taste.

I dont like too much blank lines (again a matter of taste): for me, closing a brace } does not need a blank line after it.

L*

PS you might be interested in my step-by-step-tutorial-on-perl-module-creation-with-tests-and-git even if i see you already implemented some test: good job!

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Code style question -- code review
by GrandFather (Saint) on May 21, 2021 at 03:18 UTC
    "I prefere the and put at the end of the line and all and aligned: the same for or and for thernary like ? and : because somenting at the end of line is easier to spot."

    I emphatically disagree! The easiest stuff to see is at the start of the line. I frequently break complicated expressions over lines so that it is easier to understand them. For example, the following test from a C++ project I'm working on right now could be laid out several ways:

    if (currMaj < bestMajVer || (currMaj == bestMajVer && currMin <= bestM +inVer)) // Current image is no better than the best so far if (currMaj < bestMajVer || (currMaj == bestMajVer && currMin <= bestMinVer) ) // Current image is no better than the best so far if ( currMaj < bestMajVer || (currMaj == bestMajVer && currMin <= bestMinVer) ) // Current image is no better than the best so far

    To my eye the last version is much easier to understand than the previous two.

    One of Perl's features that I really like is the provision of statement modifiers because, for short statements at least, I can get compact expressive code with the important bits in the right order:
    <do this interesting thing> <while/for/if> <something vaguely interesting>.

    For the same reason the default increment operator should be pre-increment (++$thing) instead of the cargo culted post-increment operator ($thing--). There are places where post-increment must be used, but they are rare and the default should always be pre-increment just because the operator is easier to see and the operation is less surprising.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Hello GrandFather

      > I emphatically disagree!

      :) as I told, I suppose is a matter of taste. I tend to use something like:

      # complex clause if ( defined $thing and $thing > 3 and $thing <= 42 and 0 == $thing % 2 ) # ternary print (defined $thing and $thing > 0) ? $thing : 'not';

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: Code style question -- code review
by AlexP (Beadle) on May 21, 2021 at 07:46 UTC

    Thank you very much for your time and detailed comment.

    According to your information, I can make a small manual.

    About versioning, is there a way to tell perl use version >= 1.x or what can be done?

      For using modules, the version specified is, by default, the minumum version. The same is true of perl itself, so:

      use 5.010; use Text::Diff 1.40;

      means that the script must be run using perl 5.10 or newer and must be able to load Text::Diff version 1.40 or newer.

      See the docs for use for more detail.


      🦛

        Many thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2021-09-19 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?