Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > The Samurai Principle

Reply
Thread Tools

The Samurai Principle

 
 
Robert Kern
Guest
Posts: n/a
 
      09-10-2010
On 9/10/10 5:17 PM, Lawrence D'Oliveiro wrote:
> In message<(E-Mail Removed)>, 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.

--
Robert Kern

"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

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-11-2010
On Sat, 11 Sep 2010 10:17:21 +1200, Lawrence D'Oliveiro wrote:

> In message <(E-Mail Removed)>, 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.


Of course it is. Which makes it hard to distinguish None used as data
from None used as a signal for an exceptional case.

--
Steven
 
Reply With Quote
 
 
 
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-12-2010
In message <(E-Mail Removed)>, Robert
Kern wrote:

> On 9/10/10 5:17 PM, Lawrence D'Oliveiro wrote:
>
>> In message<(E-Mail Removed)>, 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?
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      09-12-2010
On 9/11/10 7:08 PM, Lawrence D'Oliveiro wrote:
> In message<(E-Mail Removed)>, Robert
> Kern wrote:
>
>> On 9/10/10 5:17 PM, Lawrence D'Oliveiro wrote:
>>
>>> In message<(E-Mail Removed)>, 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.

--
Robert Kern

"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

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-12-2010
On Sun, 12 Sep 2010 12:08:26 +1200, Lawrence D'Oliveiro wrote:

>> 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?



Are you trolling?

On the unlikely event that you're not, if you have a function called
"len", the *intent* of the function is to return the length of its
argument. That's why it's called "len" rather than "raise_exception" or
"return_none", say.

The use of the term "fail" is entirely unexceptional here. It is normal
English to say such things as "the function re.match() returns None if
the regular expression fails to match" or "if we fail to acquire a lock,
raise an Exception".


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scythe Samurai Z Silverstrand Front Page News 0 02-08-2006 04:29 AM
Re: [Ent] From "sword master" Nick Powell to the last Samurai Tom Cruise John Doe Computer Support 0 09-16-2003 03:49 PM
[Entertainment] The Last Samurai Dzuyen Computer Support 1 09-15-2003 03:12 PM
Re: [Infor.] The Last Samurai John Doe Computer Support 0 09-15-2003 03:08 PM
[Info.] Tom Cruise in The Last Samurai Dzuyen Computer Support 1 09-15-2003 09:06 AM



Advertisments