Advertisement

Nested switch statements

Started by December 08, 2024 07:36 PM
14 comments, last by Angels 5 days, 9 hours ago

Calin said:
I remember someone saying on codeproject.com that goto defeates the purpose of c++

I can't tell why, but i have never really used the goto keyword. It feels indeed somehow wrong to do so.

But recently i did. Only for debugging reasons. Temprorary code.
It worked so well for a problem otherwise cumbersome to handle.

And now i think, it happens pretty often i want to break our of two scopes. Nested loops mostly. Requires an extra check for the outer loop.

With goto i would not need the extra check, and the code would be easier to read as well in those cases.

So i'm no longer sure we should treat goto like the black sheep of the family, which is always here, but isn't welcome.

Maybe the goto keyword deserves more attention, more use.
Yes! Let's be fair… Goto keywords matter! From now on, gotos shall be treated like any other keywords! \:D/

JoeJ said:
Yes! Let's be fair… Goto keywords matter

If you like everything c++ has to offer in terms of language variety mayby goto has it's uses. For me it sounds like a feature from assembly. I'm only using 30 to 50 percent of C++ language features (that's an optimistic appreciation). goto is probably one of those features that I don't need.

My project`s facebook page is “DreamLand Page”

Advertisement

JoeJ said:

Calin said:
I remember someone saying on codeproject.com that goto defeates the purpose of c++

I can't tell why, but i have never really used the goto keyword. It feels indeed somehow wrong to do so.

But recently i did. Only for debugging reasons. Temprorary code.
It worked so well for a problem otherwise cumbersome to handle.

And now i think, it happens pretty often i want to break our of two scopes. Nested loops mostly. Requires an extra check for the outer loop.

With goto i would not need the extra check, and the code would be easier to read as well in those cases.

So i'm no longer sure we should treat goto like the black sheep of the family, which is always here, but isn't welcome.

Maybe the goto keyword deserves more attention, more use.
Yes! Let's be fair… Goto keywords matter! From now on, gotos shall be treated like any other keywords! \:D/

This is exactly how you shouldn't use gotos and why they were deemed as bad in the theory of structured programming. That is basically instead of writing a control flow statement you use a jump to a possibly random place in execution flow.
You also have to realise that at the time all of this was thought through programming was very different from what its now, high level languages and how to structure them is what people where figuring out still.

I know exactly how you use it and lots of driver code is like that, specially for error handling in functions. This is not always easier to read for people who didnt write the code, than seeing an if and call a function.

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

Much of the hatred of goto stems from a 1968 paper titled “Go To Statement Considered Harmful”. It predates the C language by four years. It predates not just the Intel 8086 and 8088 processors, it was published four months before Intel was founded.

The paper advocates for using structures like functions, for loops, do/while loops, and other control structures introduced in languages like BASIC, COBOL, Simula, BCPL, and similar rather than using spaghetti code, and if you were using assembly to structure your code in ways that mimic the control structures used in the then-modern languages.

The best use of goto that I've consistently seen is to allow structured logical cleanup after errors when RAII can't handle it or in C where destructors don't exist. Even then, it is extremely rare.

We have a laaaaarge codebase sprinkled with that (goto), its often found in legacy projects. Makes things harder to follow, we're keeping it on the radar for future improvements when time allows..

None

Advertisement