Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I find really light weight OO cleans up globals something wonderful. Consider:

use strict; use warnings; my $obj = bless {global1 => 0, global2 => 3}; $obj->setThree (6); $obj->doSomething (); print $obj->{four}; sub setThree { my ($self, $three) = @_; $self->{three} = $three; } sub doSomething { my ($self) = @_; $self->{four} = $self->{three} + $self->{global2}; }

Prints:

9

So the cost is a bless to create $obj then calling "methods" on $obj rather than directly calling the subs and having to access stuff through $self inside the subs.

The real question is, does it buy you anything? Well, for the first cut when there were only a couple of variables and the whole thing was only a few dozen lines long, nope, it buys you nothing at all.

For the second cut where you are adding a few more globals, you don't have to scroll to the top of the file to add globals - not really a win yet. But at least it's likely the subs dealing with the new "globals" are close together and the documentation describing them can be close by too, so there is some real benefit.

But with the third round where you could really do with refactoring the code and generate a real class, guess what - you've already done most of the work!

For anything that is likely to grow I find using light weight OO from the start makes it easier to evolve the code over time. The payback isn't on day one, but by a modest chunk into the project the scale tips and the up front work becomes worth while.


Perl is environmentally friendly - it saves trees

In reply to Re: What are the core points of good procedural software design? (functions, code structuring) by GrandFather
in thread What are the core points of good procedural software design? (functions, code structuring) by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found