views:

220

answers:

6

C++ introduces OOPS, templates and variety of other concepts. But sometimes I am stuck in unmanageable storm of calling convention incompatibilities between methods, convoluted casting between distantly connected classes and struggling with code that forces me think in the level of byte-alignment.

I am very tempted to go back to plain old-C language that is low level enough and simple enough that I can spend much of my time solving a problem at hand, than having to figure out the conceptual implementation and nuances of using C++.

What is your take on such an issue. Using C-language as the first class citizen in my code-base and coating it at the end with a C++ primer make for a better way to manage a conceptual code-base ??

+9  A: 

sometimes I am stuck in unmanageable storm of calling convention incompatibilities between methods, convoluted casting between distantly connected classes and struggling with code that forces me think in the level of byte-alignment.

It sounds like you might be doing something wrong. I don't know what sorts of projects you work on, but I've rarely had to deal with any of those things--certainly never in over 99.9% of the code I've written--and I've written a bit of C++ code (though not nearly as much as others here on StackOverflow).

What is your take on such an issue.

You should consider getting a good book on C++ (like one of the introductory books or any of the best practices books listed in The Definitive C++ Book Guide and List) and really learn C++ if you want to use C++.

James McNellis
+4  A: 

I often feel the way you do. C++ compilers are incredibly bitchy about insignificant details and, if you considered them an object, they have appalling encapsulation, and they give horrendously bad error messages. Sometimes, programming C++ feels like fighting against the Standard.

However, I'd never, ever ditch it for C. If you're considering it, you fail C++. Yes, templates and their syntax and some of their semantics can be a bitch, but the power they offer is unparalleled. The things offered like automatic memory management and the power of the STL just can't be matched by C.

In addition, nobody is forcing you to use templates. You could write a whole C++ program using nothing but the pre-provided templates. You could never use polymorphism, or templates, or encapsulation, or object-orientation as a whole, and yes, sometimes none of those solutions are appropriate. But it's plain stupid not to give yourself the option.

And if you're casting classes in C++ (frequently), it sounds to me like whoever wrote the original code flat out didn't know what they were doing. Same for byte alignment.

DeadMG
"If you're considering [ditching C++], you fail C++." This is exactly backwards, a chiasmatic failure. When I ditch C++ (and I do, whenever I can), it's because C++ failed me. [More here](http://yosefk.com/c++fqa/).
Tim Schaeffer
@Tim Schaeffer: The FQA is a little piece of crap. Excuse my english. It has some truths entangled with completely false statements and a ton of half truths. Then again, someone reads that and trusts the contents...
David Rodríguez - dribeas
@TIM: for C. If you moved to say java/c# it can be beneficial. But C is 100% a step backwards
DeadMG
@Rodriquez: Your comment is less than useful; it is a rhetorical kick in the shins, not an argument.
Tim Schaeffer
@DeadMG: Maybe. In my case (embedded coding on an AVR, 64k flash, 4k ram, no OS) a move to Java/c# is not possible.
Tim Schaeffer
@Tim: Well, it would take a fool to go from C++ to C, so if you can't go to C# or Java, then you're pretty much stuck with C++.
DeadMG
@DeadMG: Well, there is always ADA ;-)
Tim Schaeffer
@Tim: Can't say I've ever used ADA, so no comments there :P C++ -> C is stupid, though.
DeadMG
+3  A: 
  • I've never had to worry about byte alignment unless I was writing binary files.
  • Casting distantly related classes to each other is bad design.
  • I've never worried about calling conventions unless I was writing interrupt routines.
Paul Nathan
byte-alignment becomes important in large code-bases. After certain thousand lines of execution, it becomes very important to check for byte-allignment. And what about processors other that x86 like ARM ??Consider using a pure C++ classes being consumed COM classes, you will see an maze of calling convention incompatibilities exposed at runtime.
de costo
@de costo: "byte-alignment becomes important in large code-bases. After certain thousand lines of execution, it becomes very important to check for byte-allignment. " how so? can you give details?
Paul Nathan
Byte alignment in C++ is very rarely an issue of concern (serialization/network protocols), and then again when it is an issue it is just as much an issue in plain C (and Java, and C#, and... name it).
David Rodríguez - dribeas
+1  A: 

If you forced to frequently "cast distantly connected classes" and "think in the level of byte-alignment" - there is something wrong with the architecture of the project. Language is not the problem here.

C++ is heavy. You probably should not use all the features it provides. KISS principle, you know. You can always pretend that C++ is just "C with classes" and exploit templates and other "hard" stuff only when it will provide reasonable improvement in some areas (like code simplicity, as a matter of fact).

Dark
I strongly disagree. You should use every language feature that you understand, whenever it is appropriate to use each feature, and you should strive to understand features that you don't understand when they would make the task at hand easier. Pretending that C++ is just C with Classes is a terrible mistake: you miss out on most of the most important, useful, and time-saving features of the language and you end up having to write more code than you really should need to write, and more code means more bugs.
James McNellis
It is usualy better to write a little more "simple" code, than try to implement a "smart" thing, and do it wrong. Although I agree that using right tool for the task is the best, it is not obvious what tool is "right" in C++ because of the multi-paradigm nature of tha language. Some arguments: http://yosefk.com/c++fqa/index.html
Dark
+Sorowfull fact: if you fully understand the feature you used, it doesn't mean that one, who will maintain your code, is understand the feature on that level too.
Dark
@James: I do not disagree with you from a project manager's perspective. "You should use every language feature that you understand, whenever it is appropriate to use each feature, and you should strive to understand features that you don't understand"Using many language features in your codebase no doubt make it close to utopia, but from maintainability, usability and understandability would go down. If I were to implement an amazingly efficient scheduler algorithm, the simple the language features i use, the more robust and easy to understand my code is and thus to maintain it.
de costo
If a language feature does not make the resulting code simpler, then that would be an inappropriate opportunity to use that language feature. There are usually multiple "right" approaches, and usually they involve a combination of various language features and idioms. I always write code assuming that whoever will be maintaining it knows the language at least as well as I do or is at least capable of learning the language and its features. I don't think that is an unreasonable assumption.
James McNellis
A: 

I recently switched back from C++ to C and took a liking in C99. But it certainly depends on what you are doing. For a library of reasonable size, C99 with its advantages over C89, is good enough, and as somebody else said you can easily provide a C++ wrapper, if necessary. C++, I only would go for a big project that has large amount of code reuse (internal or external) with templates.

Things from C++ I missed in C89 have nothing to do with objects or so, but are simple things as declaration of variables inside the for and a well defined inline feature. C99 has that plus variable length arguments for macros, so I am happy with it.

Jens Gustedt
Just out of curiosity, What would be functional equivalence of object in C++ ? Any data-structure or pattern that is of particular importance ??
de costo
How much of your thinking effort goes into resource management?
David Rodríguez - dribeas
@de costo: I just use C <code>struct</code> with some naming conventions for initializers and other `methods'. BTW, another feature of C99 are named initializers. They make `static' initialization much more convenient.
Jens Gustedt
@David: reasonable, I think, but if your question targets the lack ofdestructors, yes, this is the one feature that I am missing. With thelack of constructors you get along, I have some macros for that, buthaving things deleted automatically would be nice. But in any caseduring developement I always run my code under valgrind, this is avery reliable quality test for the allocations.
Jens Gustedt
A: 

You are not alone. These are all well-known flaws in C++, a language which forces developers to attend incompatible conventions, to struggle to overcome a convoluted caste system, and to take their code in for byte-realignment every 3,000 lines. Definitely switch back to C.

John
Humorous.....:)
Paul Nathan
Right... after about 3000 lines the objects get bored and move for better views. It is then that keeping them aligned is most important, else anarchy will overtake, and who knows if they will even start partying and drinking.
David Rodríguez - dribeas
Get. Out. Now..
Stefan Valianu