Instance First Development

From Oliver Steele's article, Classes and Prototypes, at

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:

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

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

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

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.


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