Re: Coding style question

From: imad <immaad(at)gmail(dot)com>
To: "korryd(at)enterprisedb(dot)com" <korryd(at)enterprisedb(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Coding style question
Date: 2006-11-02 18:53:54
Message-ID: 1f30b80c0611021053t526198a2red3488a06efdbca2@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Shouldn't we turn on warnings by the compiler on uninitialized
variables? This can also be helpful.

--Imad
www.EnterpriseDB.com

On 11/2/06, korryd(at)enterprisedb(dot)com <korryd(at)enterprisedb(dot)com> wrote:
>
> I've noticed a trend in the PostgreSQL code base - for some reason, we tend
> to avoid initializing automatic variables (actually, the code base is pretty
> mixed on this point).
>
> For example in _bt_check_unique() we have:
> ________________________________
> static TransactionId
> _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel,
> Buffer buf, ScanKey itup_scankey)
> {
> TupleDesc itupdesc = RelationGetDescr(rel);
> int natts = rel->rd_rel->relnatts;
> OffsetNumber offset,
> maxoff;
> Page page;
> BTPageOpaque opaque;
> Buffer nbuf = InvalidBuffer;
>
> page = BufferGetPage(buf);
> opaque = (BTPageOpaque) PageGetSpecialPointer(page);
> maxoff = PageGetMaxOffsetNumber(page);
> offset = _bt_binsrch(rel, buf, natts, itup_scankey, false);
> ...
>
>
> ________________________________
>
>
> Notice that four variables are not initialized; instead we assign values to
> them immediately after declaring them.
>
> I would probably write that as:
> ________________________________
> static TransactionId
> _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel,
> Buffer buf, ScanKey itup_scankey)
> {
> TupleDesc itupdesc = RelationGetDescr(rel);
> int natts = rel->rd_rel->relnatts;
> Page page = BufferGetPage(buf);
> OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
> BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
> OffsetNumber offset = _bt_binsrch(rel, buf, natts, itup_scankey,
> false);
> Buffer nbuf = InvalidBuffer;
> ...
>
> ________________________________
>
>
> I'm not trying to be pedantic (it just comes naturally), but is there some
> reason that the first form would be better? I know that there is no
> difference in the resulting code, so this is purely a style/maintainability
> question.
>
> I guess the first form let's you intersperse comments (which is good).
>
> On the other hand, the second form makes it easy to see which variables are
> un-initialized. The second form also prevents you from adding any code
> between declaring the variable and assigning a value to it (which is good in
> complicated code; you might introduce a reference to an unitialized
> variable).
>
> Just curious.
>
> -- Korry

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Gregory Stark 2006-11-02 18:55:08 Re: Coding style question
Previous Message Henry B. Hotz 2006-11-02 18:45:24 Re: Design Considerations for New Authentication Methods