Skip to content

Commit

Permalink
Replace goto with continue
Browse files Browse the repository at this point in the history
Thanks, Greg!
  • Loading branch information
hsutter committed Nov 8, 2023
1 parent f10444e commit 61b9b1c
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4814,29 +4814,9 @@ class cppfront

// Emit the initializer if it it isn't '_' (don't care) and ...
if (initializer == "_") {
// I'll walk the walk: I've said publicly that _structured_ goto is perfectly
// kosher -- it's only _unstructured_ goto that's harmful (and Dijkstra knew it),
// which means:
// - jumping into a block or statement (I'm looking at you, Duff's Device)
// - jumping backwards (that's an unstructured loop == spaghetti control flow)
// - jumping across a variable declaration (C++ already bans this)
//
// But jumping forward-and-outward, skipping no declarations, is righteous.
//
// Here, using goto is the right tool for the job, and is better code because:
// - it avoids a gratuitous extra level of nesting inside an
// 'if (initializer != "_")' block of the next 100 lines (and please don't
// start the other diatribe about that the next 100 lines should be a
// separate named function - no it shouldn't)
// - which extra indent of identical code would make GitHub's diff for this
// commit super hard to read (diff does not deal well with blocks of code
// that simply change indentation level - in fact seeing that diff without
// the goto was the tipping point to just switch to goto here)
// ... but sadly I feel the need to defend it.
//
// As Scott would say (and has said), "so sue me"
//
goto skip_initializer;
// And on to the next data member...
++object;
continue;
}

if (initializer.empty()) {
Expand Down Expand Up @@ -4934,8 +4914,6 @@ class cppfront
separator = ", ";
}

skip_initializer: ;

// And on to the next data member...
++object;
}
Expand Down

2 comments on commit 61b9b1c

@suy
Copy link

@suy suy commented on 61b9b1c Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did not last long in the repo, but I enjoyed reading your comment, and I liked the reasoning given, also on the talk. :-)

@jcanizales
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While in a vacuum I can buy the "it's better code" conclusion, I think the 22 comment lines of explanation it requires in practice have to be considered in that balance too lol

Please sign in to comment.