Encapsulation, inheritance and polymorphism

BartC bc at freeuk.com
Wed Jul 18 20:41:38 EDT 2012


"Lipska the Kat" <lipska at lipskathekat.com> wrote in message 
news:c76dnV778_sw4ZvNnZ2dnUVZ8uKdnZ2d at bt.com...
> On 18/07/12 01:46, Andrew Cooper wrote:

>> if not (you are permitted to do this):
>>      return -EPERM
>> if not (you've given me some valid data):
>>      return -EFAULT
>> if not (you've given me some sensible data):
>>      return -EINVAL
>> return actually_try_to_do_something_with(data)
>>
>> How would you program this sort of logic with a single return statement?
>>   This is very common logic for all routines for which there is even the
>> remotest possibility that some data has come from an untrusted source.


> someType result = -EINVAL //or your most likely or 'safest' result
>
> if not (you are permitted to do this):
>       result = -EPERM
> if not (you've given me some valid data):
>       result = -EFAULT
> if not (you've given me some sensible data):
>       result = -EINVAL
> else
>       result = -EDSOMETHING
>
>       return result
> }
> //cohesive, encapsulated, reusable and easy to read

But, it works differently from the above. Perhaps replace some of those "if" 
statements with "elif".

The "return" version is handy because it provides a quick escape mechanism 
without cluttering up the rest of code with extra variables.

-- 
Bartc 




More information about the Python-list mailing list