Instance First Development

From Oliver Steele's article, Classes and Prototypes, at http://osteele.com/archives/2004/03/classes-and-prototypes

In instance-first development, one implements functionality for a single instance, and then refactors the instance into a class that supports multiple instances.

The InstanceSubstitutionPrincipal? says that an instance of a class can be replaced by the definition of the instance, without changing the program semantics.

This OpenLaszlo program declares an instance with a method named "f" that returns 100:

 <canvas>
   <view id="myview">
     <method name="f">return 100;</method>
   </view>
 </canvas>

This OpenLaszlo program declares an equivalent reusable class, and creates an instance of it:

 <canvas>
   <class name="myclass">
     <method name="f">return 100</method>  
   </class>
   <myclass id="myview"/>
 </canvas>

Many PrototypeBasedProgramming languages don’t obey the instance substitution principle, either because they don’t have classes, or because class and instance definitions are not parallel. (Typically there’s not a declarative means for defining an instance member.) JavaScript versions 1.0 through 1.5 (the versions in browsers) is also a prototype-based language, but lacks classes as a first-class syntactic entity, and lacks the hierarchical syntax that Java, C++, and OpenLaszlo use to define class members. JavaScript 2.0, JScript.NET, and Python have a class definition syntax, but don’t use the same syntax to define instance members. For example, contrast the following two Python programs, which parallel the OpenLaszlo programs above.

 myobject = object()

myobject.f = lambda f: 100

class MyClass(object):

def f(): return 100

myobject = MyClass()

The syntactic version of the instance substitution principle makes a class look like a function or a macro. Class, function, and macro definitions are all mechanisms for abstracting program structure so that it can be reused.


CategoryLanguageFeature


EditText of this page (last edited March 7, 2006) or FindPage with title or text search