Re: Remove redundant assignment of a variable in function AlterPublicationTables

Поиск
Список
Период
Сортировка
От Chao Li
Тема Re: Remove redundant assignment of a variable in function AlterPublicationTables
Дата
Msg-id BED2315E-3F8A-4335-A87E-A60E6BF49428@gmail.com
обсуждение исходный текст
Ответ на Re: Remove redundant assignment of a variable in function AlterPublicationTables  (Peter Smith <smithpb2250@gmail.com>)
Список pgsql-hackers


On Aug 21, 2025, at 09:10, Peter Smith <smithpb2250@gmail.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/




В списке pgsql-hackers по дате отправления: