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

Re^4: MooseX obscure error and importance of Unit Testing (tools)

by tye (Cardinal)
on Aug 16, 2012 at 05:22 UTC ( #987697=note: print w/ replies, xml ) Need Help??


in reply to Re^3: MooseX obscure error and importance of Unit Testing
in thread MooseX obscure error and importance of Unit Testing

I prefer: 1) Don't use tools that make subroutines having the same names as classes. 2) Don't use tools (like old base.pm) that silently ignore failures when you try to require a module.

But since I find Moose so strongly pushes toward bad class design (and MooseX::Declare leads to obscure errors and days wasted debugging), not using them is no hardship for me anyway. :)

- tye        


Comment on Re^4: MooseX obscure error and importance of Unit Testing (tools)
Re^5: MooseX obscure error and importance of Unit Testing (tools)
by tobyink (Abbot) on Aug 16, 2012 at 07:11 UTC

    If you don't like silent failures, then you should be a big fan of ClassName::->new. With warnings enabled it will emit a compile-time warning if the ClassName package hasn't been loaded.

    Update: also according to my benchmarking (under Perl 5.16.0), ClassName::->new runs faster than ClassName->new. 'ClassName'->new is somewhere in between; ClassName->new is the slowest.

    use Benchmark; sub new { bless \@_, $_[0] } timethese(100_000, { bareword => sub { eval q{ main->new } }, quoted => sub { eval q{ 'main'->new } }, colons => sub { eval q{ main::->new } }, });

    Most of the penalty appears to be at compile-time, but the following illustrates that there's a small performance penalty at runtime too:

    use Benchmark; sub new { bless \@_, $_[0] } timethese(1_000_000, { bareword => sub { main->new }, quoted => sub { 'main'->new }, colons => sub { main::->new }, });

    So given that ClassName::->new is unambiguous, faster and can provide helpful warning messages when lexical warnings are enabled, what possible argument (other than force of habit) can there be for using ClassName->new? In cases where the warnings need to be suppressed (e.g. calling a class method on a class loaded at run-time), then 'ClassName'->new can be used, because that is also unambiguous, and also faster than ClassName->new.

    Update 2: further testing with the runtime-only tests seems to show that the quoted version may actually be fastest at run time. The bareword version is still slowest though. The version with the colons wins hands-down at compile-time. Either way, the speed difference is minor - the primary argument against the bareword version is its ambiguity.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      When I first saw ClassName::->new() proposed as a way to prevent ambiguous interpretation of ClassName->new(), it was shortly revealed that it didn't actually result in unambiguous parsing. It sounds like Perl's parsing may have been changed to improve that.

      I'd actually rather more improvements be done so ClassName->new() is unambiguous [or at least generates a warning when it isn't unambiguously 'ClassName'->new()]. I'd much prefer to have to actually write (the first "()" in) Function()->new() when that is what I want. That turned out to be the sane thing for both $hash{BareWord} and BareWord => $value, despite it taking years to realize that.

      For now, I'm not just yet going to run off and adopt the new fad considering the history of the many things in Perl that I've been told I should just stop using the old alternatives to, only to later find the new "must use" feature getting seriously down-graded, often out-right deprecated: v strings, pseudo hashes, base.pm, version.pm, Switch.pm, LVALUE subs, inside-out objects, source filters (actually, that last one became unpopular almost as soon as it became possible).

      Then there are the things that I've already realized serious problems with that the broader community may eventually clue in to: using inheritance for things like Exporter / AutoLoader / DynaLoader, 'use warnings' in modules or in test suites, given/when, 'smart' match (it smarts!), { ... redo ... }, inheritance, my $x = shift, objects as glorified hashes, Moose, MooseX::Declare (wait, I already said "source filters"). I'm sure I'm leaving a few out. :)

      But I'll keep it in mind. I appreciate the reminder of this potential pit-fall and the note about the compile-time warning.

      - tye        

Re^5: MooseX obscure error and importance of Unit Testing (tools)
by stonecolddevin (Vicar) on Sep 05, 2012 at 17:48 UTC

    What makes you feel Moose pushes strongly toward bad class design?

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

      Public accessors, for one.

      I disagree slightly with tye that Moose forces them on you, but any OO code which spends a lot of time setting (or Larry help us all, even getting) attribute values from outside the object is questionable.

      Good objects let you tell them what to do.

        I disagree slightly with tye that Moose forces [public accessors] on you

        I find it entertaining that I must parse that as "chromatic believes that Moose forces public accessors upon you", since I don't believe that Moose does that and chromatic claims to disagree with me on that point. :)

        As I previously noted:

        Now, I'd be more convinced by the standard "don't blame the tool" arguments if I had argued that one shouldn't use Moose because it fails to prevent you from doing stupid things.

        Moose doesn't force you to do stupid things. I haven't even come close enough to such a claim as to have claimed that Moose should be faulted for failing to prevent you from doing stupid things.

        It is easy to see that you are quite free to do nothing at all, despite choosing to use Moose:

        package Uninteresting; use Moose(); # Why did we do this?? 1;

        (Untested, however, as I don't plan on waiting for hour(s) while Moose and all of its dependencies try to install.)

        But much more to the point, it is quite possible to fight against Moose and avoid violating encapsulation in your class design by not making your implementation details about what attributes to store in each object manifest in the public interface to your class.

        But doing that requires eschewing so much of the functionality of Moose that it starkly raises the question asked in the prior code comment.

        package Silly; use Moose; has 'foo' => ( is => 'bare', # no accessors init_arg => undef, # not exposed via constructor ); ...

        stvn himself says that the only benefit of using Moose that way is:

        What this buys you over doing it by hand is [...] proper constructor initialization for any subclasses (via BUILD).

        But inheritance (in Perl) is just anti-modular so claiming some benefit based on supporting inheritance doesn't actually demonstrate a benefit when trying to do good OO design.

        So that leaves us with using Moose to automatically generate "private accessors". Since private accessors have underscores at the front of their names, they don't actually leak implementation details into the public interface. Well, I half buy that argument.

        But even generated private accessors encourage poor class design. When you've got a generated accessor, you end up wanting to add a small bit of functionality to one of the accessors. So you end up defining a "data type" for that attribute and/or you declare a 'before' or 'after' wrapper around the method.

        And this leads to: "Your logic for a single class will be split apart into tons of tiny pieces where the order and interactions will become almost impossible to see, predict, adjust, and debug. But your code will look pretty. Just don't try to understand it on any sort of deep level (such as when you need to fix something)."

        To which stvn noted: "Nothing about Moose forces you into doing something as idiotic as you describe."

        So, yes, you don't have to use generated private accessors with Moose since the only way to adjust/extend them is "idiotic" design.

        So, the bottom line is that using Moose but not in any of the ways that lead to non-modular or idiotic design also means that Moose offers no benefits. So there is no point using it that way.

        And having repeatedly had to deal with the pain of the non-modular design that takes great discipline to avoid slipping into once you establish a practice of using a tool that makes generating accessors easy or encourages the use of inheritance to partially re-use code (despite working with a group of very smart and capable Perl developers), I very much don't want to revisit that pain.

        And the pain of fragmented code from "idiotic" design from when you've established a practice of using a tool that encourages generated accessors that you customize with different types of wrappers ("datatype", "before", "after", etc.) was so acute that having experienced when that becomes painful only a few times was still enough to make me quite eager to avoid it.

        So, yes, I find using Moose is very likely to lead to painful situations caused by poor class design in the long run. But only if you actually use any of the features that Moose provides. So, indeed, "Nothing about Moose forces you into doing" this. (:

        - tye        

      What makes you feel Moose pushes strongly toward bad class design?

      :) tye say the documentation and implementation

      The documentation (esp examples) encourage designing classes in terms of attributes not in terms of interfaces (tye say good design is interface first, then internal details like attributes)

      The implementation , is based around attributes, so if you don't generate accessors then you can't make use of many "roles".

      see How Large Does Your Project Have To Be to Justify Using Moose?, read tye say this

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-11-28 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (195 votes), past polls