← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/maas/get_one into lp:maas

 

The proposal to merge lp:~jtv/maas/get_one into lp:maas has been updated.

Description changed to:

Discussed with Julian.  The get() in Django's ORM is great for looking up an object that you expect to be there, but not if you're expecting zero-or-one objects because it raises an exception in the zero case and expects you to catch it, typically immediately.  In this branch I provide something that's more like Storm's one(): “I firmly believe that this result set contains at most one item.  Give me that item, or None.”  To maintain flexibility in its use, it also supports other sequences and iterables: you don't exactly need to know whether you're passing it a raw result set or the result of a wrapper that happens to listify the raw result.

I believe this is healthier for situations where finding no object is part of regular, successful program flow.  Exceptions are not part of routine program flow.  Bertrand Meyer says an exception occurs when a function's preconditions are met but it cannot meet its postcondition.  A try/except construct covering a single statement differs from an if/else in that it conveys to readers that the “except” code path should be read in the light of failure recovery, and to the language implementation that the path's performance can be sacrificed in favour of the non-exceptional path.

This is not to be confused with Grace Hopper's “begging forgiveness is easier than asking for permission” paradigm, which applies to interaction with external resources.  There, the choice of code path is best discovered by attempting a single interaction.  That principle is useful to push the control-flow choice down in the call stack so that the program can avoid double interaction with the outside world, and with it, high costs or race conditions.

Jeroen

For more details, see:
https://code.launchpad.net/~jtv/maas/get_one/+merge/121126
-- 
https://code.launchpad.net/~jtv/maas/get_one/+merge/121126
Your team MAAS Maintainers is requested to review the proposed merge of lp:~jtv/maas/get_one into lp:maas.


References