Encapsulation, inheritance and polymorphism
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Jul 18 21:34:47 EDT 2012
On Wed, 18 Jul 2012 15:40:00 +0100, Lipska the Kat wrote:
[...]
>> Even with a break, why bother continuing through the body of the
>> function when you already have the result? When your calculation is
>> done, it's done, just return for goodness sake. You wouldn't write a
>> search that keeps going after you've found the value that you want, out
>> of some misplaced sense that you have to look at every value. Why write
>> code with unnecessary guard values and temporary variables out of a
>> misplaced sense that functions must only have one exit?
>
> Object Oriented programming is all about encapsulating human concepts in
> a way that makes sense to human beings. Make no mistake, it is NEVER the
> case that a software system is written for any other reason than to
> serve human beings. OO is more than just the mechanics of writing code,
> it's a state of mind.
Um, yes?
I'm no sure what this has to do with single-exit functions/methods. You
can just as easily write multi-exit methods in an OO language as in a non-
OO language. So long as your language has a return statement which exits
the function, you can have more than one.
I am aware that some languages enforce a single exit point, but that
seems to be an unnecessary restriction to me.
Of course it does require discipline and/or sense to not write crap code:
if you have a 300 line function or method, whether it has dozens of exits
or just one, that is crap code in any language. But if the choice is to
write a 20 line function with three exits, or a 30 line function with a
single exit, there would have to be a good reason to prefer the single-
exit version.
--
Steven
More information about the Python-list
mailing list