in reply to Correct idiom for default parameters

It has been said before that this code is "too simple" for CPAN, but I must disagree. I have seen people write this kind of code over and over again and they always get it wrong. Perhaps now they will spend more time getting the rest of their code right...
-- Leon Brocard in the notes of Data::Page

All the errors, mistakes, and cryptic golfing in this thread, along with the fact that you will need to do this more than once imply you need a modular, re-usable solution.

The software engineering approach is to use Params::Validate or a newer related module.



The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.

-- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"

Replies are listed 'Best First'.
Re: DWIM code would use Params::Validate (overengineered)
by Corion (Patriarch) on Apr 29, 2010 at 17:29 UTC

    You seem to be a big fan of software (over)engineering. As I've only seen overengineered solutions, maybe you can show an example where the code using Params::Validate is shorter and/or more concise or even more readable than the solutions already presented. The synopsis of Params::Validate only shows verbose "solutions" spanning at least five or six lines.

        I'm not sure what problem P::V solves, so I'm not sure how it is applicable here. You also couldn't show how it is applicable here, so maybe we should wait for somebody to show how it is applicable here.

Re: DWIM code would use Params::Validate
by BrowserUk (Patriarch) on Apr 29, 2010 at 18:42 UTC

    Here's one reason not to use Params::Validate.

    The code (minus the inline stuff), in Re: Inline Subs Revisited takes just over 4 1/2 minutes to run.

    Modify those subs to validate their parameters using P:V like so:

    sub max { validate_pos( @_, { TYPE => ARRAYREF } ); my ($list) = @_; my $max; for (@$list) { $max = $_ if ! defined $max || $_ > $max; } return $max; }

    And it takes almost 22 minutes.

    And achieves nothing that couldn't be done with die unless ref( $list ) eq 'ARRAY';. (That costs nothing measurable.)

    Its like trying get better fuel economy by adding a large, copper-wound electric motor and 50Kg of batteries to your car.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      You can disable validation for production to avoid the hit, set VALIDATION to something true

        And what happens to defaults if you disable validation?

    A reply falls below the community's threshold of quality. You may see it by logging in.