Is this a true statement: Part III

Steve Horne sh at
Thu Jul 5 19:24:18 CEST 2001

On 3 Jul 2001 07:57:37 -0700, aahz at (Aahz Maruch) wrote:

>I managed to do the rewrite in a week, but only by sticking to straight
>C code.  The rewrite was cleaner, simpler, and faster.

The thing is, C++ isn't meant to make initial development easier,
except in cases where re-use is an issue. It's meant to make
maintenance easier.

Any language suffers when bad programmers use it. The biggest
reliability problems in C++ are...

1.  Using old C libraries, when the new C++ libraries do a lot more
    checking and have fewer awkard problems to deal with. A C++
    string, for instance, will not choke on NULL characters and
    will not fail when real strings are longer than anticipated.

2.  Overuse of C features such as pointers, when standard
    container templates would be more appropriate - or function
    pointer callbacks and awkward methods of finding their context
    data when classes with virtual functions would be more

3.  The age-old inherited problems with pointers

4.  Failing to use exception-safe patterns.

In short, C++ is for large systems that are going to be around for
years. There is a tradeoff in favour of greater reliability and better
maintainability - provided you really write C++ and not some
half-baked hybrid - but the cost is more time spent planning - where
are classes appropriate and where not, where is subclassing
appropriate and where not, which parts of the code are most likely to
suffer from changing requirements, etc etc etc.

C (or a C-like subset of C++) is more appropriate for smaller systems
with no more than a few thousand lines of code - and development will
be faster in such cases because there are fewer structuring decisions
to consider. Not only will the development time be faster, but the
code will be smaller, and will run a little faster too - in some cases
a lot faster. I last seriously used C in embedded systems, for
instance, where C++ simply wasn't an option (at the time, and with the
particular platform) for essentially these reasons.

Another good reason to use C for embedded systems is because the
translation to assembler is easier to understand - it is less
abstract. I'd rather not worry about virtual tables or the
implementation of exceptions when debugging means using a rather
primitive in-circuit emulator with no source level facilities.

The size limit of a few thousand lines (I'd say 10,000 lines at most)
is important because, when maintenance is required, you have very few
boundaries to limit the scope of the changes you need to make. C++ is
designed to allow you to put such scope limits in place - if you need
to change the implementation of a class, you have a degree of
assurance that the necessary changes are limited to that class. But
only if you planned things properly to start with.

Steve Horne
Home : steve at
Work : sh at

More information about the Python-list mailing list