Object Programing and Objective C

Al Globus al at aurora.UUCP
Fri Oct 4 10:53:44 AEST 1985


My earlier posting regarding Objective C elicited a number of responses,
many of whom wished to know more.  The following is much of my
knowledge and experience with object programming and Objective C.  For
the purpose of this piece, object programming refers to a programming
methodology limited to a single process, on a single processor, within a
single address space.  No parallelism is implied by the term message (see
below).  There are other meanings for object programming, but they are not
addressed here.

Objective C was developed and is marketed by PPI, 27 Glen Road, Sandy
Hook, CT 06482 (203) 426-1875.

The primary advantages of object programming and Objective C are
improved modularity, reduced code bulk (lines of code), and a better
mapping between many problems and the software.

I was originally attracted to object programming because it could have
solved some of the problems I ran into developing a dynamic display editor
(completed in C a year and a half ago).  Specifically, I think that using
Objective C I could have built the editor in 4-5,000 lines of code rather
than the 20,000 or so it actually took.  The result would have had better
modularity and taken perhaps 35% less time to complete.  Powerful
incentives.

I have used Objective C to implement a prototype of a simulation.  This
simulates the activities of the crew on-board NASA's space station which
is to be launched in the early 1990's.  The prototype went well and we are
now a few weeks away from finishing the first beta test version.  This
work has also gone well, somewhat better than expected actually.  All told
I have written about 1500 lines of Objective C.

Objective C is a preprocessor for the C language.  It adds SMALLTALK-like
messaging, objects, classes, and inheritance to C.  The preprocessor runs
after the C preprocessor and outputs C source code.  Normal C code is
passed through unchanged, so you keep all of C.   Essentially, Objective C
adds syntax to declare classes and messages; and a new expression,
messaging.  The messaging expression has all of the properties of any C
expression, i.e., it evaluates to a value and can be used anywhere a normal
C expression can be used.

Object programing software consist of objects that exchange messages. 
Think of objects as synthetic machines or people that remember
information, do particular things, and interact by sending each other
messages.  Since the real world consists of objects that interact, object
programs frequently reflect the problem domain better than convential
techniques (i.e., operators that work on operands).

Objects contain both code and data.  The data is private and can (normally)
only be accessed directly by the object's code (as usual in C, there are
ways to get around this).  Every object is a member of a class.  Each class
consists of one FACTORY object and zero or more INSTANCE objects.  The
factory object is usually used only to create instance objects.  Every
instance object in a single class has the same data structure, although
values may vary, and the same code.  This code is called the object's
METHODs.  Each method corresponds to a message that the object
understands.  Programming in Objective C is mostly a matter of writing
classes.  Each class goes in a separate file.

In summary, objects are chunks of code and data with a single pointer to
the whole mess, rather than a pointer to the data and a pointer to each
function that works on the data as in conventional data hiding approaches
in C.  This improves modularity at a very fundimental level.

Messages are somewhat equivalent to function calls.  Messages cause a
piece of code to be executed, can have arguments, and return a value.  The
difference is that the code executed and the data worked on depends on the
object the messages is sent to.  

Messages give several advantages.  One is that you can add new sorts of
things (classes of objects) to a system without modifing the code that
tweeks these objects, causing them to save to disk for instance.  Another
is that the name space of messages is partitioned between classes so
name collisions are infrequent; thus the practice of prepending names with
characters to identify different parts of a large system becomes
unnecessary and names like IPDJMOVE disappear.  Also, when you change
graphic libraries you don't get the name collisions that have cost me
several days of frustrating debuging.  Finally, the giant switch statements
common to programs that must deal with many different kinds of data
disappear.  These switch statements usually switch on a field that tells
the type of the data and the code (semantics) for each type goes into the
case portion.  With Objective C the semantics go into each separate class. 
This gives better modularity and reduces code bulk a bit.

The last major property of object programming is inheritance.  Classes are
arranged into a heirarchy (a tree) with the Object class at the top (root). 
Each sub-class inherits all of the data structures (INSTANCE VARIABLES)
and methods of its superclass.  In general, sub-classes are specializations
of their super-class, with the Object class being the most general of all. 
Inheritance saves large amounts of code since sub-classes need only add
the new methods and data they need and over-ride any methods that are
inappropriate.  In addition, new data and/or methods can be given to all
objects in a system simply by modifying the Object class.  Inheritance,
then, can reduce code bulk substantially.

Reduction in code bulk can save enormous amounts of money in large
systems.  According to Putnam, a software metrician, life cycle software
cost is proportional to the cube of code bulk.  Since theoretical
consideration and some empirical evidence suggest that object
programming can reduce code bulk by a factor of 2 to 10, in principle cost
should be reduced by a factor of 8 to 1000.  I don't know if the savings are
that large, but they may be substantial.

Objective C has got some additional nice stuff for the working
programmer. 

First, there are two messages defined by the Object class, storeOn: and
readFrom:. storeOn: will save any instance object and all the objects it
references recursively to disk in a readable ascii file.  readFrom: will read
a file created by storeOn: and recreate the same graph of objects.  I.e.,
with one line of code you can save or retrieve ANY data structure created
out of objects - or send it around the world on the net!  

Second, all messages go through the messaging function.  There is a global
flag, msgFlag, that, when set, will print out a trace of all messages.  This
is incredibly useful for debugging.  When you get bus error - core dumped,
just run the program again with msgFlag set (I set it from the command
line) and the trace will tell you right where the crash occured. 

Third, if an object receives a message it can't understand you get an error
message and a stack trace leading you directly to the problem.

Lastly, PPI provides a number of classes with the compiler.  I have found
these to be extremely useful.

Objective C isn't perfect of course.  Here are the main disadvantages I've
found:

Too expensive.  I wish they'd cut the cost and sell thousands of copies.  It
may be worth the current price, but that doesn't mean enough people will
buy it.

Its got a few bugs.  None of them have hurt too badly but the system is not
as mature as it could be.  PPI's support has been excellent and I think this
problem will eventually be ironed out.

Compile time is long.  Objective C does a full parse of C and then spits out
a complete C program which must be parsed by the local compiler.  PPI is
working on an interpreter call VICI that may solve this problem.

Classes are divided into groups called phyla.  I still don't understand phyla
and have had problems as a result.  Phlya are used to optimize message
passing and are a pain in the ....

Object programming is not appropriate for all problems.  It has been used
for compilers, operating systems, simulations, and user interfaces at
least.  I'm not sure exactly what the boundaries of usefulness are.

Its not available on the MacIntosh.


Speaking of availablility, Objective C is available on a lot of UNIX systems
and the IBM PC.  PPI is working on MacIntosh and VMS versions.

The bottom line: I love it.  I hope I never have to go back to functions and
data structures, objects are for me.  I also convinced NASA (I'm a
contractor) to spend almost $20,000 on object programming software, and
we're real short of money these days.



More information about the Comp.lang.c mailing list