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

comment on

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

I am going to comment on this from a different direction, namely *why* object-oriented programming. Understanding this makes a lot of things easier to understand.

There are basically two kinds of bugs in software: those that involve doing the wrong thing with the right data, and those that involve doing the right thing with the wrong data. Of the two, the second is far harder to track down and fix because the data bacame wrong somewhere else and that has to be tracked down. For this reason state management is a central problem in programming.

Looking at your code you seem close to structural programming. The idea of structural programming is you keep data together that belongs together and this makes it easier to determine when data is bad. With your hashes, this is what you are doing. It also simplifies assumptions your code has to make.

The next step is to go object oriented and tie the data to behavior. Ideally state should only be changed by going through the behavior. This gives you fewer points to track down when looking for how data became bad. In Perl you use packages and bless to do this, but you can also use Moose which is a really nice framework.

One reason to recommend Moose btw is that it makes the next step (after going OO) a lot easier, namely making your objects immutable. With immutable objects you have only one place to check data validity (the constructor) and so it becomes nearly possible to eliminate this whole class of bugs.

This latter point gets you closer to functional programming because if your output is dependent only on your input (i.e. instead of changing an object you return a new one), you can manage state well enough to have programs which safely write programs.


In reply to Re: How do I go from procedural to object oriented programming? by einhverfr
in thread How do I go from procedural to object oriented programming? by Lady_Aleena

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 contemplating the Monastery: (1)
As of 2024-04-24 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found