tags:

views:

524

answers:

11

I’m electronic engineer with experience with both language C and C++ (I wrote with C for micro-controller and with C++ I wrote for Windows with Borland C++ Builder)

My company develops motor control products, and we are working with STM32 and IAR compiler.

I recognize the technical differences between the languages, I interest in the development coast and in the maintenance cost of the code:

  1. Is the development time of writing C++ code is longer then C?
  2. Is the maintenance cost of C++ code is cheaper then C? (I know that always will be changes in the code)
  3. Is it easy to documents code in C++ against C? (Documents that describe how the code works)
+5  A: 
  1. Depends on the experience of the team involved. With C++ you have access to a richer set of library functions (but be careful of their footprint when embedding them).

  2. Well-designed C++ code will be easier to maintain than C code, as it allows for some syntactic sugar (constructors, destructors, RAII).

  3. The cost of documentation is about the same.

florin
+16  A: 

This is incredibly subjective. I personally think developing in C++ costs less in terms of development time and maintenance, especially for large, complex projects. This is because most large, complex C projects I've seen invariably end up implementing C++ features (such as polymorphism) one way or another. Additionally, I would argue that certain C++ features such as namespaces, data encapsulation and automatic initialization/destruction of objects increase maintainability for large projects; although again some of those things can be more or less emulated to varying degrees of effectiveness in C.

But your mileage may vary considerably, depending on a ton of additional factors. For example, if your programming team has more experience with C than C++, then it may not be worth doing it in C++.

Charles Salvia
Agreed. Now, the benefits of C are mostly in generating small executables with little overhead.
Patrick Niedzielski
I would say the benefits of C is knowing that `i++;` will execute an `inc` instruction(x86) and not do a huge function call involving who knows what.
Earlz
@earlz, I'm highly skeptical of that argument. C++, like C, is a strongly-typed language. Anyone reading the code can trivially determine whether `i` is an `int` or some user-defined class with an overloaded increment operator. Besides, the same argument could be applied to C. Does `i++` increment an integer, or does it move a pointer up by `sizeof(*i)`? You simply can't know, unless you (gasp) know the type, which is trivially easy to determine by simply glancing a little further up the source code.
Charles Salvia
For that matter, `i++` in C might make a call to a function in a floating-point emulation library. Not much different from the situation with a user-defined operator overload in C++. OK, so there's an upper limit on how much work a floating-emulation routine will actually do, whereas in C++ there's no upper limit to how much work an operator overload can do. But in both cases, the code if well-written will do as much work as is necessary to post-increment i, and no more. If you don't want to do that much work, then you can't increment i.
Steve Jessop
+8  A: 

Is the development time of writing C++ code is longer then C?

This will depend entirely on your programmers. Are they better at writing C or C++?

Is the maintenance cost of C++ code is cheaper then C? (I know that always will be changes in the code)

Again, will depend on your programmers. You can write maintainable or unmaintainable code in any language.

Is it easy to documents code in C++ against C? (Documents that describe how the code works)

Depends entirely on your team and the tools you are using, but I would say they would probably be about the same.

So, to summarize, it all depends on the people you have and what they are best at. If you try to put a bunch of C programmers on a C++ project, you'll probably end up with some pretty bad C++. Likewise if you try to put a bunch of C++ guys on a pure C project.

Eric Petroelje
+1  A: 

Language choice has very little to do with the three questions you asked. Choose the language that you are more experienced in, and that allows you to be more expressive.

Scottie T
A: 

Neither one has a specific advantage, really. It's more about how you use it than what, exactly, you're using.

specific answers:

  1. No, this generally depends on the developer's level of experience with the languages. If he/she is more comfortable with C, then he will write C faster than C++(and vise-versa).
  2. Same as the above answer. Maintenance is just as much about the quality of code initially written and sometimes the quality of documentation.
  3. The documentation syntax is exactly the same between the two languages.
Afcrowe
A: 

definitely C++ :)

ufukgun
A: 
  1. Is the development time of writing C++ code is longer then C?

sometimes. there are real-time applications which can't be implemented in C++ at all.

  1. Is the maintenance cost of C++ code is cheaper then C? (I know that always will be changes in the code)

No. It's applications sensitive.

  1. Is it easy to documents code in C++ against C? (Documents that describe how the code works)

In general they equivalent.

vitaly.v.ch
Not sure I believe your first comment. I can think of anything that C does that I can not do in C++
Martin York
You can't trust code generated by C++ compiler as determinate code.
vitaly.v.ch
+1  A: 

You mention the word readtime which I understand means that performance is critical for you. On this point, I can say that C++ does not give a performance hit when compared to C and C++ can and is used in Operating System Kernels (Symbian).

C++ does however offer idioms that can degrade performace. Getters and Setters may cause extra code to be produced. Vtable lookups will require a few extra instructions when compared to C code.

When it comes to maintainability, one also needs to bear in mind that system programmers will probably be more comfortable with C then they will be with C++.

doron
A: 

I think all three points you mentioned depend on the tool support. Since C++ is a lot harder to parse and also harder to analyze statically, it may be that some tools are only available for C.

swegi
+2  A: 

Pick the known evil
If you/your shop has a lot more more experience with C than with C++, stick with C. I wouldn't easily introduce a new technology on a mission critical project. Most if the time, qualification of developers is much more important to project succes than your toolset.

Pick the better toolset
On many microcontrollers, compiler technology is five or ten years behind the consumer desktop platforms. A good compiler will make you stop worrying about micro-optimizations, with a decent profiler (that is ready and armed) you can skip a lot of guesswork and custom measurements.

... it's how you use it
All else being equal, it's not the choice of the language, but how you use it. The performance problems of C++ are pretty limited:

  • More features available also means more features you could use incorrectly. There's a temptation to use them because they sound cool, and what could go wrong?
  • Some more things to know the relative/absolute cost for (e.g. member function calls, virtual calls, exceptions, ...)
  • A piece of code can have more different meanings depending on other code, so some performance implications are not as quickly visible as they are in C.

All of these points are about the developers, not the language itself.

peterchen
+1  A: 

Summary

If decent C++ developers and compilers are available, stick to C++.

If not, then you will have to balance the learning curve, the project complexity (which will be easied away by the language adaptability), and the available compilers.

Learning curve

In C, the learning curve for the language is easier: Your developers will more easily grasp the fundamental features of the language... C++ is a much more larger language, containing both a C subset, and a template subset, and an object subset, etc., each subset quite different from the previous one.

Fact is, it can be easy to have very inefficient code with C++. Not because "the language is slow", but because developers will sometimes code it the wrong way (usually by ignoring non-C parts of the C++ languages, like references, etc.). This is part of the "learning curve" problem.

But once the "learning curve" is over, then we can look at the language features...

Language adaptability

C is quite blunt. Either you use C's built-in constructs, and it will be easy (like, adding two integers...), or you don't, and it will be error prone, as well as potentially inefficient.

C++ makes it easy to avoid resource leaks, buffer overruns, memory or stack corruption. In C, those could happen almost at every line of code.

With inlining and encapsulation, C++ makes it easy to work efficiently and securely with any type (i.e. user-defined types). With templates and OOP, C++ makes it very very easy to extend some type with an additional feature.

Of course, all this assume C and C++ will have equally efficient compilers...

Compilers

C++ makes a lot of assumption on compilers. By reading an STL implementation, you'll see a lot of apparently useless function calls, and will wonder at the cost of all this "fancy". Fact is, the compiler will inline those away, making the resulting binary smaller that you thought possible.

But if your compiler is not able to do that, then perhaps C++ would be a bad idea.

Conclusion

1 - Is the development time of writing C++ code is longer then C?

It depends on the size/complexity of the project, and on the familiarity of your developers with the chosen language. If you have C++ developers, stick to C++.

2 - Is the maintenance cost of C++ code is cheaper then C? (I know that always will be changes in the code)

Again, it depends on the size/complexity of the project. The more complex it is, the more architecture you'll need, and thus, the more language supported features you'll need, and then, use C++.

3 - Is it easy to documents code in C++ against C? (Documents that describe how the code works)

I assume you are talking about code clarity (i.e. not Doxygen-like documentation, which works the same way for both C and C++).

It depends on how much features you use. For example, using a C++ string is quite natural, compared to the equivalent code in C. In fact, using complex structures is a lot more easier in C++ than in C...

MyMatrix A, B, C ; // My Matrix is an user-defined object
// etc.
C = A * B ;        // If you believe this is anything but a
                   // multiplication of two matrices, then
                   // you need serious medical help.

There's no need to document this code. Everyone knows what a matrix is, and what a multiplication is. The same code in C would have been quite more verbose...

But then, C++ can be quite verbose, too. Without a foreach (like Boost.FOREACH), writing a loop to iterate each item in a STL container can be quite impressive :

for(std::map<int, std::string>::iterator it = myMap.begin(), itEnd = myMap.end() ;
    it != itEnd ;
    ++it)
{
   // Do something quite complicated...
}

Some C++ language features are even non-natural when coming from a procedural language, and will be part of the learning curve. And the messages resulting from a compiler error for this kind of code could break the sanity of your developers to pieces, if they are unprepared for it.

:-)

So it gets depends again on the knowledge of C++ among your developers.

My personal conclusion?

I started as a C developer. Upon learning C++, I found the language too complicated for my uses, but still, I thought some C++ features would be cool in C... Since then, after years of experience, my viewpoint changed completely. There's no way I will code in C anymore.

For me, there's no point: It's like limiting myself to move with a bike, when I could have the choice to use a bike, a car, a plane, or even my legs.

As long as your C++ compiler is Ok, and your developers know C++ (or want to learn C++), C++ can do everything C can (and usually better) and much more (remember, this is a personal viewpoint).

Post Scriptum: Is C++ used on critical apps?

Apparently, C++ is used in the F-35 (the "F-22 lite"). The following document is interesting because it shows what C++ features are really zero cost (and thus, have positive impact), and what C++ features could have a negative impact on the software:

http://www2.research.att.com/~bs/JSF-AV-rules.pdf

If C++ is good enough for this plane, then I guess C++ is good for a lot of less ambitious projects...

:-)

paercebal