Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

At what level of structural complexity is Moose a win-win?

by dwm042 (Priest)
on Sep 29, 2010 at 14:39 UTC ( [id://862618]=perlquestion: print w/replies, xml ) Need Help??

dwm042 has asked for the wisdom of the Perl Monks concerning the following question:

I'm running into Moose through Catalyst, and liking what I see of Moose. I've worked in C++ before, with the endless getters and putters, the constructors from hell. For complex classes, the savings in time and clarity gained is worth the code layer.

But how complex does it have to be to be a win? Obviously I'm fishing for judgments here based on the experience of the Monks, as I have just my exposure over the past couple weeks of Catalyst hacking. For something as simple as a stack I'm not sure it's a win over

my @stack; push @stack, $foo; my $saved_value = pop @stack;

That said, in Catalyst, a Moose based model can be preloaded with stuff, from a config file, and it isn't that much harder to do things like

my $moose_stack = MyApp::MooseStack->new(); $moose_stack->add_item($foo ); if ( $moose_stack->has_item ) { $saved_value = $moose_stack->pop_item; }

My gut feeling though is, the bigger the collection of related data structures in a class, the bigger the potential win. And I'm curious how others feel about this issue.

David.

Replies are listed 'Best First'.
Re: At what level of structural complexity is Moose a win-win?
by chromatic (Archbishop) on Sep 29, 2010 at 15:05 UTC

    The question I ask is "Is this data structure clearer as an object?" For a simple stack, the answer is often no. For a value such as currency—even if the only attributes of the object are two integers to represent dollars and cents—the answer is often yes. Remember that associating a distinct identity with a data structure adds clarity and helps reduce bugs.

      Why would you want to store currency as two items, instead of storing it as an amount of cents alone?

      What makes more sense in this context is to store a monetary amount as the currency it is in, together with the amount and potentially the place of the decimal point.

        Sure, that's usually a better approach. I don't remember the details of why, but for one project it made sense to store dollars and cents separately. (Maybe it was because the retailer used the number of cents to indicate special information such as "manufacturer price" and "clearance price" and we didn't want to recalculate from decimal information every time.) Pre-breakfast, of course that was the first example to come to mind.

      Remember that associating a distinct identity with a data structure adds clarity and helps reduce bugs.

      Using an object:

      use Currency; my $amt = new Currency;

      And not:

      my $amt = [0,0];

      And two screens down in the code:

      my $oldAmt = $amt;

      But which file are you in?


      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.
Re: At what level of structural complexity is Moose a win-win?
by BrowserUk (Patriarch) on Sep 29, 2010 at 15:04 UTC
    and it isn't that much harder to do things like

    Why would you opt for something that is harder? Even if it is only "that much"?

    What benefits do you get that offset the extra complexity and verbosity?

    What would happen if you did:

    my $moose_stack = MyApp::MooseStack->new(); $moose_stack->add_item($foo ); my $saved_value = $moose_stack->pop_item;

    And there wasn't an item to pop?


    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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-04-19 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found