in reply to new() function: incorrect?

Whether it's wrong or not depends on your viewpoint. I can identify at least three issues in your new methods I've seen people argueing against.

  1. People object to give the programmer liberty to create a new object of the same class as a given $obj by doing $new_obj = $obj->new, insisting the programmer must jump hoops and write $new_obj = (ref $obj)->new, because they themselves get confused, insisting that if an object method is called new it must return a clone.
  2. People object to integrating object construction with object initialization, arguing they are two different operations and should be into different function. There argument is that combining object construction with object integration makes it harder to do multiple inheritance. They argue it's better written as:
    package Bob; sub new {bless({}, shift)->init} sub init { my $self = shift; $self->{command} = undef; $self; } my $bob = Bob->new;
    That way, you can do MI more easily:
    package MyUncle; sub new {bless({}, shift)->init} sub init { my $self = shift; $self->{topping} = "dessert"; $self; } package BobIsMyUncle; our @ISA = ('Bob', 'MyUncle'); sub new {bless({}, shift)->init}; sub init { my $self = shift; $self->Bob::init->MyUncle::init; $self->{floor}='wax'; $self } my $bob_is_my_uncle = BobIsMyUncle->new;
  3. Some people say that using hashrefs as objects is wrong, as it leads to the possibility of name clashes of object attributes when inheriting (a subclass of Bob::MyObject may use 'command' itself as well). And that if you typo a hashkey name, you will not get a compile time error, just odd runtime behaviour. They prefer you use Class::InsideOut or some other module implementing classes for you.
I typically ask how people prefer to implement their objects as well during an interview. Unless they really screw up, there aren't wrong answers. What interests me is the motivation (I ask for that). I don't care whether they have the same preference as I have, as long as they can motivate them (and bonus points for identifying the weaknesses in their choices).

Replies are listed 'Best First'.
Re^2: new() function: incorrect?
by lukeyboy1 (Beadle) on Nov 12, 2008 at 14:06 UTC
    Their main point was "new() doesn't make the object -- it's not really OO programming, it's procedural"

    ..at which point, I said something along the lines of "I don't really agree with your thoughts on that", and left it at that.......

    (Honestly -- the most dangerous people are the ones that think that they DON'T need to ask questions of themselves, IMO -- what's the point, if you can't keep improving yourself?)

    To be honest, the panel of people also said that my commenting was "uninspiring" (but, what did they expect in a 45 minute interview, eh?!) -- but I like to declare an object like this (I'm not keen on the alternate ways of declaring a new object, but that's my own view!) :
    my $objectUsing = Bob::Object->new();

      "at which point, I said something along the lines of "I don't really agree with your thoughts on that""

      If your goal was to stand by your understanding then you certainly succeeded. However, if your goal was to get the job then you might have left a better impression by instead saying "Well I am always open to learning more correct ways of getting the job done." I am all for honesty but during the interview I am certainly aiming to please. If I disagree then I will reflect that by not accepting the offer. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re^2: new() function: incorrect?
by lukeyboy1 (Beadle) on Nov 12, 2008 at 14:22 UTC
    I take point 2 on board. I think that's where they got hung up.

    I mentioned that I was using the new() function as a sort of short-hand to set up what properties that I might use for a simple example.

    but -- in the real world -- you'd nearly always have a separate init() function (or similair). I know I would, in most cases.

    but -- using $proto clearly stuck in the chap's craw. And you're always fighting a losing battle in that case, aren't you....?
Re^2: new() function: incorrect?
by jeffa (Bishop) on Nov 12, 2008 at 19:08 UTC

    "That way, you can do MI more easily: "

    Friends don't let friends use multiple inheritance. ;) Down vote me all you want, i'll say it again and again: Friends don't let friends use multiple inheritance! I recommend looking into Roles and Traits if you want Bob to be yer uncle.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)