http://qs321.pair.com?node_id=114115


in reply to simple OO constructor

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