The Samurai Principle
robert.kern at gmail.com
Sun Sep 12 07:20:46 CEST 2010
On 9/11/10 7:08 PM, Lawrence D'Oliveiro wrote:
> In message<mailman.654.1284158340.29448.python-list at python.org>, Robert
> Kern wrote:
>> On 9/10/10 5:17 PM, Lawrence D'Oliveiro wrote:
>>> In message<mailman.567.1283927599.29448.python-list at python.org>, Ian
>>> Kelly wrote:
>>>> And returning None on failure is dangerous, because if the programmer
>>>> does not take care to handle that case, the program may attempt to
>>>> regard it as actual data.
>>> But None *is* actual data.
>> And that is exactly the reason why the Samurai Principle says to not
>> return None when the function fails to do what it intended to do.
> How can the function “fail” when it returns what it is specified to return?
The Samurai Principle is about what specifications and guarantees a programmer
should make about his functions. It gives advice for programmers on how to
specify their functions in the face of failure conditions the function itself
sees. For example, a function that is supposed to fetch a record from a database
given a key, a failure condition would be that the key does not exist in the
database. Or the connection times out, or anything else that prevents the
function from retrieving a correct record for the key. The Principle says that a
programmer should specify his function such that it raises an exception to
signify this failure rather than to return None.
Of course, a programmer may make his functions' contracts as loose as he
pleases, and thus returning None may be "correct" in the sense that it satisfies
those contracts. The Principle simply advises that making such contracts is not
a good idea.
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list