Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
There is also a school of thought (in which I'm enrolled {grin}) that an object should also be a "valid" object upon instantiation, and that a caller should never be given a partially constructed object that has to be prodded a bit before it would be valid for the rest of the program.

Since the point of an object is to hold varying internal state, creating an object with "new" that has no internal state almost certainly means that the object is not yet "initialized" and thus does not yet represent a valid object. (I can think of a few rare exceptions, but those are exactly that, rare.)

So, this "simple constructor" is nothing more than an interesting sequence of code, but in practical use, practically worthless.

In perlboot, I even argue that constructors shouldn't simply be called "new", since almost any real constructor will require some sort of state that cannot be determined without the use of parameters, and naming the method to match the intent of the parameters makes it somewhat clearer. As a practical famous example, note the constructor for DBI is called connect and not new. There's no way to create a "new DBI object". Only a connected one, one that has purpose and internal state and can be used with other calls. This is a very good model to follow.

-- Randal L. Schwartz, Perl hacker


In reply to Re: simple OO constructor by merlyn
in thread simple OO constructor by galacticdruid

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 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? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-12-04 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (30 votes). Check out past polls.

    Notices?