lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] [lmi-commits] master 6730bd6 2/3: Add member irr_initialized_


From: Vadim Zeitlin
Subject: Re: [lmi] [lmi-commits] master 6730bd6 2/3: Add member irr_initialized_ to keep track of state
Date: Thu, 22 Feb 2018 22:49:06 +0100

On Thu, 22 Feb 2018 20:25:30 +0000 Greg Chicares <address@hidden> wrote:

GC> In-class initialization would make it less obviously correct IMO.
GC> When I reviewed the changeset one last time before committing it,
GC> I used vim to step through all occurrences of
GC>   /\<irr_initialized_\>
GC> several times, to make sure they all looked right. Because it's
GC> initialized in each ctor-initializer, I know that pressing 'n'
GC> repeatedly, in the 'ledger_invariant.cpp' buffer only, shows me
GC> every place where this member variable's value is set or used
GC> throughout the class. Verifying correctness would be harder if
GC> it could also acquire a value in the header.

 But what prevents an inline ctor from being defined in the header? And
using ":Ggrep -w irr_initialized_" (or ":!git grep -w irr_initialized_"
without vim-fugitive) is not that much harder than using "*".

GC> I don't see in-class initialization as always preferable to
GC> ctor-initializers. It may be better when we have many ctors, e.g.:
GC> 
GC>   http://www.stroustrup.com/C++11FAQ.html#member-init
GC> | the real benefits come in classes with multiple constructors.
GC> 
GC> but here we have two ctors that repeat the same two initializers,
GC> ten lines apart. Duplicating two lines is not a high price to pay
GC> for keeping all initialization and assignment to this member
GC> localized in a single file.

 The trouble is that it's inconsistent. An object will still be
(default-)initialized even if you don't use any specific expression in its
declaration, but a field of a primitive type, such as irr_initialized_,
won't be. I don't see any reason for this difference and because we don't
want to (nor can) keep the objects uninitialized, it seems logical to
conclude that all fields should be initialized.

 I'd also like to say that I've never seen a bug in the code due to an
extra initialization, while I did see (and wrote myself) bugs due to
missing initialization more times than I can remember. So IMO initializing
everything is much safer than not always doing it.

 Regards,
VZ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]