I liked Damian Conway's book Perl Best Practices. It had a lot of sound advice that can help a programmer or programming group decide on a set of house rules. For those who aren't interested in making a lot of decisions, it can even be used as a pre-built set of standards (although a few of its suggestions, generally those involving modules releaed by Damian for the book, are untenable).
Perl::Critic provided a fantastic way to check your code gains the rules in PBP, or against many other kinds of rules, and it became quite popular.
What has begun to really drive me nuts is the application of these rules without regard to how they affect the program. More than once, now, I've seen code changed to comply with PBP, only to be completely broken as a result. To paraphrase Mark Jason Dominus, apparently it is important to these programmers to get the wrong answer as maintainably as possible.
Sure, tests would solve this problem, but Perl::Critic can't tell you that you have uncovered branches. It can just tell you that you separated statements with a comma, or that you named a method after a builtin.
Here's tonights example, which drove me to rant about this:
sub prepare_report {
my ($self, $file) = @_;
$self->analyze($file)->report;
}
Oh no! There's no explicit return statement! We better fix that or Perl::Critic will complain!
sub prepare_report {
my ($self, $file) = @_;
$self->analyze($file)->report;
return;
}
Ugh!
Why "Ugh!"? (Score:1)
In the absence of some explicit documentation that says the example function doesn't return anything, the change you show seems reasonable.
And if you have explicit documentation that says the example function doesn't return anything, the change also seems reasonable -- it guarantees that someone using your interface can't ignore the documentation, depend on undocumented functionality, and force y
Re: (Score:2)
In the absence of some explicit documentation that says the example function doesn't return anything, the change you show seems reasonable.
I disagree. If it doesn't say it returns nothing, then you should assume it returns something.
In complete absence of info on what it returns, the code should thus become:
Re:Why “Ugh!”? (Score:1)
Mindlessly putting it one place is just as wrong as mindlessly putting it in another. If you don’t know where it goes, you don’t put it anywhere – you go and look at the docs and the rest of the codebase. If that’s not in the cards, for whatever reason, then you leave damn well alone – even if that means Perl::Critic continues moaning about it. Because it should. Filling in the paperwork just to make it happy without knowing what you’re doing is just self-deception.
Re: (Score:1)
Beyond that, though, this change caused the entire module to become completely useless, which means that it wasn't desk checked. Why not? Because the module wasn't undergoing any actual maintenance. In the course of other work, someone saw Perl::Critic complain about "no explicit return" and added one, without further testing.
Sure, automated testing would've caught this, but if there are no automated tests, you don't make cha
rjbs
In all fairness (Score:1)
The original code doesn’t make it obvious whether anything should be returned or not. Putting in an explicit return – assuming, of course, that you put it in the right place – definitely improves the code.
Re: (Score:1)
I'll also note that rjbs didn't say what the subroutine's documentation says. Maybe it reads "Prepares a report and saves it in $file. Doesn't return a damn thing."
Re: (Score:1)
Euhm. How are you going to ban implicit returns without requiring
returnstatements?In any case, my point was that if an explicit
returnwould make the code self-documenting.Re: (Score:1)
Re: (Score:1)
Sure, h
rjbs
Re: (Score:2)
I have just finished a project for a client, I have disabled 8 policies and have documented as to why I have done so in the POD.
So disable the policy and document it
Re: (Score:1)
rjbs
/xsm (Score:2)
It might be good to write new regexes with
Pe
Re: (Score:1)
And then there's
rjbs
Re: (Score:1)
I think it's far easier anyway. I'm only one person.
Guilty! (Score:1)
So okay, I changed a bit of code that piped to sendmail (exim, actually).
But then, and here's where I ran into trouble, before I tested it I made some other cleanups, one of which involved deciding to replace the hardcoded sendmail with Email::Send.
And after some fiddling with Email::Send
Re: (Score:1)
rjbs