Is C++ too complex?

Started by
121 comments, last by Vortez 11 years, 4 months ago

[quote name='snowmanZOMG' timestamp='1354529055' post='5006558']
Almost everyone LOVES Java and hates C, C++,

[/quote]

Oh, I missed that one earlier. Almost everyone has bad taste, I see. smile.png

I hate Java, I love C and I like enough things about C++ that I don't think I can go back to programming in C.
Advertisement

Try writing a parser in pure C#, you will run into exactly the same issues. It also happens that there are many more parsers available to C++ than there is C#. And those for C# are usually just bindings to a C++ library anyway.


The original argument was that writing a parser for C++ is a complete pain due to the complexity of the language, not that writing a parser in C++ is a pain.

Writing a parser in C# and C++ are pretty similar in difficulty. Writing a parser for C# is tons easier than C++.
Does anyone still write a parser by hand instead of using yacc, antlr or something ? tongue.png

[quote name='NightCreature83' timestamp='1354278416' post='5005667']
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);


Sorry, been away and a tad late to the party.

There was a great example in Bjarne Stroustrup's book demonstrating throwing an exception out of a deeply nested loop containing the result and then catching it after.
This was however a toy implementation and isn't recommended at all.

I have no problem using a goto in this way (and I do find myself doing similar). In my opinion, this isnt misusing C.
[/quote]
Just thought I'd throw an alternative out here (it's kind of hard to give any kind of "proper" alternative, because the original will always print 100, no matter what...):

int findSomething()
{
for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
return 100;
}
}
}
return -1;
}


// Then in your code...
printf("%d", findSomething());


I've found that most of the time, deeply nested stuff can be turned into useful, smaller functions. Not always, but a lot of the time.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Does anyone still write a parser by hand instead of using yacc, antlr or something ? tongue.png

Actually yes, especially when dealing with C++. The parser for clang is a hand written recursive descent parser and IIRC, gcc switched to a handwritten parser sometime in 2004 or 2005. C++'s grammar is sufficiently unruly that most standard tools have a hard time with it, especially the LALR ones.
Cool, didn't know that !!! thanks for sharing ;)

Just thought I'd throw an alternative out here (it's kind of hard to give any kind of "proper" alternative, because the original will always print 100, no matter what...):

int findSomething()
{
for (int bar =0; bar < 100; ++bar)
{
for(int foo = 0; foo < 100; ++foo)
{
if (foo * bar == 100)
{
return 100;
}
}
}
return -1;
}


// Then in your code...
printf("%d", findSomething());


I've found that most of the time, deeply nested stuff can be turned into useful, smaller functions. Not always, but a lot of the time.


If you can find a good name and parameters that make sense for that function, indeed this is a good solution. But this has little to do with goto: Extracting code into functions with good names and sensible parameters should be done everywhere because it makes the code more clear.

I have seen people bending over backwards trying to avoid using goto, doing things like extricating that part of the code, but the function name ends up being some monstrosity with the word "helper" in it, and it ends up getting most of the local variables of the calling function as parameters, some of them by reference. In that case that piece of code has no business being a function, and the goto is preferable.

I have seen people bending over backwards trying to avoid using goto, doing things like extricating that part of the code, but the function name ends up being some monstrosity with the word "helper" in it, and it ends up getting most of the local variables of the calling function as parameters, some of them by reference. In that case that piece of code has no business being a function, and the goto is preferable.
I recognize multi-level break goto is not a terrible construct, and it's probably necessary in some high performance code, but most of the time you can do this:
[&](){ /* may return from inner loop here */ }();
Same natural code structure as with the goto solution, but no goto, no contrived names or messy argument lists.

[&](){ /* may return from inner loop here */ }();


I am not familiar enough with lambda expressions to know if this is cool or horrible. :)

Same natural code structure as with the goto solution, but no goto, no contrived names or messy argument lists.[/quote]
The goto solution has the same natural code as with the lambda-expression solution, but no lambda expression and no returns that don't actually return from the function.
FWIW, I wasn't necessarily saying "never use goto" (I have issues with blanket statements). I was just showing another alternative (which I have found to usually be preferable (though as I said is not always the case)).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement