Is C++ too complex?

Started by
121 comments, last by Vortez 11 years, 4 months ago
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.
I say Code! You say Build! Code! Build! Code! Build! Can I get a woop-woop? Woop! Woop!
Advertisement
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.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

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.

how would you replace the goto in this bit with an exception

for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
goto loopBreak;
}
}
}

loopBreak:
printf("%d", 100);


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?

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion


how would you replace the goto in this bit with an exception

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

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


how would you replace the goto in this bit with an exception
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:
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;
}

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

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.


That is foolish. The articles that challenge our assumptions are the most important for us to read. Too many developers fall into dogmatic faith that what they know is best, ignoring better solutions. This is one of the quickest ways to become a bad programmer.

While there aren't many unnecessary parts of C++ there are many parts that should be avoided, and far more that require great care to use properly. The language itself is a poor option for many programming problems.

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

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.

Stephen M. Webb
Professional Free Software Developer


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?

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.
Project: Project
Setting fire to these damn cows one entry at a time!

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.

In C++. The assertion is not transitive to C.

Stephen M. Webb
Professional Free Software Developer

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

This topic is closed to new replies.

Advertisement