"Lipska the Kat" <lip...@lipskathekat.com> wrote in message news:c76dnv778_sw4zvnnz2dnuvz8ukdn...@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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to