Coding standard: return succ on failure?

Shuxin Yang shuxin.llvm at gmail.com
Fri Aug 2 09:58:40 PDT 2013


On 8/2/13 7:16 AM, Rafael EspĂ­ndola wrote:
> On 1 August 2013 16:25, Chandler Carruth <chandlerc at google.com> wrote:
>> I have lobbied in the past for doing away with returning false on success. I
>> continue to do so.
>>
>> There are parts of the Clang parser that do this consistently, but they are
>> increasingly few and far between. I consistently see new code being written
>> in both Clang and LLVM using false to mean failure and true to mean success,
>> so I think we should just admit that this is the de-facto standard for new
>> code going forward.
>>
>> That said, the last time I raised this question, Chris showed up to argue.
>> ;]
> ccing him :-)
>
> One think I like with returning true on failure is that
>
> bool doFooBar();
>
> looks like just a special case of
>
> error_code doFooBar();
>
> for functions that don't need to report different types of errors. If
> we change the convention to return false on error, error_code should
> probably evaluate to false on errors too.
>
> Cheers,
> Rafael

Such analogy has hole. "bool foo()" expect returning bipolar result, 
either yes or no.
People tend to think "true" means "it is ok", while false means 
"something wrong take place".

error_code is a structure,  and its name suggests it is to figure out 
which error.
So, it sound quite natural to me that 0 means no error.  I don't think 
"if ((error_code)foo())"
will mislead reader.

The name strongly suggest what it is going to return on succ. In this 
case, name dose mater,
because the name bridge the gap between the reader and the code. Being 
confusing or
insistent is really annoying. I usually not picky at coding style (so 
long as I can clearly understand
the code), however, I'm really mad at return false on succ, as I make 
several mistakes due
to this counter-intuitive practice.

Is returning int 0 on succ confusing?  I don't think so,  if all code in 
the a project consistently
return int 0 on succ as with many old C code, or consistently return 1 
on succ, I guess
the reader will soon get used to it.






More information about the llvm-commits mailing list