Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Use modules or roll your own?

by ichimunki (Priest)
on Jul 29, 2002 at 15:27 UTC ( #186009=note: print w/ replies, xml ) Need Help??


in reply to Re: Use modules or roll your own?
in thread Use modules or roll your own?

The problem there isn't a "pointless use of OO", but rather an API that isn't very sensible. One is tempted to ask why the module has an interface that forces a user to create an instance, give the instance a number, and then retrieve a string from the instance when a simple class method exported would have worked more easily. I mean, then I have to remember whether the current number in the instance is the one I want-- which is the sort of "internal state of object" knowledge the rest of my program shouldn't have to be thinking about. It would bother me a lot less if the module had fewer steps:

use Lingua::EN::Numbers; my $n = Lingua::EN::Numbers->new; print $n->get_string(1281), " fish in the sea.\n"; #or use Lingua::EN::Numbers qw(get_string); print get_string(1281), " fish in the sea.\n";
With this sort of interface I have a choice of whether or not to pollute my namespace with a function name (like with CGI.pm, I prefer not to attempt to export all those HTML markup tags as function names).


Comment on Re: Re: Use modules or roll your own?
Download Code
Re: Use modules or roll your own?
by Abigail-II (Bishop) on Jul 29, 2002 at 15:33 UTC
    One is tempted to ask why the module has an interface that forces a user to create an instance, give the instance a number, and then retrieve a string from the instance when a simple class method exported would have worked more easily. I mean, then I have to remember whether the current number in the instance is the one I want-- which is the sort of "internal state of object" knowledge the rest of my program shouldn't have to be thinking about.

    This is exactly what I mean by "pointless OO". Even your suggested class method is "pointless OO" to me. It doesn't buy you anything, except additional clutter.

    Abigail

      For a module with one essential function, parsing a number into an English string, it does seem excessive. But what if my program already has a get_string() function or an %N hash, and I add this module after the fact? The OO layer protects me from name collisions (and in my example is completely optional). While I might not worry too much about it for Lingua::EN::Numbers, I certainly would worry if CGI didn't offer me a choice-- that module has the potential to export several dozen function names, many of which (in a CGI script) might clash with my function names (poorly chosen names, perhaps).

      The OO isn't really pointless. It's happening either way-- exporting symbols without being asked to do so is just a sneaky way of concealing it is all. What I've said is that it should be explicit and optional for a module where the instance is really just an instantiation of a machine, rather than an instance of a class with unique properties that would distinguish it from other instances in the same class.

      Essentially the instance becomes a shorter way of doing print Lingua::EN::Numbers->get_string(1281), " fish in the seas\n";... which you can also get around by offering the ability to export get_string(). Is there an actual reason for avoiding instantiation of a machine to prevent namespace collision? Or is the objection purely aesthetic?

        But what if my program already has a get_string() function or an %N hash, and I add this module after the fact?
        Well, so what? You _do_ know the working of Perl, don't you? Then you _do_ know that if you do:
        use Module ();
        the import() routine isn't called, and nothing will be exported. In which case, you can still use the function by calling it like:
        Module::get_string ()
        But a name clash is an exceptional case - normally you don't have a name clash (I've been programming Perl for over 6 years now, I cannot recall ever having had a name clash problem), and you can just use the short get_string. But with a class method, you have to use Module -> sub, even if it's not necessary.
        The OO isn't really pointless. It's happening either way-- exporting symbols without being asked to do so is just a sneaky way of concealing it is all.
        Exporting symbols without being asked is only a sneaky way if there isn't an easy way to avoid it. All you need to do is ask to _not_ import anything. That takes a whopping two characters. A module author using Exporter gives the programmer a choice - a module author using OO to avoid name clashes doesn't. I know who acts Perlish, and it ain't the latter programmer.

        As for the %N of Lingua::EN::Numbers::Easy, let me quote from the manual:

               By default, "Lingua::EN::Numbers::Easy" exports a hash
               "%N" to the importing package. Also, by default, "Lin-
               gua::EN::Numbers::Easy" uses the British mode of "Lin-
               gua::EN::Numbers". Both defaults can be changed by
               optional arguments to the "use Lingua::EN::Numbers::Easy;"
               statement.
           
               The first argument determines the parsing mode of "Lin-
               gua::EN::Numbers".  Currently, "Lingua::EN::Numbers" sup-
               ports British and American.  The second argument deter-
               mines the name of the hash in the importing package.
        
                   use Lingua::EN::Numbers::Easy qw /American %nums/;
            
               would use American parsing mode, and "%nums" as the tied
               hash.
        
        If %N gives you a name clash, just use any other suitable name. OO isn't the only way of solving name clash problems!

        Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2015-07-06 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls