To new or not to new

Boris.Gaertner Boris.Gaertner at gmx.net
Fri Sep 28 07:27:01 UTC 2007


 Alejandro Martínez  wrote:

  Hello list. I'm searching for the opinion of experienced developers:

  In which case do you create specialized class message for instantiating an object?

  I think two factors are fundamental:
  1) You want to enforce correcteness of your objects by making sure they are complete, so you "minimize" #new and provide specialized messages for creation. 
  2) You do not want to enforce anything until real usage experience shows you which messages you must "promote" to the class side.

Point 1 is indeed fundamental: Specialized instance creation methods are
used to create fully initialized instances. Beautiful examples of that
approach can be found mainly in the old parts of Squeak. Have a
look e.g. at
   PluggableListView
   PluggableButtonView
   PluggableTextView
   PopUpMenu
   SelectionMenu
or at classes Browser  and Inspector.
PluggableListView has one general class method that creates
a fully initialized instance. That method is also sent by two
simpler class methods that provide reasonable default values
for unneeded features of an instance.
In PopUpMenu a general class method is #labelArray:lines:
its simplified variant is #labelArray:
All the classes that I just mentioned provide both general
and specialized class methods for instance creation.
That approach is, in my opinion, an example of careful
class design that should be followed. I agree however
that some additional work is needed to find out required
initial values and suitable default values for features
that are not always needed.

Additional remark: In the absence of other documentation
a set of specialized instance creation method helps you to
understand how you should use a class. At times this can
be very helpful.

Second additional remark:

 myClass new
   initVariable1: <anExpression1>;
   initVariable2: <anExpression2>;
   initVariable3: <anExpression3>.

is of course possible, but it does not surpass the
quality of code that is generated by simple tools.
As we are not code generators we should perhaps
try to do better.  :-)

Greetings,
Boris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20070928/1f12a9ad/attachment.htm


More information about the Squeak-dev mailing list