Re: sparse (static analyzer) report - Mailing list pgsql-hackers

From Greg Stark
Subject Re: sparse (static analyzer) report
Date
Msg-id 87hdliu26n.fsf@stark.xeocode.com
Whole thread Raw
In response to Re: sparse (static analyzer) report  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: sparse (static analyzer) report  (Bruno Wolff III <bruno@wolff.to>)
List pgsql-hackers
Tom Lane <tgl@sss.pgh.pa.us> writes:

>     if (ptr)
>
> Can anyone mount a good readability argument for one over the other?

My argument in favour of "if (ptr)" as well as "if (integer)" and "if (!ptr)"
is simply that all else being equal the shorter expression is clearer. Forcing
the reader to slog through additional syntax to read the code has a cost.

Obviously that argument is subject to abuse and it's very dependent on whether
you consider all else equal. 

For a reduction argument, pretending there's a boolean type with no casts
leads to the excessively verbose java-ish syntax like:
if (foo != 0 && bar != null && baz != 0)

which you can't tell me is easier to read than
if (foo && bar && baz)


What I miss most in both C and Java is the lispish ability to write
expressions like:
foo = bar() || baz() || qux();

instead you have to write out things like this (except worse since you have to
start storing things in temporary variables):
if (bar() != null) {  foo = bar();} else if (baz() != 0) {  foo = baz()} else {  foo = qux();}

I've seen code that consists of a dozen or so of these things repeated. When
the code starts getting so verbose that what could be 12 very simple and clear
lines no longer fits on the screen because you're trying to make it easier to
understand, well, I think you've lost the battle.

-- 
greg



pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: sparse (static analyzer) report
Next
From: "Marc G. Fournier"
Date:
Subject: Re: FATAL: catalog is missing 1 attribute(s) for relid