Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

17
  • 13
    Most of the people who like functions and procedures to have single entry and exit points grew up on Pascal. Pascal was not the first language that I learned, but it had a profound impact on how I structure code to this day. People always comment on how easy it is to read my Java code. That's because I avoid multiple exits points as well as intermixing declarations with code. I try my best to declare every local variable used in a method at the top of the method. This practice avoids code ramble by forcing me to keep methods succinct. Commented Mar 15, 2011 at 15:37
  • 6
    Pascal also had nested functions. Just sayin'... Commented Mar 15, 2011 at 17:32
  • 6
    From what I remember, back in the day, the main reason that people didn't like multiple return statements in functions was because debuggers didn't handle them properly. It was a real pain to set a breakpoint at the end of a function but you never hit it because of an earlier return statement. For every compiler I use nowadays that is no longer an issue. Commented Mar 15, 2011 at 19:16
  • 38
    @bit-twiddler: I'm not so sure about that. I'm still using Pascal today, and I generally regard "single entry single exit," or at least the single-exit part of it, as cargo cult programming. I just consider Break, Continue and Exit as tools in my toolbox; I use them where it makes the code easier to follow, and don't use them where it would make things harder to read. Commented Mar 15, 2011 at 21:32
  • 3
    Many have agreed that the point is to make reading the code or reasoning about it easier. When reading code, though, I usually don't follow a top-bottom pattern. I'm trying to understand something about it and not everything about it. So, if I see a while (a < 0) {...} and I think I could jump to the end of the block and expect to a >=0 to hold. A break could break that expectation. Commented Jul 13, 2016 at 21:23