in reply to Re^15: Assignable Subroutines
in thread Assignable Subroutines

Both examples require more functionality in the code higher up. It's not OO due to insufficient encapsulation.
I don't understand what you mean by "higher up". The code is almost identical to yours, just written directly in Perl, rather than some new and cumbersome operator application language.

I also don't understand what you mean about "insufficient encapsulation". Why is that? If you mean that I shouldn't be calling $_->salary directly than you are arguing circularly as this whole discussion is about whether that's OK or not. If you don't mean that, please tell me what I should have encapsulated but didn't.

As for the "deny by default" strategy, I get it free with Perl already, why do I need to write all that code to get it back again? Plus, allow by default is really easy to implement in "normal" Perl too so it's not like it was urgently in need of simplification. Also, it's a pain now to ignore some but not others and it's still under the control of the called object, not the caller. This may suit a framework (where things kind of become inside-out) but it's not the general case.

Replies are listed 'Best First'.
Re^17: Assignable Subroutines
by hardburn (Abbot) on Jan 27, 2005 at 21:19 UTC

    I don't understand what you mean by "higher up".

    I mean the code to handle this is lifted outside the class and into the caller.

    The code is almost identical to yours, just written directly in Perl, rather than some new and cumbersome operator application language.

    I, for one, like the addition of small, domain specific languages. Your choice is to either learn an API or learn a (small) language. Choosing one or the other tends to force you twards different design decisions, but the cost of learning is not so different.

    I also don't understand what you mean about "insufficient encapsulation".

    Encapsulation is hiding data and behavior. Applying trvial mutators exposes data. It's therefore bad encapsulation. QED.

    But again, I think dragonchild's giveRaise() and givePromotion() methods are better than my own implementation.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      I don't understand what you mean by "higher up".
      I mean the code to handle this is lifted outside the class and into the caller.
      It's in the caller in your code too, it's just all stuffed into an effect object which is created and applied by the caller and I still haven't seen an example of why that might cause a problem.
      I, for one, like the addition of small, domain specific languages. Your choice is to either learn an API or learn a (small) language. Choosing one or the other tends to force you twards different design decisions, but the cost of learning is not so different.
      Yes they're great but I have 2 problems with your.
      • It's domain is already completely covered by Perl
      • Your language has to be typed in as a structure that represents a parsed syntax tree which is not so convient
      If you want to continue use it fine but I really recommend looking at Tangram, the techniques in there would allow you to replace
      my $effect = Effect->new( [ salary => 1.02, '*' ], [ frobnitz => 1.5, '+' ], ); $_->apply_effect( $effect ) for @employees;
      with
      my $effect = Effect->new; $effect->salary *= 1.02; $effect->frobnitz += 1.5; $_->apply_effect( $effect ) for @employees;
      which may not look so different but because it's actual Perl it requires no learning at all. It also would allow stuff like
      $effect->salary = $effect->frobnitz + 1.5;
      Encapsulation is hiding data and behavior. Applying trvial mutators exposes data. It's therefore bad encapsulation. QED.

      That's exactly the circular logic I was talking about. Your using your own definition of encapsulation to to justify itself. You've basically said "it's not my kind of encapsulation therefore it's bad encapsulation".

      For me, encapsulation does not mean hiding all data it just means hiding particular pieces of data from particular parts of the program. In many situations, some data really does need to be exposed but by exposing the data through methods you are not breaking encapsulation because the actual internal data is still guarded from direct manipulation.

      You keep talking about trivial mutators. Are non-trivial ones OK? If so what should I do if I change my implementation so that a non-trivial mutator is now trivial?

        It's in the caller in your code too, it's just all stuffed into an effect object . . .

        No, my code just provided the data for the Effect object to do its job. Your code put actual code (subroutine refs) to do the job. I want all of this information hidden away by the object.

        It's domain is already completely covered by Perl

        That gets down to a Turing completeness argument. Sure it's already covered. But I want to make a better one.

        Your language has to be typed in as a structure that represents a parsed syntax tree which is not so convient

        In your opinion. I rather like it.

        The example replacement ([ salary => 1.02, '*' ] with $effect->salary *= 1.02), is a matter of syntax. It's not a fundamentally different design.

        Your using your own definition of encapsulation to to justify itself.

        Hardly mine alone. From the OODBM Manifesto:

        We believe that proper encapsulation is obtained when only the operations are visible and the data and the implementation of the operations are hidden in the objects.

        This is the standard definition (with some variation) of encapsulation I've allways heard. OO provides one way of doing encapsulation (though it hardly has a monopoly on it).

        It's hardly circular. Encapsulation hides, trivial mutators expose. These are mutually exclusive concepts.

        You keep talking about trivial mutators. Are non-trivial ones OK? If so what should I do if I change my implementation so that a non-trivial mutator is now trivial?

        Any method that modifies the internal state of the object is a mutator. A trivial mutator passes the input to the method directly into the internal state. Doing verification on the value first is an improvement, but can often be replaced with a more rigid design (like giveRaise() and givePromotion()).

        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.