Click here to Skip to main content
15,887,267 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Do you not understand booleans? Pin
mostlyharmless196414-Dec-11 3:15
mostlyharmless196414-Dec-11 3:15 
GeneralRe: Do you not understand booleans? Pin
Fabio Franco14-Dec-11 4:06
professionalFabio Franco14-Dec-11 4:06 
GeneralRe: Do you not understand booleans? Pin
alanevans6-Dec-11 2:58
alanevans6-Dec-11 2:58 
GeneralRe: Do you not understand booleans? Pin
CDP18026-Dec-11 3:26
CDP18026-Dec-11 3:26 
GeneralRe: Do you not understand booleans? Pin
alanevans6-Dec-11 3:54
alanevans6-Dec-11 3:54 
GeneralRe: Do you not understand booleans? Pin
CDP18026-Dec-11 4:16
CDP18026-Dec-11 4:16 
GeneralRe: Do you not understand booleans? Pin
alanevans6-Dec-11 4:55
alanevans6-Dec-11 4:55 
GeneralRe: Do you not understand booleans? Pin
CDP18026-Dec-11 5:29
CDP18026-Dec-11 5:29 
The simple answer: I would, for example, use the Win32 type BOOL. I can safely compare any variable of the type BOOL to the definitions of TRUE or FALSE and that is what I would do. Trying to compare any variable of another type with TRUE or FALSE would be asking for trouble. Also, I would not use other types to express boolean values, even if C/C++ allows this, so that there is never any need to compare them to TRUE or FALSE


BOOL b = TRUE;
int i = 1;

if(i == TRUE)
{
    // TRUE may be defined in numerous ways, so this is likely not to work
    // I would simply not use an int (or other types) to express boolean values.
}

if(b)
{
    // It would be logical to assume that this works, but who knows how TRUE has
    // really been defined. In any case, we should not have to know and that
    // is why I avoid it.
}

if(b == 1)
{
    // TRUE (the value of b) may be defined in numerous ways, so this is likely not to work
}

if(b == i)
{
    // Very bad. Even if the definition of BOOL is based on int, it's unsafe to assume that this is so. And again
    // we should not have to care about how TRUE or FALSE are defined.
}

if(b == TRUE)
{
    // No problem here. Explicitly comparing a BOOL variable with TRUE or FALSE seems to be the only safe way.
}


Coming back to managed languages where a boolean type exists: Here no other type can be used to express boolean
values, so explicitely comparing to true or false is not needed. I guess it's simply a habit I have carried over
without thinking much about it.
And from the clouds a mighty voice spoke:
"Smile and be happy, for it could come worse!"

And I smiled and was happy
And it came worse.



GeneralRe: Do you not understand booleans? Pin
PIEBALDconsult9-Dec-11 1:55
mvePIEBALDconsult9-Dec-11 1:55 
GeneralRe: Do you not understand booleans? Pin
OriginalGriff9-Dec-11 2:50
mveOriginalGriff9-Dec-11 2:50 
GeneralRe: Do you not understand booleans? Pin
BobJanova9-Dec-11 4:15
BobJanova9-Dec-11 4:15 
JokeRe: Do you not understand booleans? Pin
harold aptroot9-Dec-11 4:27
harold aptroot9-Dec-11 4:27 
GeneralRe: Do you not understand booleans? Pin
Rob Grainger11-Dec-11 23:32
Rob Grainger11-Dec-11 23:32 
GeneralRe: Do you not understand booleans? Pin
CDP180211-Dec-11 23:46
CDP180211-Dec-11 23:46 
GeneralRe: Do you not understand booleans? Pin
BobJanova13-Dec-11 23:41
BobJanova13-Dec-11 23:41 
GeneralRe: Do you not understand booleans? Pin
Kabwla.Phone12-Dec-11 23:48
Kabwla.Phone12-Dec-11 23:48 
GeneralRe: Do you not understand booleans? Pin
Addy Tas13-Dec-11 12:41
Addy Tas13-Dec-11 12:41 
GeneralRe: Do you not understand booleans? Pin
PIEBALDconsult9-Dec-11 2:00
mvePIEBALDconsult9-Dec-11 2:00 
GeneralRe: Do you not understand booleans? Pin
CDP18029-Dec-11 2:34
CDP18029-Dec-11 2:34 
GeneralRe: Do you not understand booleans? Pin
harold aptroot9-Dec-11 3:00
harold aptroot9-Dec-11 3:00 
GeneralRe: Do you not understand booleans? Pin
CDP18029-Dec-11 3:33
CDP18029-Dec-11 3:33 
GeneralRe: Do you not understand booleans? Pin
harold aptroot9-Dec-11 3:42
harold aptroot9-Dec-11 3:42 
GeneralRe: Do you not understand booleans? Pin
CDP18029-Dec-11 4:24
CDP18029-Dec-11 4:24 
GeneralRe: Do you not understand booleans? Pin
BobJanova9-Dec-11 4:20
BobJanova9-Dec-11 4:20 
GeneralRe: Do you not understand booleans? Pin
Stefan_Lang12-Dec-11 5:40
Stefan_Lang12-Dec-11 5:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.