Robust Booleans in C

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
Duoas
Posts: 0
Joined: Mon Apr 18, 2005 10:32 am

Robust Booleans in C

Post by Duoas »

Hi. I am new to the Blender codebase, and I am impressed by the work you're all doing. That said, C lacks an explicit boolean type which traditionally causes some confusion and irregularities in coding style. Remember, in C, booleans are evil, and they'll seek to destroy your sanity. :twisted:

Ton style

Ton made some excellent notes here which everyone should review. In addition to what he wrote, I occasionally see the likes of:

Code: Select all

if (my_bool_var == x) ...
which will bite you. Even if x is zero, which is the only safe case to do this, the following is friendlier to read:

Code: Select all

if (my_bool_var) ...
if (!my_bool_var) ...
Bugs! (The really, really evil kind).

Here's a disaster waiting to happen:

Code: Select all

my_bool_arg_func( my_bit_flags & BF_FOO );
Here's the reason:

Code: Select all

void my_bool_arg_func( short b );
#define BF_FOO 0x10000
Note that b could never be true in this example. :( Worse yet, the most virulent complaint the compiler will give you is a weak type error warning. And to rub it in, it will probably claim to have fixed the error. This will provide many happy nights of bug-hunting.

Whoever gets to it first should modify BKE_utildefines.h with the following, right underneath the definitions of TRUE and FALSE:

Code: Select all

#define BOOL( x ) ((x) ? (TRUE) : (FALSE))
Every parenthesis is important. (I haven't commit priveledges yet.)

Now, our mysterious bug can be obviated thus:

Code: Select all

my_bool_arg_func( BOOL( my_bit_flags & BF_FOO ) );
No matter what happens, your function is now forevermore guaranteed to get a correct and proper boolean value.

Egads! More yapping!?
No, not really. :)

Duoas
Posts: 0
Joined: Mon Apr 18, 2005 10:32 am

Post by Duoas »

Sorry to keep beating a dead horse, but the more this stuff is disseminated into the general C programming mindset the better. :wink:

Naming Conventions
Since C does not explicitly provide a boolean data type, variables which are expected to maintain boolean values should be tagged as such. The reason is that the purpose of the variable is made immediately clear when it is well-named, and there is never any confusion about the range of values it may take (is it really a boolean or is it a degenerate form of enumeration?)

Blender documentation sources do not specify a boolean naming convention. The two most common industry conventions are prefixing the boolean name with a lowercase b or with the word is. Examples:

Code: Select all

int is_textediting;
int isTextEditing;
int bTextEditing;
Any C programmer worth his salt will immediately recognize all of the above as boolean variables indicating whether the user is editing text. (I'm partial to the first.)

Using this convention becomes particularly important when writing function prototypes and when using global variables for purposes of self-documentation. Later on, when someone other than the original author wishes to interface with the code, it is immediatly known what the purpose of the variable is and how it is expected to be used.


Thank you all for your indulgence. :)

Post Reply