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