Re: [PATCH] Implement motd for PostgreSQL - Mailing list pgsql-hackers

From ilmari@ilmari.org (Dagfinn Ilmari Mannsåker)
Subject Re: [PATCH] Implement motd for PostgreSQL
Date
Msg-id 874kglq4bt.fsf@wibble.ilmari.org
Whole thread Raw
In response to Re: [PATCH] Implement motd for PostgreSQL  (Fabien COELHO <coelho@cri.ensmp.fr>)
Responses Re: [PATCH] Implement motd for PostgreSQL  ("Joel Jacobson" <joel@compiler.org>)
List pgsql-hackers
Fabien COELHO <coelho@cri.ensmp.fr> writes:

>>> The actual source looks pretty straightforward. I'm wondering whether pg
>>> style would suggest to write motd != NULL instead of just motd.
>>
>> That's what I had originally, but when reviewing my code verifying code style,
>> I noticed the other case it more common:
>>
>> if \([a-z]* != NULL &&
>> 119 results in 72 files
>>
>> if \([a-z]* &&
>> 936 results in 311 files
>
> If other cases are indeed pointers. For pgbench, all direct "if (xxx &&"
> cases are simple booleans or integers, pointers seem to have "!=
> NULL". While looking quickly at the grep output, ISTM that most obvious
> pointers have "!= NULL" and other cases often look like booleans:
>
>   catalog/pg_operator.c:          if (isDelete && t->oprcom == baseId)
>   catalog/toasting.c:     if (check && lockmode != AccessExclusiveLock)
>   commands/async.c:       if (amRegisteredListener && listenChannels == NIL)
>   commands/explain.c:     if (es->analyze && es->timing)
>   …
>
> I'm sure there are exceptions, but ISTM that the local style is "!= NULL".

Looking specifically at code checking an expression before dereferencing
it, we get:

$ ag '(?:if|Assert)\s*\(\s*(\S+)\s*&&\s*\1->\w+' | wc -l
247

$ ag '(?:if|Assert)\s*\(\s*(\S+)\s*!=\s*NULL\s*&&\s*\1->\w+' | wc -l
74

So the shorter 'foo && foo->bar' form (which I personally prefer) is
considerably more common than the longer 'foo != NULL && foo->bar' form.

- ilmari
-- 
"The surreality of the universe tends towards a maximum" -- Skud's Law
"Never formulate a law or axiom that you're not prepared to live with
 the consequences of."                              -- Skud's Meta-Law



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: SP-GiST confusion: indexed column's type vs. index column type
Next
From: Tomas Vondra
Date:
Subject: Re: Autovacuum on partitioned table (autoanalyze)