Reading about Delphi’s Exit statement (see here for instance), I cannot ignore that writing about it every author feels the duty to give a piece of advice, for example:
Warning : use with caution – jumping is a concept at odds with structured coding – it makes code maintenance difficult.
Now, I’m coming from C and C++ in Unix and I’m familiar with re-entrancy issues, but honestly I cannot figure out why in Delphi returning from a function before it reaches its natural end should be evil.
Unless every function and procedure in Delphi is considered as re-entrant.
What am I missing?
There are two schools of thought.
One school of thought says that functions should have a single exit point. Many many coding standards enforce that as a rule. The motivation for this comes from the hard experience of maintaining spaghetti code full of large functions with gotos, multiple exits and so on.
The other school of thought says that spaghetti code is bad but one should be pragmatic and judge coding styles on their merits rather than following dogmatic rules. For example, many programmers feel that guard clauses are much preferable to the deeply indented functions that arise when you refrain from using
exit. As an illustration consider the following example from Martin Fowler’s excellent refactoring catalog: Replace Nested Conditional with Guard Clauses.Fundamentally it all comes down to personal preference. I personally encourage the use of guard clauses, but refrain from wild use of exit in long procedures.