Hi cody,
no this is no trollposting and please don't get it wrong but iam very
curious why people still use C instead of other languages especially C++.
I can't answer for people in general of course, but as a moderately able
C programmer with a thorough dislike of C++ I can try to explain what my
motives are.
i heard people say C++ is slower than C but i can't believe that. in pieces
of the application where speed really matters you can still use "normal"
functions or even static methods which is basically the same.
A few years ago I did some timing and (counter to my intuition) I found
that, indeed, it didn't make a difference, as you point out. One
remarkable thing was that the C++ executables for my (small) benchmarks
were quite a bit larger, which may be relevant for embedded applications.
in C there arent the simplest things present like constants, each struct and
enum have to be prefixed with "struct" and "enum". iam sure there is much
more.
These are some areas where I would agree that yes, C++ is (a bit)
cleaner than C. Another example is declaring variables inside for()
statements and such, this can truly help readability, and limiting the
scope of a local variable if possible is a good thing. Note that many of
these (almost cosmetic) changes have made their way back into C99.
i don't get it why people program in C and faking OOP features(function
pointers in structs..) instead of using C++. are they simply masochists or
is there a logical reason?
In all honesty I think that many people who prefer C over C++ don't
quite get what all the fuzz is about in OOP (I know I don't). In
principle there are sound advantages to grouping together structs and
their associated method functions from a design perspective. Inheritance
and polymorphism have an important part to play as well, especially in
some areas of application (such as GUIs).
However, this is the first place where you get a tradeoff of execution
speed, executable size, and runtime memory usage (for virtual method
tables) versus design-time advantages. In reality of course, especially
with nowadays' big machines, these disadvantages are not too important
for most applications. However, (to me at least) it is a bit unnerving
to be at the mercy of the compiler and its designer, and hope that he
implemented all this machinery properly and without causing too much
overhead. This is more a psychological barrier than a real one of
course, since modern compilers are able to optimize away many
unneccessary machinery, and some rather clever tricks have been found to
make virtual method calls very cheap. However, as a programmer I am no
longer completely in the driver's seat as I am with C. Looking at a C
porgram, I can have a straightforward and relatively accurate mental
picture of what the actual machine code produced by the compiler will
look like. With OOP and C++, that's no longer true, especially with code
that uses all the available C++ features including exceptions and templates.
One generic complaint I have with OOP (not limited to C++) is that I can
no longer look at a code fragment and reconstruct the execution flow in
my head, because of polymorphism and operator overloading; in C (unless
you're doing funky stuff with threads or longjumps), the execution flow
is pretty much known at compile-time, and can be reproduced from the
code. I happen to like that.
Of course OOP proponents will counter that this is in fact the entire
point of OOP: one should no longer be thinking in terms of structural
execution flow, but rather in terms of objects with a well-defined
behaviorial 'contract', that can be triggered by invoking methods.
Now this is where a lot of subjectivity comes in, but I for one simply
don't think that way. This is probably due to the way I earned my
computer experience (going up from BASIC to 8-bit machine language to
Pascal to C). "Thinking in classes", something that is essential for
good OOP programming, is just not for me (except for some obvious cases
with a small number of classes - I have done some C++ programming in my
time of course).
When the programs get bigger, and the number of classes grows, you come
to another point: there is a transition in the level of design: instead
of putting statements in the right order, you have to start managing a
class model. The Design Patterns school-of-thought comes in here: how do
I design a set of classes with interaction to get a certain type of
bahavior? For me, this has two problems. First, there is often more than
one sensible way of designing a set of classes to address a certain
problem. I have an instinctive dislike of that kind of situation: I have
the (admittedly rather naive) feeling that software related problems
should have a canonical 'best' solution. The second (real) problem is
that C++ doesn't really support the Design Pattern level of abstraction.
Instead, it hands the programmer a number of nuts and bolts that enable
him to approximate the abstract idea encapsulated in a DP. I think there
are no programming languages available today that properly support the
Design Pattern methodology, and it's certainly not C++.
Now of course all this is more a rant against OOP than C++, but IMHO C++
offers no advantages to C99 other than OOP-support, so it is relevant to
your question.
You could argue of course that C++ > (C + OOP). The two most prominent
features apart from OOP that set aside C and C++ are, I think,
exceptions and templates.
As for exceptions, you may know Dijkstra's paper "Goto's considered
harmful". In this paper he has a number of points that I would subscribe
to, concerning the ability of the human programmer to read the meaning
of a piece of code from the source. In essence, he argues that GOTO
statements destroy this possibility.
I would argue that exceptions are "goto's on steroids". Since exceptions
are allowed to cross function-call boundaries, execution flow becomes
very non-transparant - at least to me! This is a similar objection I
have with polymorphism as described above.
A serious problem with exception handling in C++ is it's interaction
with memory management, which is made even worse by having implicit
object creation (with implicit constructor calls). Now I know a lot of
talented programmers, many of which are far more accomplished in C++
programming than I am, but not a single one of them can quote me the
do's-and-don'ts of a 'good' C++ program in this respect. It's a minefield.
Incidentally, having just written a rather big real-life C library with
error handling, I do appreciate the need for exception handling.
However, I think it is a mistake to have a language that does include
exception handling but doesn't do garbage collection for the reason
stated above. Sure, it's possible to do it properly. But one has to
remember that programmers are human beings; most programmers I know have
simply not the level of intimacy with the C++ runtime model to do this
without making mistakes.
Templates.... Suffice it to say that one cannot write a portable C++
program using templates and expect it to work identically on different
compilers. Portability is nil, and this makes this feature not useful in
many practical sutuations. One can complain about (or to) compiler
builders in this regard, but this is just a symptom of overly
complicated semantics. Even if compiler builders get their act together,
the semantics would still be too difficult for most programmers.
Including me.
To summarize I would say C++ with its feature set is just too
complicated, as a language design I feel it has failed. One has to keep
in mind that a programming language is a tool to make programs. If a
tool has a significant chance of being unintentially misused (with
possibly disastrous results), it's not a good tool. I will stick with
something I actually (more or less) understand, which is C.
By the way, did you ever read the Stroustrup book ('Programming in
C++')? As the book progresses, his examples evolve from things that look
sort-of-like-C to STL-based programs that are (to my untrained eye at
least) simply ugly. My feeling is that he tries to bring the expressive
power of dynamic interpreted languages to the realm of compiled
languages. A valliant attempt, and I would applaud him for it. However,
his writing conveys a breathtaking arrogance or perhaps lack of
understanding for the fact that most programmers are mere mortals... I'm
sure as the language's designer he is able to mentally internalize the
runtime model of C++, but to think that your average programmer could
readily do the same is just preposterous.
And a last thing: try writing a library in C++ and linking it with a
program written in C (like Matlab, IDL, Mathematica...). Now there's a
practical reason to prefer C over C++ if I ever saw one.
Thank you for giving me an opportunity to rant a bit about this. Perhaps
this therapeutical excercise of mine will give you some insight in the
reasons why some of us still prefer C over C++!
Best regards,
Sidney Cadot
The Netherlands