wuthering thoughts
Another pleonastic blog. Diary of an insomniac. And I always lose my words beyond the second sentence.

WTF #1: Microsoft's ternary boolean

Category: , , , , , By Suman Kar
This one goes into my little red-book of WTFs in all-caps. Epic fail from the behemoth. Look up the MSDN documentation of Win32 GetMessage() function. The section on 'Return Value', you ask? Yeah, right,the warning box. I'll quote them:

'Because the return value can be nonzero, zero, or -1,[...]'

In case you are not too familiar with Win32, I'll post the signature as well.
BOOL GetMessage( 
LPMSG lpMsg,
HWND hWnd,
UINT wMsgFilterMin,
UINT wMsgFilterMax
PS: Just so the joke is not lost to the multitude of non-programmers (or even our lucky non-Win32 programmer comrades) I'll try to explain. The 'BOOL' on the left of GetMessage() says that the function (GetMessage) has a boolean return type. Which, for what it is worth, means that the function can either return the value 'true' or the value 'false' after executing. The boolean data-type was never a part of good-ol' C, neither C++ and hence all sorts of folks have had their own interpretation of how these two values ought to be represented. Historically, people have used integers to pose as booleans and treated the value '0' as 'false' and all others (positive or negative) as 'true'. A sin we mortal programmers are committed to, when using a boolean that really are integers under the hood, is to check if the value is '0' or not. And we just ignore the others -- we know they are true. Unfortunately, Microsoft, in this case, couldn't help but further the cause of the already abused boolean to the next step. It suggested that, we'll give you true and false all right, but you know, you'd be better keeping an eye out if you're getting a '-1' as your function's return value. Now that's a royal screw-up. Booleans are booleans. There ain't no way no self-respecting programmer will check for a petty '-1'! Never.

Dear MS, change the type while you still can and save us the ignominy.

PPS: For the historically inclined check out George Boole after whom the type is named.

0 comments so far.

Something to say?