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

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: Jelte Fennema <Jelte(dot)Fennema(at)microsoft(dot)com>, Bruce Momjian <bruce(at)momjian(dot)us>, Chapman Flack <chap(at)anastigmatix(dot)net>, "tgl(at)sss(dot)pgh(dot)pa(dot)us" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [EXTERNAL] Re: Allow declaration after statement and reformat code to use it
Date: 2021-08-21 20:05:39
Message-ID: CAEudQAqAQ_yefg8mz31m4dhgi8pkETq-Tk2AzzTQ_H5F_b=tWg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Em sáb., 21 de ago. de 2021 às 12:02, Andrew Dunstan <andrew(at)dunslane(dot)net>
escreveu:

>
> On 8/20/21 12:30 PM, Ranier Vilela wrote:
> >
> >
> > There is a reason why GMs Brian Kernighan and Dennis Ritchie made the
> > C89, less buggy.
> > IMHO C99 makes it easy to make more mistakes.
> > One more step and we won't even need to declare a variable.
> >
> >
>
> I've used both styles in different languages over the years. I don't
> know that one is a clearly better way, notwithstanding what various
> theorists might say.
>
IMO declarations first is better, without a shadow of a doubt.
Maybe because my mind is vision.
Seeing the complete view, I can look for bugs and find some way to improve
performance.
It's one of the reasons for having small, well-defined functions.
Of course, with extra reward, you can and should reduce the scope.
In this case the compiler does a better job and generates more optimized
code.

XLogRecPtr
XLogInsertRecord(XLogRecData *rdata,
XLogRecPtr fpw_lsn,
uint8 flags,
int num_fpi)
{
XLogCtlInsert *Insert = &XLogCtl->Insert; /*
What is the purpose of this variable */
XLogRecord *rechdr = (XLogRecord *) rdata->data; /* What is the
purpose of this variable */
XLogRecPtr
StartPos; /* What is the
purpose of this variable */
XLogRecPtr
EndPos; /* What is the
purpose of this variable */
pg_crc32c
rdata_crc; /* What is the
purpose of this variable */
uint8 info = rechdr->xl_info & ~XLR_INFO_MASK;
bool isLogSwitch = (rechdr->xl_rmid == RM_XLOG_ID && info ==
XLOG_SWITCH);
bool prevDoPageWrites = doPageWrites;
bool inserted;

/* we assume that all of the record header is in the first chunk
*/
Assert(rdata->len >= SizeOfXLogRecord);

/* cross-check on whether we should be here or not */
if (!XLogInsertAllowed())
elog(ERROR, "cannot make new WAL entries during recovery");

All declarations organized by type and use, almost a structure.
No need to find or think, just see.
We can immediately connect variable info with isLogSwitch.

> Note that in C89 it's fantastically easy to put the declaration as close
> as you like to the first use of a variable. All it takes is a pair of
> braces enclosing the variable scope.
>
IMO is an exception, which does not follow the C89 pattern.
It's a valid last resource, of course, but not as good practice.

> Even if we were to relax our rules on this, making wholesale changes
> along these lines and even more backpatching them seem out of the question.
>
It would be a nightmare.

regards,
Ranier Vilela

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bossart, Nathan 2021-08-22 04:28:51 Re: .ready and .done files considered harmful
Previous Message Andrew Dunstan 2021-08-21 15:01:57 Re: [EXTERNAL] Re: Allow declaration after statement and reformat code to use it