Coding style question

Поиск
Список
Период
Сортировка
От
Тема Coding style question
Дата
Msg-id 1162492459.7998.254.camel@sakai.localdomain
обсуждение исходный текст
Ответы Re: Coding style question  (imad <immaad@gmail.com>)
Re: Coding style question  (Gregory Stark <stark@enterprisedb.com>)
Список pgsql-hackers
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).<br /><br /> For example in _bt_check_unique() we have:<br /><hr
noshade="NOSHADE"/><pre>
 
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);...

</pre><hr noshade="NOSHADE" /><br /><br /> Notice that four variables are not initialized; instead we assign values to
themimmediately after declaring them. <br /><br /> I would probably write that as:<br /><hr noshade="NOSHADE" /><pre>
 
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;...
</pre><hr noshade="NOSHADE" /><br /><br /> I'm not trying to be pedantic (it just comes naturally), but is there some
reasonthat the first form would be better?  I know that there is no difference in the resulting code, so this is purely
astyle/maintainability question.<br /><br /> I guess the first form let's you intersperse comments (which is good). 
<br/><br /> On the other hand, the second form makes it easy to see which variables are un-initialized.  The second
formalso prevents you from adding any code between declaring the variable and assigning a value to it (which is good in
complicatedcode; you might introduce a reference to an unitialized variable).<br /><br /> Just curious.<br /><br />
           -- Korry  

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

Предыдущее
От: Jeff Davis
Дата:
Сообщение: Re: Tsearch2 index size
Следующее
От: "Henry B. Hotz"
Дата:
Сообщение: Re: Design Considerations for New Authentication Methods