Is C++ too complex?

Started by
121 comments, last by Vortez 11 years, 3 months ago
I always want to improve my C++ knowledge and this time around this means finding out what parts of C++ aren't any good, so I thought I'd field a question to the experts: what parts of the c++ language are too complex to the point they interfere with writing good code and should be avoided as much as possible. Should I always use the stdio and not iostream for example, dynamically allocated memory in classes vs putting it in structs. What's the debate on what parts of c++ improve code, and the other parts that are over complicated and are damaging to code standards.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
Advertisement
There are no 'bad' places of C++. C++ is a toolbox with a lot of tools in it, each more or less suitable for particular jobs. The merits of a good programmer are the ability to pick the right tool for each particular job (actually a really good programmer would not limit themselves to just C++, but let's stay with just that language for the purpose of this thread).

If I wanted to parse a small text file every two hours I would certainly not use C style file IO over iostreams. If the whole purpose of my program were chewing through Gigabytes of text files I would use neither explicitly, I would have a look at flex/bison, Antlr or their friends to see which suited my needs best.

What's the debate on what parts of c++ improve code, and the other parts that are over complicated and are damaging to code standards.
C++ is full of subtle and complex features, but they are only "overcomplicated" relative to incompetent users, not relative to their purpose; there are many good reasons to do things in a certain way, and valid use cases for everything you don't like or understand.

Omae Wa Mou Shindeiru


dynamically allocated memory in classes vs putting it in structs.


I'm not sure what you mean by that. Maybe stack vs heap ?

[quote name='Kyall' timestamp='1354261356' post='5005610']
dynamically allocated memory in classes vs putting it in structs.


I'm not sure what you mean by that. Maybe stack vs heap ?
[/quote]

I believe he means that member data of a class is either dynamically allocated and referenced with a pointer or else it isn't (in which case it may be global, stack or still dynamically allocated, depending on how the instance of the class was defined).
what parts of the c++ language are too complex to the point they interfere with writing good code and should be avoided as much as possible.
[font=courier new,courier,monospace]goto[/font]


[edit]and [font=courier new,courier,monospace]volatile[/font][/edit]
While I personally hope that I never have to deal with goto myself, I have encountered enough "damned if I do, damned if I don't" scenarios to understand that I might be at some point in a situation where goto might be the least evil way to solve something. I hope I won't have to ever use it though - no amount of showers would be enough to ever feel clean again after that.

dynamically allocated memory in classes vs putting it in structs.

Classes and structures are exactly the same thing except for default visibility of members and methods. You are asking about 2 unrelated things—whether related information is inside classes or structures does not matter, nor does it matter if it was allocated dynamically or embedded within an encapsulating class/struct. There are combinations of these 2 things suitable for various situations, and there is a time and place for all of these combinations.

It is just common practice to make structures hold “plain old data”, but otherwise there is nothing unique between classes and structures.


The only part of C++ that should be avoided as much as possible is “[font=courier new,courier,monospace]goto[/font]”.
[EDIT]
Damn it Hodgman, beat me to it.
[/EDIT]


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Although I agree that there are valid uses cases for everything in the language, I mostly restrict myself to the subset of the language that I believe every C++ programmer will understand, because code should be written primarily to be read by other programmers.

Some examples:

  • I use inheritance very infrequently. When I do, I only use single public inheritance and only for the purpose of using virtual dispatch, and I always provide a factory function that creates the actual objects (this is also about the only place where I would write `new' in a program, which is wrapped in a smart pointer right away). The rest of the code is not allowed to know about the specific subclasses.
  • I don't use RTTI.
  • I only overload operators when defining mathematical classes, and the occasional operator << or >> for streams.
  • I don't use trivial standard algorithms (e.g., std::copy), because loops are much easier to read, especially with C++11.
  • I don't make complicated template structures, preferring to do some things at run time that could be done at compile time, because it usually helps the clarity of the program.


I am not as adverse to goto as others are here. I learned programming in the 80s and I wrote my share of goto-packed nightmare programs in BASIC. I don't recommend this to anyone, but it helps me tell the difference between a goto that shows poor understanding of loops and function calls (the vast majority of gotos beginners would use) and one that is clear. There are two types of goto that are so useful and so harmless that they were given special names: `break' and `continue'. Using goto to break out of a loop from within a switch statement, for instance, seems perfectly fine to me. I'll write the code in some other way if I think it's more clear, but only then.
The language itself is to me very beautiful. But it is complex and the programmer requires more knowledge on how the computer system works, compared to a Java/.Net/Perl programmer.
ome stuff doesn't make much sense anymore in the modern C++11 standard, like having structs and classes, which essentially are the same today. I guess they are still around for backwards compatibility.

I think the nasty bit of C++ isn't the language, it is everything around it. Most toolchains for C++ are old, very old, and seem to be deliberately complicated. I spent so much time crawling through the internet, getting my cross platform build script to make a platform dependent build script to build my project. Yet, my project is still not glued to unit tests.
Comparing it to the java, you have far easier toolchains, designed for modern standards of development. And you almost get platform indipendence for free.
Project: Project
Setting fire to these damn cows one entry at a time!

This topic is closed to new replies.

Advertisement