'Single Entry Single Exit' - Valid Or Nonsense?
In a book I'm reading right now, the author suddenly started to talk about 'single entry single exit' as a principle we should follow. Single exit was supposed to mean, that we don't have many returns in a function, but only one, at the very end, because for some reason that's better. So this is how I might write a prime function: def is_prime(n): if n<2 or not n%2: return False if n==2: return True for div in range(3, sqrt(n)+1, 2): if not n%div: return False return True I'd definitely have several return points. Whenever it's already clear that a number can never be prime, why should I stick around, store a boolean, loop nonsensically and only then return? Where's the gain? Or rather: Isn't there a clear loss? Not only would I end up defining useless variables, I'd run useless code, just for the sake of it. Am I missing something? Am I not understanding the idea correctly? Or is there something to this which I just don't see yet?