3 ms·
There's a very old programming style rule about only ever having one entry and one exit to a function. This rule dates back to the days of assembly programming,
by thristian 7y ago
There's a very old programming style rule about only ever having one entry and one exit to a function. This rule dates back to the days of assembly programming, where any 'function' could just jump directly into the middle of some other function, or jump out before the end. This made understanding and maintaining code super-difficult, and this rule made a lot of sense.
Eventually, "structured programming" languages like C and Pascal were invented, where you couldn't just leap from one arbitrary part of the program to another. In these languages, the compiler enforced that each function had a single entry point, solving part of the problem. However, for C in particular the rule was still kind of useful - all the resources allocated during the function needed to be cleaned up by the end, so it was still good practice to have a single exit point that always did whatever cleanup was required.
These days, most languages have a garbage collector that will automatically clean up for you. Languages that don't have a garbage collector, like C++ and Rust, generate all the deallocation logic at compile time (the RAII idiom).
If you're using a language designed after the early 1980s, the "single entry and single exit point" rule is either wholly irrelevant, or does more harm than good.