Sign in to follow this  
Kyall

Is C++ too complex?

Recommended Posts

Kyall    287
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.

Share this post


Link to post
Share on other sites
LorenzoGatti    4442
[quote name='Kyall' timestamp='1354261356' post='5005610']
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.
[/quote] 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.

Share this post


Link to post
Share on other sites
solenoidz    591
[quote name='Kyall' timestamp='1354261356' post='5005610']
dynamically allocated memory in classes vs putting it in structs.
[/quote]

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

Share this post


Link to post
Share on other sites
Steve_Segreto    2080
[quote name='solenoidz' timestamp='1354264118' post='5005621']
[quote name='Kyall' timestamp='1354261356' post='5005610']
dynamically allocated memory in classes vs putting it in structs.
[/quote]

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).

Share this post


Link to post
Share on other sites
BitMaster    8651
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.

Share this post


Link to post
Share on other sites
L. Spiro    25622
[quote name='Kyall' timestamp='1354261356' post='5005610']
dynamically allocated memory in classes vs putting it in structs.
[/quote]
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 Edited by L. Spiro

Share this post


Link to post
Share on other sites
Kyall    287
All good answers guys, it was nice to read every single post confirming my biases that there's nothing unnecessary in the language, next time I read an article that has me challenge my assumptions about c++ I'll ignore it. Also using a subset for clarity seems like a good idea.

Share this post


Link to post
Share on other sites
kop0113    2453
The parts of C++ that should generally be avoided (unless necessary) is misusing C stuff rather than the C++ alternative.
For example fopen, malloc and goto could be replaced with ifstream, new (with smart pointer) and exception respectively.

Whilst the ability to use classic C stuff seems to make the language complex, I actually prefer the way C++ extends rather than reinventing the whole language again from scratch (such as C#).

Kindof like OpenGL is seemingly quite hard to learn for new developers because it still has all the old stuff rather than dropping it all and starting with a brand new graphics API.

Share this post


Link to post
Share on other sites
NightCreature83    5002
[quote name='Karsten_' timestamp='1354273814' post='5005651']
The parts of C++ that should generally be avoided (unless necessary) is misusing C stuff rather than the C++ alternative.
For example fopen, malloc and goto could be replaced with ifstream, new (with smart pointer) and exception respectively.

Whilst the ability to use classic C stuff seems to make the language complex, I actually prefer the way C++ extends rather than reinventing the whole language again from scratch (such as C#).

Kindof like OpenGL is seemingly quite hard to learn for new developers because it still has all the old stuff rather than dropping it all and starting with a brand new graphics API.
[/quote]
how would you replace the goto in this bit with an exception
[code]
for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
goto loopBreak;
}
}
}

loopBreak:
printf("%d", 100);
[/code]

The code I am presenting is not doing something usefull at all to be honest but imagine a difficult calculation going on over a grid in which if a certain condition is met you need to break out of both loops and continue the rest of the algorithm with the results already calucalted?

Share this post


Link to post
Share on other sites
Bacterius    13165
[quote name='NightCreature83' timestamp='1354278416' post='5005667']
how would you replace the goto in this bit with an exception
[/quote]
[source lang="cpp"]int doWork()
{
for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
return 1;
}
}
}

return 0;
}

// ...

if (doWork()) printf("%d", 100);[/source]

Or, use a boolean flag for each loop... works too but it's ugly.

EDIT: I guess foo wasn't the best function name, lol. Edited by Bacterius

Share this post


Link to post
Share on other sites
Hodgman    51234
[quote name='NightCreature83' timestamp='1354278416' post='5005667']
how would you replace the goto in this bit with an exception
[/quote]I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
[code]void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}[/code]
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

[hr][edit]
Out of interests sake, because I assumed they'd all generate pretty much the same Asm, I [url=http://pastebin.com/TJhFHYg9]tested[/url] my C++ compiler (MSVC9 /O2) with the above loop+flag, the previously posted function+return, and the original goto-as-double-break variants. In all cases the loop+flag was the slowest, so I'll normalize the results against it and call it's speed 100%.
If the break condition is hit quickly (e.g. foo*bar == 100), then the function+return solution finished in 94% of the time, and the goto in 92% of the time. If the break condition isn't hit (e.g. foo*bar == 99999), then function+return finished in 67% of the time, and goto in 99% of the time...
[edit2]Added a 4th test for throwing a bool to escape the two loops. If the condition isn't met, it weighed in at 103%, but if the condition is met quickly as before, it took 206358% as long...
So, uh, yeah, I'd go with Bacterius' code. Edited by Hodgman

Share this post


Link to post
Share on other sites
Bregma    9202
[quote name='Hodgman' timestamp='1354279576' post='5005671']
I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++.
[/quote]
It's funny, because ,y gut feel puts me in the same position with many constructs. Using dictionaries or regular expressions in Python, for example, is normal but in C++ I tend to avoid them because they're expensive.

Funny thing is, much of the time such constructs in higher-level languages are implemented using the underlying C++ construct. For instance, Java exceptions in the GCC Java runtime are implemented using exactly the same mechanism as C++ exceptions (it's the same libgcc_s.so file on my system). Any performance barrier is psychological: Java forces you to pay the cost always by design, C++ allows you to pay only for what you use, and you can avoid using exceptions, so you feel guilty about waste.

I am still of the opinion that exceptions are too costly to use for anything but exceptional situations. I limit (but not avoid) their use in C++, avoid Java completely, and use Python mostly for prototyping.

Share this post


Link to post
Share on other sites
Bluefirehawk    1232
[quote name='NightCreature83' timestamp='1354278416' post='5005667']
The code I am presenting is not doing something usefull at all to be honest but imagine a difficult calculation going on over a grid in which if a certain condition is met you need to break out of both loops and continue the rest of the algorithm with the results already calucalted?
[/quote]
I would say wrong semantic. Since you loop as long as a condition is met, you should need a while loop. Breaking out of a for loop is a strong indicator for that.

It is a bit nitpicky, but from a theoretical computer science stand point, for loop with a break statement is wrong. A for loop is intended to repeat the internal statement for a known amount. Therefore a for loop can be expanded by repeating the line of code by x amount of time.

A while loop is very different, each while loop has a variance and invariance which you can use to proof your loop aborts, and with the invariance you can proof that your loop calculates what it should.
</detail>

I am sure there is a freak case where a goto statement makes more sense and is more readable than any other solution, But I have yet to see a good example for it.
You can live a happy and productive life writing clean code without ever using a goto statement. Edited by Bluefirehawk

Share this post


Link to post
Share on other sites
Bregma    9202
[quote name='Bluefirehawk' timestamp='1354284849' post='5005693']
I am sure there is a freak case where a goto statement makes more sense and is more readable than any other solution, But I have yet to see a good example for it.
You can live a happy and productive life writing clean code without ever using a goto statement.
[/quote]
In C++. The assertion is not transitive to C.

Share this post


Link to post
Share on other sites
Hodgman    51234
[quote name='Bregma' timestamp='1354284410' post='5005688']It's funny, because ,y gut feel puts me in the same position with many constructs. Using dictionaries or regular expressions in Python, for example, is normal but in C++ I tend to avoid them because they're expensive.[/quote]Yeah I get the same feelings, in C# a dictionary is common for me, but I never use [font=courier new,courier,monospace]std::map[/font] in C++, and a hash-table is almost always implemented on top of a flat array... but this is because I'm writing real-time systems in C++ and bloated tools in C#.
[quote]Any performance barrier is psychological: Java forces you to pay the cost always by design, C++ allows you to pay only for what you use, and you can avoid using exceptions, so you feel guilty about waste.[/quote]No, exceptions in C++ are a fundamentally different construct than exceptions in Java, they just happen to share terminology. Plus most C++ compilers suck at implementing their flavour of them, while the JVM is good at it's flavour. If you port my benchmarks to Java, there's no way the throw version of the double-break idiom will be 2000x slower than the other implementations. Edited by Hodgman

Share this post


Link to post
Share on other sites
SeraphLance    2603
[quote name='Kyall' timestamp='1354271289' post='5005648']
All good answers guys, it was nice to read every single post confirming my biases that there's nothing unnecessary in the language, next time I read an article that has me challenge my assumptions about c++ I'll ignore it. Also using a subset for clarity seems like a good idea.
[/quote]

The question presented was whether there were any components to C++ that were so complex as to be unecessary. There are definitely unecessary parts to C++ -- Valarrays immediately come to mind -- but I'm not learned enough in the language to identify any language features that are unecessary out of overcomplexity (except the ultimate low-hanging fruit goto).

Share this post


Link to post
Share on other sites
larspensjo    1561
My own experience of C++ is that, for every good pattern to solve a problem, there are 10 bad ways of doing it. I needed a lot of time to learn that, and there are surely ways I don't know yet (and I don't know that I don't know).

Share this post


Link to post
Share on other sites
Mike.Popoloski    3258
C++ most assuredly has features that are too complex and/or arcane for their own good. Sure, they're "valid" features, but if you use one in your code you're going to get slapped upside the head during the code review. For example, overriding the comma operator [img]http://public.gamedev.net//public/style_emoticons/default/sleep.png[/img]

Share this post


Link to post
Share on other sites
larspensjo    1561
[quote name='Mike.Popoloski' timestamp='1354299991' post='5005766']
C++ most assuredly has features that are too complex and/or arcane for their own good. Sure, they're "valid" features, but if you use one in your code you're going to get slapped upside the head during the code review. For example, overriding the comma operator [img]http://public.gamedev.net//public/style_emoticons/default/sleep.png[/img]
[/quote]
It can be funny also, making practical jokes (not possible with proper configuration management).

#define struct union // Save memory

#define while if // Speed optimizer

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this