Re: Allow declaration after statement and reformat code to use it

From: Chapman Flack <chap(at)anastigmatix(dot)net>
To: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>, Jelte Fennema <Jelte(dot)Fennema(at)microsoft(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Allow declaration after statement and reformat code to use it
Date: 2021-08-19 15:40:44
Message-ID: 611E7B7C.8000102@anastigmatix.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 08/19/21 09:38, Ranier Vilela wrote:
>> 2. Declarations are closer to the actual usage. This is advised by the
>> "Code Complete" book [2] and has the following advantages:
>> a. This limits variable scope to what is necessary. Which in turn makes
>> the mental model you have to keep of a function when reading the code
>> simpler.
>> b. In most cases it allows you to see the the type of a variable
>> without needing to go to the top of the function.
>> 3. You can do input checking and assertions at the top of the function,
>> instead of having to put declarations in front of it. This makes it clear
>> which invariants hold for the function. (as seen in the example above and
>> the changes for pg_file_rename[3])

I'm in sympathy with all of those points. I've never believed that the
arbitrary separation of declaration from use that was forced by C < 99
made anything more readable. If the project were started now from scratch,
I would be all in favor of declaring at first use.

But I think Tom's concern about backpatching hazards may carry a lot
of weight in practice.

>> It uses a Perl regex to search and replace! (obligatory jokes at the
>> bottom of the email)

However, even if such an idea were to get the green light, I think I would
take the obligatory regex jokes seriously, and instead use something like
srcML [0] and do the analysis and modification on proper parse trees.

Regards,
-Chap

[0] https://www.srcml.org/about.html

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2021-08-19 16:07:25 Re: Middleware Messages for FE/BE
Previous Message Alvaro Herrera 2021-08-19 14:50:10 Re: Proposal: More structured logging