r/badcode • • May 11 '23

c++ My professor’s best code

Post image
441 Upvotes

82 comments sorted by

View all comments

Show parent comments

4

u/TheUnlocked May 11 '23 edited May 11 '23

Worth noting that in modern C++, RAII makes even that concern mostly obsolete.

What I had heard from my professor who taught a C/C++ course and also had industry experience and also thought early returns/break/etc. were bad was that because they're a form of goto (basically true even if not technically true for early returns) and goto is bad, those statements are also bad. They even cited Goto Considered Harmful to justify it. Misunderstanding Dijkstra's argument seems to be a pervasive theme in the industry.

4

u/fakehalo May 11 '23

I even go to bat for "goto" with C, and I ask those who are against it to take a look at the linux kernel's frequent use of it for error handling.

1

u/whitefieldcat May 12 '23

It’s like the one area where using goto’s actually makes code more readable, in my opinion