<span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Dan, my firm-but-not-rabid opinion on this has been formed from decades of tracking down portability bugs, irreproducible bugs, etc.</span></div>
<div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Return 't'. Try to get the contract tightened up to specify t.</span></div>
<div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">But so long as the contract is loose, in testing it can be useful to try to inject defects by returning other values.</span></div>
<div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">For any one instance of this, the odds of there being a bug based on the return value is low. But given how many functions return boolean values, the odds of none of the calls to them having a bug aren't so good. Making everything return 't' as a matter of standard practice has the potential to eliminate quite a number of bugs (in the sense of bad behavior, rather than in the sense of incorrect coding).</span></div>
<div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">It's not just Lisp that has this problem. I've seen quite a number of C/C++ bugs where code erroneously depended on a return value of 1 or -1 or odd/even or positive/negative when it should have been zero/non-zero.</span></div>
<div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div><div>A similar area is when algorithms are sensitive to the order of traversal of hash tables. Java supplies LinkedHashTable and LinkedHashSet, which is one approach to removing this variability.</div>
<div><br></div><div>In both cases, the real issue isn't so much that there can be bugs. It is that the bugs that can result, have the potential to be very hard to reproduce, test for, and eliminate. An API contract that doesn't leave this sort of wiggle room makes for much better testability.</div>
<div><br></div><div>Of course, if everyone follows this rule, there's no need for:</div><div>(when (fn1 arg2 arg2)</div><div>   t)</div><div><br></div><div>because fn1 would have returned t or nil -- unless you have an actual mismatch/conversion of contract. In that case, I argue that it is best to  actually capture that intent in the code, as well as all the benefits above. But perhaps it should be written:</div>
<div>(not (eq (fn1 arg2 arg2) null))</div><div>and reserve when for actual control-flow matters, rather than boolean operations.</div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br>
</span></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><br></span></div>Date: Fri, 14 Jan 2011 11:42:16 -0500<br>From: Daniel Weinreb <<a href="mailto:dlw@itasoftware.com" style="color: rgb(20, 125, 186); ">dlw@itasoftware.com</a>><br>
Subject: [pro] Style issue about predicates<br><br>If you have a function that is a predicate, in the sense that<br>the function's contract says that its value should be interpreted<br>as being either false or true, do you think it's better to code<br>
it so that it always returns "t" for the true case?<br><br>Since Common Lisp is quite clear that when a value<br>is being considered in the context of being true/false,<br>nil means false and everything else means true.<br>
So from a language point of view, even considering<br>the "intent" of the definition and not just the spec,<br>there is no need to return t.<br><br>Furthermore, the contract of the function should<br>make it clear that the returned value is an a<br>
true/false context.  This should be in the doc<br>string, or at least in a comment, and the function<br>name should end in "p" (or always "-p" but let's<br>please not get into that in this email thread).<br>
So the caller should know.<br><br>All that said, it's possible that a programmer will<br>fail to heed the contract, simply look at the code,<br>and take advantage of the returned value in<br>more than true/false context.  If you want to prevent<br>
that, you can do something like:<br><br>(defun ...<br>...<br>(when (fn1 arg2 arg2)<br> t))<br><br>It seems that it might depend on the circumstance: how likely<br>do you think it is that a programmer would commit such<br>
a mistake?  The more potentially valuable the returned value<br>is, the more likely.  On the other hand, if it's so valuable,<br>maybe you should actually make that part of the contract<br>rather than making the function have the contract of<br>
a predicate.<br><br>Is this good, bad, don't care, depends on the circumstance?<br><br>-- Dan<br></span>