Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: How Critical is Critic?

by kcott (Archbishop)
on May 29, 2023 at 10:51 UTC ( [id://11152456]=note: print w/replies, xml ) Need Help??


in reply to How Critical is Critic?

G'day Bod,

Perl::Critic is based on the book Perl Best Practices by Damian Conway (aka TheDamian here at PerlMonks). He is very clear that his book offers a series of guidelines, not hard-and-fast rules. However, as you "wound up the severity", those guidelines and suggestions probably started to look more like rules and regulations.

In my opinion, the practices in the book are generally solid; I probably follow many of them without even thinking. There are some, however, that I don't agree with and, consequently, don't use.

If you look at the Perl::Critic distribution page, you'll see a huge number of Perl::Critic::Policy::* modules; these have explanations of why your (or anyone else's) code was criticised. Here's a quick rundown of the modules relating to the points you raised (I've omitted the Perl::Critic::Policy:: namespace from the link text in each case):

BuiltinFunctions::ProhibitStringyEval
In the main, I follow this policy. The most likely exceptions would be code like 'eval "require $module;";'. I'd question the usage you show. I don't know the context, but in isolation it looks odd:
$ perl -E 'our $V = q{0.1_1}; $V = eval $V; say $V' 0.11

It seems like extra work to achieve:

$ perl -E 'our $V = q{0.11}; say $V' 0.11
RegularExpressions::RequireDotMatchAnything
RegularExpressions::RequireExtendedFormatting
RegularExpressions::RequireLineBoundaryMatching
In my opinion, telling people to always use /xms promotes cargo-cult programming. I wonder how many people who use those modifiers actually understand them. None of them makes any sense with your split pattern.
Subroutines::RequireArgUnpacking
I almost always do this. I would have written your code as:
sub new { my ($class, %attr) = @_; ... }

For a rare exception, see "Re: Optional Subroutine Arguments Like in print FILEHANDLE LIST":

sub queue { my $Q = ref $_[0] eq 'GLOB' ? shift : \*STDOUT; my @list = @_; ... }

I'd say you have two basic options.

  • Run your code through perlcritic. Pick and choose the criticisms you believe to be valid. Possibly modifying your code accordingly.
  • Choose to make perlcritic a formal component of your code. Make all changes that it suggests. Avoid criticisms you strongly disagree with using "## no critic" — see "Perl::Critic: BENDING THE RULES".

I see you're using http://perlcritic.com/; I'm not familiar with that URL. See also: perlcritic (for on-the-fly, command line critique) and Test::Perl::Critic (to integrate with your test suite).

In addition to "Perl Best Practices" (2005 book) you may be interested in "Modern Perl Best Practices" (2014 video course) also by Damian Conway. I own a copy of the former and have read it a few times; I've only seen a trailer for the latter work.

Finally, I've voiced my opinions here; I'm well aware that others have differing opinions. Listen to all for a spread of advice then make your own mind up. :-)

— Ken

Replies are listed 'Best First'.
Re^2: How Critical is Critic?
by cavac (Parson) on Jun 01, 2023 at 09:25 UTC

    I agree. You have to customize Perl::Critic to your own requirements. For example, i have upped the "maximum complexity score" and "maximum length of functions" of some parts of my code quite a bit. When you write complex state machines that (for example) handle HTTP connections. the main state machine winds up to be quite long. Yes, i could split that up into more functions, but in my case it would hinder readability and up the mental complexity while debugging.

    Perl::Critic is certainly a helpful tool. But only after adapting it to the projects requirements and coding style. Plus, P::C regularly behaves stupid when new Perl features come out.

    It's also important to remember that P::C only does static analysis. But only you can prevent forest fires the perl interpreter can properly parse Perl. And some P::C recommendations might even go against project/company policies.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re^2: How Critical is Critic?
by choroba (Cardinal) on May 30, 2023 at 08:33 UTC
    Great reply++!

    See also Perl::Critic::Community for a different set of perlcritic rules.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Thanks for the comment and upvote.

      Thanks also for the link. I checked through all of it and there's only three things I'm not compliant with:

      • I never really liked FindBin; thankfully, I don't need it that often. I'm pleased to have found some alternatives.
      • I agree with issues noted about JSON and JSON::XS: I'll be happy to trial JSON::MaybeXS.
      • Typing 'while (<$fh>) {' is almost muscle-memory; I'll have to train muscles to add 'my $line ='. :-)

      I've been getting questions about using some form of perlcritic at $work. Although arguing against full-blown Perl::Critic usage, I think Perl::Critic::Community is probably something I can get behind. So, that link was very useful and thanks again.

      — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found