From: janzen@lichen.mpr.ca (Martin Janzen)
Subject: Re: C/C++ Correctness (was: Re: C/C++ Speed)
Reply-To: janzen@mprgate.mpr.ca
Organization: MPR Teltech Ltd.
Date: Wed, 13 Jan 93 02:24:55 GMT

In article <1993Jan11.224238.22152@csservices.Princeton.EDU>,
blume@kastle.Princeton.EDU (Matthias Blume) writes:
>The question should be: are programs written in C++ more reliable than those
>written in C.  I serioulsy doubt that!

I beg to differ with this very general statement.  Obviously not every C++
program is more reliable than every C program.  But my first "real" C++
experience (on a recent UI project: ~50 classes, ~14K lines, ~3800 stmts)
has been that reliability _can_ be improved by a number of C++ features,
if you choose to take advantage of them.

The things that I've found contribute most to increased reliability are:

- the ability to encapsulate all of the code related to a particular
  interface into one class.  This results in code which is easier to
  read, and thus to review, test, debug, and maintain.

- the use of constructors and destructors.  These make it _much_ easier
  to avoid memory allocation bugs, such as memory leaks and duplicate
  calls to free().  (Though I'm not throwing out my "Purify" yet!)

- the ease with which Bertrand Meyer's notion of "programming by contract"
  may be implemented.  Pre- and post-conditions may be checked by adding
  assertions to the beginning and end of the appropriate methods.  (I
  suppose that class invariants could be implemented as well; say, by
  using a macro which is inserted before each exit from each method -- I
  didn't go quite that far!)

Of course none of the above are exclusive to C++; you can accomplish the
same things in many other languages.  But in my experience, C++ provided
reasonably "natural" ways to do all of the above.


>I'd rather stick to something simpler and more reliable than that.  If you
>need inheritance et al. then use something that isn't suffering feature
>cancer (for instance: Objective C)!

There are a number of languages that may be "better" in one way or
another.  For instance, the new version of Eiffel sounds really good.
But so far, I'd say that C++ still has an advantage based on other
factors -- availability and tool support, to name just a couple.

>With a bit of discipline it is even
>possible to write OO-programs in plain old C (Many people have done this as
>an exercise, and literature about it will probably (hopefully) appear soon.)

Sure; you could even write OO programs in assembler, if you really want to.
But try working with the X Toolkit or with OSF/Motif code for a while!  It's
object-oriented C code all right, but it's pretty scary stuff...

Regarding some of Matthias' other criticisms:

>	C++
>		- is highly complicated

No argument about that!

>		- has no fixed specification (or rather: the specification is
>		  a moving target)
>		- employs a syntax that is even more ambiguous than that of C
>		- has NO WORKING compiler that really implements the language
>		  (all compilers show major shortcomings in basic areas like
>		  constructor/destructor call placement)

Maybe I've just been lucky, but these haven't presented major problems.

>		- is overfeature[d] (who needs multiple inheritance?)

I do.  Not often; but it's useful for, say, mixin classes which
implement the Model-View-Controller architecture...

>The sad truth is, however, that almost everybody seems to believe in C++
>nowadays... :(  C++ will not be the solution to the current software crisis --
>it will be the cause of the next one.

A bold statement! :-)   I guess we'll find out...


-- 
Martin Janzen                     janzen@mprgate.mpr.ca (134.87.131.13)
MPR Teltech Ltd.                  Phone: (604) 293-5309
8999 Nelson Way                   Fax: (604) 293-6100
Burnaby, BC, CANADA  V5A 4B5