What is the stance on partial initializers when declaring multiple variables of the same type?

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 20 23:57:47 PDT 2011


On Thursday 21 July 2011 08:52:03 Andrej Mitrovic wrote:
> I ran into this simple C declaration:
> 
> float float_x, float_y, float_xb, float_yb;
> 
> These need to be explicitly initialized in D, otherwise you either get
> crashes or you won't get anything but a blank screen (with regards to
> rendering with OpenGL).
> 
> Almost instinctively I went for:
> 
> float float_x, float_y, float_xb, float_yb = 0.0;
> 
> But that's incorrect, only float_yb is zero-initialized, the others
> are initialized to NaN (well I predicted that but I was kinda hoping D
> would be cool and use a common initializer).
> 
> What I'm asking is, are partial initializers useful for people? I'm
> not suggesting any changes, but just starting a discussion. It does
> look like this could maybe introduce bugs. I think someone might have
> mentioned this topic before, but I don't recall.
> 
> Here's to a healthy discussion..

Honestly, I tend to think that it's bad practice to declare more than one 
variable on a line. Regardless, this is doing exactly what I'd expect it to. 
But I don't see much point in changing it. If you changed it so that all of 
those floats were initialized to 0.0, it would likely break existing code, and 
personally, I would think that it would be more surprising for them all to be 
initialized to 0.0 than for what happens to happen. In any case, I would 
generally encourage people to avoid declaring multiple variables on the same 
line. It generally makes for less readable code IMHO.

- Jonathan M Davis


More information about the Digitalmars-d mailing list