When to use C and when C++?

The C++ programming language has been created to supplant the C programming language. It has been a quite successful language, but C is still used in many projects. When it is better to stick to C and when the migration to C++ is come of age?

First of all, the most important aspect to choose any programming language are its knowledge by the developers, and the existence of code base to maintain.

Secondary factors are the availability of good tools and good libraries for the language, and a large and active community of developers.

But given that a completely new big project or a set of completely new projects is about to start, and the programming team has already some experience in using both C and C++, and for the target platform high quality tools are available, which language is to be used?

I think that the size of the program to build should be used as discriminating factor. And the size of the program should be measured by its addressable memory, using the following rule.

If the executable binary software to construct will need to address no more that 64KB of code and no more than 64KB of data (static+stack+heap), then use C language; otherwise use C++.

For old MS-DOS programmers, this requirement amounts to: if you are going to use the “small” memory model, using only “near” pointers, use C language; otherwise use C++.

Fifteen years ago, perhaps the threshold could be bigger (say 1MB for code+data) , because at that time C++ had some drawbacks that in the last fifteen years have been overcome.

The rationale for such a threshold is twofold:

  • If a program should be very strict in its memory use, then by using C it is easier to manage resource consumption, while by using C++ the design abstractions may make some resource waste slip in.
  • If a program should be small both in written code and in used libraries, the design abstractions and the large libraries of C++ are of little use.

But there is another important rule:

If less than 20% of developers is the team have read at least 1500 pages about C++, forget about using it.

This amounts to say: “A successful C++ team should have at least one knowledgeable C++ developer every five developers, and to become a knowledgeable C++ developer a person should read books for at least 1500 pages in all”. If this requirement is not fulfilled, study more, or hire C++ experts, or stick to C language. Of course all C++ developers must know some C++, but it is enough that some of them are experts, to design the architecture and solve problems. The others will begin with chore programming, and will become experts with time.

This is so, because C++ is difficult to use correctly (i.e. to make working software), but it is even more difficult to use well (i.e. to make efficient and maintainable software).

Advertisements

About Carlo Milanesi

I am a software developer in Italy. I have develop financial, engineering and commercial software using many programming languages, mainly C, C++, Visual Basic, Java, and C#. Now I am interested in Rust and TypeScript.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s