[openbeos] Re: ifdef style

On 2007-02-25 at 22:19:57 [+0100], Jonas Sundström <jonas@xxxxxxxxxxx> 
wrote:
> Which ifdef:ing is the most desirable in this case?
> (OT's coding guidelines don't mention defines.)
> 
> This:
> 
>     if (fDeviceIcon) {
>         if (IsEnabled()) {
> #ifdef __HAIKU__        
>             Menu()->SetDrawingMode(B_OP_ALPHA);
> #else
>             Menu()->SetDrawingMode(B_OP_OVER);
> #endif
>         }
>         else {
> #ifdef __HAIKU__
>             Menu()->SetDrawingMode(B_OP_ALPHA);    
>             Menu()->SetDrawingMode(B_OP_ALPHA);
>             Menu()->SetHighColor(0, 0, 0, 64);
>             Menu()->SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
> #else
>             Menu()->SetDrawingMode(B_OP_BLEND);
> #endif
>         }        
> 
>         Menu()->DrawBitmapAsync(fDeviceIcon, where);
>     }
> 
> 
> Or this:
> 
> #ifdef __HAIKU__
>     if (fDeviceIcon) {
>         if (IsEnabled()) {
>             Menu()->SetDrawingMode(B_OP_ALPHA);
>         }
>         else {
>             Menu()->SetDrawingMode(B_OP_ALPHA);    
>             Menu()->SetDrawingMode(B_OP_ALPHA);
>             Menu()->SetHighColor(0, 0, 0, 64);
>             Menu()->SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
>         }        
> 
>         Menu()->DrawBitmapAsync(fDeviceIcon, where);
>     }
> #else
>     if (fDeviceIcon) {
>         if (IsEnabled())
>             Menu()->SetDrawingMode(B_OP_OVER);
>         else
>             Menu()->SetDrawingMode(B_OP_BLEND);    
>         
>         Menu()->DrawBitmapAsync(fDeviceIcon, where);
>     }
> #endif
>
> I think the latter is a lot easier to read, but leaves up to the
> reader to understand what the diff is between the two cases,
> (the actual drawing mode setting), which of course in this case'
> is very easy to see. Is this okay?

I would probably choose the second version, but move the common first and 
last 3 lines out of the #ifdef. What you do is what you like best though. 
There is no real guideline, save the general one to prefer code that is 
better to read.

> Also, is it really necessary to #endif // __HAIKU__ or is that
> only used for multipage #ifdefs, such as header guards ?

Again, no real guideline. For multipage #ifdefs it is definitely a must, 
for a few lines it is acceptable to omit the comments, I think. The second 
version is already a borderline case, IMHO. The more interesting question 
is actually what the comments should say, though. Probably:

                ...
        #else           // # !defined(__HAIKU__)
                ...
        #endif  // # !defined(__HAIKU__)

A simple "!__HAIKU__", although not quite correct, would be OK, too, I 
guess.

CU, Ingo

Other related posts: