It certainly was not my intent to start a long winded discussion about bitwise flag testing, but I suppose I may as well add in my own 0.02...
Darren Reed wrote: > > My take on it is if you do: > > if ((foo & X) == 0) > > Then it is much less likely for the you to mistakingly do > (and for the compiler to accept): > > if ((foo && X) == 0) > > vs > > if (foo && X) > > So while > > if (foo & X) > > may work and be ok, it's not what I call > "good defensive programming." This argument happens to be one case where I happen to disagree. if ((foo && X) == 0) is completely valid syntax, and will in fact not generate a compiler warning and should lint clean (mind you I have not checked this specific case, but there is no reason as to why this would not be acceptable). IMHO, this is going a bit overboard; mistakes happen. Programming defensively does not mean that brevity must be sacrificed to make code "safe". The problem here is purely one of style. The ANSI/ISO C standards define boolean evaluation very specifically, so nothing is being abused or taken advantage of. With regard to the Style guide, I agree with James C.'s comment; I believe the primary goal of the document is to provide guidelines for easily understandable and maintainable code. Unfortunately this can be somewhat subjective (as in this case). That said, which is more readable? if ((foo & SOME_FLAG) != 0) or: if (foo & SOME_FLAG) Chances are, the fellow in the next cube/office has a differing opinion! Both are correct; the intent of the code is clear in either case. So which is right? Given the above, whichever method is chosen, consistency should be the major consideration (which spurred my original comment), not the style. Cheers, Steve > > Darren > -- Yet magic and hierarchy arise from the same source, and this source has a null pointer. Reference the NULL within NULL, it is the gateway to all wizardry. _______________________________________________ driver-discuss mailing list [email protected] http://mail.opensolaris.org/mailman/listinfo/driver-discuss
