Xref: utzoo comp.object:1457 comp.lang.eiffel:1003 Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!bellcore!roland!sjs From: sjs@roland.ctt.bellcore.com (Stan Switzer) Newsgroups: comp.object,comp.lang.eiffel Subject: Re: Should a constructor do "work"? Message-ID: <25303@bellcore.bellcore.com> Date: 13 Jul 90 13:38:44 GMT References: <55236@microsoft.UUCP> <519@tetrauk.UUCP> <364@eiffel.UUCP> Sender: news@bellcore.bellcore.com Reply-To: sjs@bellcore.com (Stan Switzer) Organization: Bellcore Lines: 21 The original question is whether it is reasonable for a constructor to do a great deal of work. People have answered (correctly) that it must at least do enough work to guarantee that its class invariant is satisfied. Sometimes, though, that can be too expensive and a "lazy evaluation" approach is in order. Suppose that the original class invariant is "C". Let the new invariant be "!initialized || C." All the constructor has to do is set "initialized" to "false" and squirrel away any initialization parameters. The price of this is, of course, an extra instance variable (or so) and that every method has to begin with "if ( !initialized ) initialize();" It's not appropriate for every situation, but it can be a quite useful technique when used wisely. This is actually a special case of the polar/cartesian representation problem discussed under "Legitimate side-effects: and example" in Meyer's *Object-oriented Software Construction*; in this case one of our representations is just the raw initialization parameters. Stan Switzer sjs@bellcore.com