Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Reducing Perl OO boilerplate

by dragonchild (Archbishop)
on Feb 18, 2004 at 13:24 UTC ( [id://329904]=note: print w/replies, xml ) Need Help??


in reply to Reducing Perl OO boilerplate

To address the above discussion really quick - do HeckIfIKnow->new( ... ) and sidestep the whole problem. Larry himself has said that indirect method calling was a mistake.

To address the OP's question ... there are a few commonly-used classes (Class::MethodMaker and Class::Struct) that I would examine, if I were you. Personally, I found the exercise of creating my own OO-base class to be both fun and edifying.

As for what's wrong ... I can't say this with certainty, but it looks like you're using classes as a way to created HashesThatCanDoStuff. It's still primarily a data structure, just jazzed up. Now, that's fine and all, but it's not OO. OO is three things (q.v. http://c2.com/cgi/wiki?ObjectOriented for more info):

  1. Encapsulation (You can't poke inside the Foo)
  2. Inheritance (You can make a Bar that is-a Foo)
  3. Polymorphism (A method of Foo will do different things depending on the calling context)

While Perl's OO gives you 2 & 3 without you doing anything, HashesThatCanDoStuff don't have Encapsulation. (In fact, pure data-hiding is non-trivial in Perl, but that's another story.)

------
We are the carpenters and bricklayers of the Information Age.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Re: Reducing Perl OO boilerplate
by flyingmoose (Priest) on Feb 18, 2004 at 14:41 UTC

    You're right, it's not full blown OO -- and that is a fault with my shortened constructor. I realized this last night when I realized my shortened constructors had pretty much lost that loving feeling (i.e. probably made inheritance about useless). Any suggestions about how to make HashesThatCanDoStuff a little more flexible?

    Aside: I'm not really going for HashesThatCanDoStuff at all, I'm after named parameters, and the data structures would have been, most likely, used to implement Composites via named parameters. After all, a hash is a hash, but what you put in it makes it special :)

      What do you mean by "named parameters"? "Composites"? "named parameters", to me, are a way of passing information to a function. It doesn't have much to do with OO, specifically.

      I would look at the problem differently.

      • What system are you trying to model?
      • What are the things that do stuff in that system?
      • What is the minimal set of actions that each thing needs to be able to do?
      • How will those things interact?

      Once you know those things, then you can start working out what attributes each class needs to have in order to fulfill its requirements. Then, and only then, can you figure out where that information needs to come from. You may find out that most of it shouldn't come from the client at all!

      Now, to answer the question "How do I pass attribute/value pairs to a constructor?" ... your very short constructor is actually the canonical (if non-resilient) way of doing it. *shrugs* Personally, I'd never use that outside of golf, but that's just me. There's CPAN code you use every day that uses that (or similarly terse) constructs. Heck, CGI doesn't even use strict!

      ------
      We are the carpenters and bricklayers of the Information Age.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Ok, I'll make up an example to explain why I'd like named parameters...pardon syntax errors, this is all untested. There *will* be syntax errors. Also, please don't study my problem space too much, as I'm just coding up a random example out of my head. I'm not actually coding a Chess program -- it's a generic question. You should see, though, from this example that I am not going after "HashesThatDoStuff", exactly, but rather something else stylistically.

        Rationale for named parameters -- One of the things I often don't like about OO sometimes is that parameter order matters. When folks put more than a few methods in a constructor, it's rarely obvious which is which. So named parameters (as in, say, Python? Others?) allow the code to be a bit more readable.

        my $board = ChessBoard->new('start'); my $game = GameState->new( -white => ChessPlayer->new( -board => $board, -controller => Human->new( -name => 'Larry', -coffee => 1, ), ), -black => ChessPlayer->new( -board => $board, -controller => Computer->new( -level => 5, -ply => 3, -timecap => 60, -style => 'Nimzovitch', ), ), -board => $board, -rules => new ChessRules('suicide'), );

        In the above example, timecap, ply, and level are all numbers, but I can easily tell them apart by the names, without having to know the order of the parameters as required. If I get the order wrong, it won't matter.

        I'll admit the example above is contrived (I wouldn't normally construct my entire object model in one statement), but it shows what I'm getting at.

        My OP question was, essentially, what are better ways to implement the above goal in Perl? How can we do this without creating mammoth constructors, yet still support inheritance and so on? MethodMaker will go a ways to keeping from writing accessor boilerplate and variable declarations, but constructors...that's the hardest part to keep elegant.

        Thanks to everyone for the help and insight though. This is very good help for someone who is experienced in OO but still not quite getting it to come out elegantly in Perl. Appreciate it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-25 08:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found