http://qs1969.pair.com?node_id=321178


in reply to Class automators should be standard

One thing I find troubling about MethodMaker is often my class setter/getters change object state beyond that attribute. There's probably a name for this, but I don't know it.

For example, suppose you have a car object, with attributes of  accelerator_setting,  gas_in_tank,  velocity, etc.

You can get or set the accelerator, but doing so should change the velocity and available gas.

If you use MethodMaker to automagically make getters and setters, the resulting get/set methods are bare-bones basic. And if bare-bones basic isn't what you want, so you need to subclass or rewrite these methods (so that, in this case, pressing the accelerator moves the accelerator and also increases speed and decreases gas), then why use C:MM to create them?

(And imho I don't the appearance of a class which uses C:MM for some getter/setter methods but hand-rolls others... just doesn't fit my code aesthetic.)

Perhaps I am missing something, but that's my problem with automatic method makers. The OO examples in the docs always use independent attributes like name, rank, serial number -- and promoting a solider doesn't change his or her name or number.

The easy stuff is great for C:MM, but not all objects are so simple.

rkg

Replies are listed 'Best First'.
Re: Re: Class automators should be standard
by danb (Friar) on Jan 14, 2004 at 18:43 UTC

    (And imho I don't the appearance of a class which uses C:MM for some getter/setter methods but hand-rolls others... just doesn't fit my code aesthetic.)

    It fits my code aesthetic; in fact, I think it is very attractive. But even if I didn't, I would continue to mix C:MM and hand-rolled code because of this lazy principle:

    If it is simple and/or repetitive, the computer should do it.

    Do you think that the principle itself is ugly, or just the "C:MM with hand-rolled code" implementation of the principle?

    I think the following is a great example of C:MM, due to my liberal application of the hubris virtue. ;-)

    use Business::Shipping::CustomMethodMaker new_with_init => 'new', new_hash_init => 'hash_init', boolean => [ 'update', 'download', 'unzip', 'convert', 'is_from_west_coast', ], hash => [ 'zone' ], grouped_fields_inherit => [ optional => [ 'zone_file', 'zone_name', ], required => [ 'from_state', ], ];

    The above would take a lot of coding to represent manually. You can see simple methods, and slightly-less-simple methods (like the required() and optional() methods, which automatically call parent methods and concatenate the results to their own). (Note that the above is using a sub-classed version of C:MM, taken from the Business::Shipping project.)

    While I'm on the subject, I think it would be great if Java had something like C:MM, but the closest you can get is those code-generators that run just before compile.

    As far as the topic's original question, I wouldn't mind if all or none of the OO modules were in the core. Newbies can continue to find the ones they like by reading the OO docs. (Damian Conway's book was my initiation to the Perl OO world.) Which makes me wonder, is there an "OO Perl Module Survey" doc somewhere that is updated when new OO modules come out?

    -Dan

      Ok, I'l agree with your point about making the computer do the boring repetitive stuff.

      But back to my question:

      How do you handle getter/setters when the attributes interact? In your shipping example, what would keep me from (assuming I understand the methods correctly) setting  from_state to Massachusetts and is_from_west_coast to TRUE? Shouldn't setting the state cause the west coast flag to flip on or off as appropriate?

      A handrolled get/set could enforce all these things; doesn't C:MM fall short here?

      I am not C:MM bashing; I use it and am a fan; I just haven't figured out how to handle interrelated attribs yet.

      Thanks for your comments

      rkg

        Your point is a good one. It is necessary to hand-roll methods that do anything non-simple, like keeping is_from_west_coast() in the correct state.

        Because of that, C:MM can only be used for simple methods. In my case, that means C:MM methods are usually private methods, and public methods are hand-rolled.

        I am looking forward to automatic method constructors getting smarter in the future. I could imagine a module that combines something like Params::Validate with C:MM, like so:

        use Class::MethodMaker optional => [ 'zone_file' => [ 'no spaces', 'alpha only', 'uppercase', 'error on parameter failure' ] ];

        -Dan