Re: Remove redundant assignment of a variable in function AlterPublicationTables

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Peter Smith <smithpb2250(at)gmail(dot)com>
Cc: Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Kirill Reshke <reshkekirill(at)gmail(dot)com>
Subject: Re: Remove redundant assignment of a variable in function AlterPublicationTables
Date: 2025-08-21 02:15:14
Message-ID: BED2315E-3F8A-4335-A87E-A60E6BF49428@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Aug 21, 2025, at 09:10, Peter Smith <smithpb2250(at)gmail(dot)com> wrote:
>
> I feel that having redundant assignments can be misleading because
> someone reading the code might think the initial value matters or has
> some significance, when it does not.
>
> ~~~
>
> Here's another example, in the same function:
> ----------
> PublicationRelInfo *newpubrel;
> Oid newrelid;
> Bitmapset *newcolumns = NULL;
> ----------
>
> None of those initial values above is needed because these variables
> are all immediately/unconditionally reassigned. So, why is 'newpubrel'
> not assigned up-front, but 'newcolumns' is assigned? IMO this implies
> that the 'newcolumns' initial value has some meaning (which it
> doesn't), so it just introduces unnecessary doubts for the reader...
>

For “newcolumns”, the initialization can be removed, because it’s immediately and explicitly assigned:

newcolumns = pub_collist_validate(newpubrel->relation,
newpubrel->columns);

However, for “isnull”, it doesn’t get explicitly assigned, it depends on the subsequent function to set a value to it. If the subsequent function gets a bug with not assigning it, it would lead to unpredictable results. With an explicit assignment, even if there is a bug of not assigning “isnull" in the subsequent function, the bug will lead to a consistent result. So, I think it’s a good practice to always initiate a value before passing its pointer to a function.

--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Erik Wienhold 2025-08-21 02:26:32 Re: Add GUC to enable libxml2's XML_PARSE_HUGE
Previous Message Fujii Masao 2025-08-21 02:13:44 Re: Don't treat virtual generated columns as missing statistics in vacuumdb --missing-stats-only