Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!news.kei.com!ub!csn!stortek!k1!alec
From: alec@k1.stortek.com (Alec Sharp)
Subject: Eliminating procedural code
Message-ID: <DAA0E1.8un@stortek.com>
Sender: news@stortek.com
Organization: Storage Technology Corporation
X-Newsreader: TIN [version 1.2 PL2]
Date: Fri, 16 Jun 1995 17:25:13 GMT
Lines: 89

Question: What alternative ways have you seen or used to eliminate
procedural code?


In our product, we have various objects that sit in a loop,
waiting on shared queues. Based on what they get from the
shared queue, they do different things.

For example, in a procedural scheme we might have in class MyClass:

myDoLoop
    [object := self myQueue next.
    self myProcessObject: object] repeat

myProcessObject: anObject
    anObject isSuccessResponse ifTrue: [^self processSuccess: anObject].
    anObject isFailureResponse ifTrue: [^self processFailure: anObject].
    anObject isHeartbeat ifTrue: [^self processHeartbeat: anObject].
    self processUnknown: anObject



In our product, to get rid of the procedural nature, we tell the object
to tell us to process it. For example, in MyClass:

myProcessObject: anObject
   anObject processYourselfVia: self


Then in the object, we see

SuccessObject>>processYourselfVia: anObject
    anObject processSuccess: self

FailureObject>>processYourselfVia: anObject
    anObject processFailure: self

HeartbeatObject>>processYourselfVia: anObject
    anObject processHeartbeat: self


Back in MyClass, we implement

processHeartbeat: anObject
    do stuff

processSuccess: anObject
    do stuff

processFailure: anObject
    doStuff


A problem with this scheme is that when additional objects are added
to the product and can appear in one of these shared queues, we have
to make sure that we add a processXXXX: method to MyClass. (Although
any problem should appear pretty soon if any testing is done.)

An alternative way is to have MyClass define:

myProcessObject: anObject
    selector := anObject processingMethod.
    (self respondsTo: selector)
	ifTrue: [self perform: selector with: anObject]
	ifFalse: [self doSomeErrorStuff]


Then the objects themselves would have something like:

SuccessObject>>processingMethod
    ^#processSuccess:

FailureObject>>processingMethod
    ^#processFailure:

HeartbeatObject>>processingMethod
    ^#processHeartbeat:


Question: What other ways have you seen or used to eliminate
procedural code?


Alec

--
alec_sharp@stortek.com		Storage Tek, 2270 S. 88th Street
(303)-673-2955			Louisville, CO 80028-4286
self standardDisclaimer
