If/then style question

Tim Harig usernet at ilthio.net
Thu Dec 16 17:05:52 EST 2010


On 2010-12-16, John Gordon <gordon at panix.com> wrote:
> I like this style more, mostly because it eliminates a lot of indentation.
>
> However I recall one of my college CS courses stating that "one entry,
> one exit" was a good way to write code, and this style has lots of exits.

So, take the good intentation from one and the single exit from the other:

   def myMethod(self, arg1, arg2):

     if some_bad_condition:
       exitCode = bad1

     elif some_other_bad_condition:
       exitCode = bad2

     elif yet_another_bad_condition:
       exitCode = bad3

     else:
       exitCode = do_some_useful_stuff()

     # possible common cleanup code here

     return exitCode

Or, raise an exception on bad condtions rather then passing an error code.



More information about the Python-list mailing list