- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Because I have been working with Sybase, the RC Cola of Enterprise RDBMS, for the last 5 1/2 years. From writing code that I am ashamed of in order to get around driver bugs to accepting fundamental weaknesses in the DB engine itself (an INT identity column? Who would ever want that?!?), Sybase ASE has crushed my spirit.
Hey, you asked.
Luckily, we're in the middle of a full conversion to SQL Server.
Admin
You've just reminded me of the main reason I hate C. You are quite right about what you say about the boolean being set to a default value ( false in Java and C#). However, if the variable is defined in a local scope any attempt to use it without assigning a value will result in a compiler error (in Java and C#).
C# version 2 has come up with this syntax for nullable ints:
int? myInt;
This allows the full range of valid integer values and null to be assigned to the variable.
Admin
I prefer to use a float for my booleans:
Court martial in progress:
Judge: Are you positive about that statement?
Cmdr Zap Brannigan:Absolutely, positively, 99.9% sure!
Admin
FileNotFound is always a good candidate for any solution :-))
Admin
You know, that was the first thing that popped into my mind too...
Admin
Hey! You're back and as cryptic as ever!
Where've you been?
Admin
It's because a comparison to a null is null since null means there is no value and you cannot compare it to any other value. Ergo, null <> 'N' is null. null is not true, so the comparison fails.
Admin
Because NULL is not a value. It's an "absence of value". You'll notice that if you instead query for field=N, the NULL fields will also be excluded, "as though" they were equal to Y.
Notice the flaw in the "as though" logic. I'll use numbers, but remember that NULL is not a value, but the same logical principle applies:
values are:
1
2
3
where field = 1 -- ignores 2 and 3
where field = 2 -- ignores 1 and 3
This does not mean that 3 == 1, nor that 3 == 2. 3 is its own value. NULL is also its own "value", but in the sense that it has no value at all. In your example, just because NULL "values" are excluded from your query does not imply equality with the opposite.
Furthermore, note that you will get no return values if you query for field=NULL. This is because NULL != NULL. This is like saying "an unknown value == an unknown value", which is impossible to determine. You must use the "is null" keywords to detect NULL values.
Admin
I feel your pain. We used Sybase 11 (yes, 11.0.0!) for 4 or 5 years before moving to postgres 8.1. Nothing is more frustrating than working around Sybase's quirks and lack of features. Want row locking? Sorry, not in syb 11. Want ansi joins? Nope. Want CASE statements. Nope. Fulltext searching? Not in 11. functions? no way. isql sucks. Its ability to help you find performance bottlenecks sucks. Its lack of a varchar larger than 255 bytes sucks. Its requirement to eat a whole page for any non-null text column sucks. 2kb to store ('hi!') in a text column. Insane. IDENTITY BURNING SET FACTOR (aaargh!!). Ad nauseum.
We've generated some WTFs of our own out of necessity because of Sybase.
Admin
> the RC Cola of Enterprise RDBMS
Huh ?
Admin
You guys are in rare form today. My coworkers are staring at me now.
Admin
It also allows Nulls. (Just looked at it today)
--The funny thing is that my confirming image today is "Green"
Admin
Burn!
I like rereading the hitchhickers guide every now and again for some mindless comedy, but I generally agree with you.
Could you recommend some good comedy reading? The only other thing that comes to mind is Catch-22....
Admin
A distant third in market-share, and not likely to make up ground anytime soon.
Admin
And just be be certain everyone knows you're handy:
Admin
I remmber a long time ago I had to write my first application. You guys sound like you never did!
Admin
I understand all of that. Also note that consciously using the AsThough operator provided Tentative status to the subsequent statement I made as to the equality of 'N' and null.
Still, I wasn't querying for = N, I was querying for <> N.
So to keep with your int example, would make:
where field <> 1 -- should logically select 2 and 3
where field <> 2 -- should logically select 1 and 3
Yet, it doesn't, because null is apparently outside of reality as experienced by the DB. null can be compared for neither equality nor inequality with any value. Viz:
'N' = null -> false
'N' <> null -> false
yay
Admin
It seems to me that you are basically saying here "boolean has two values, therefore it can't have three", which seems to me to be a slightly silly way of arguing the point. It is entirely reasonable to have a boolean data type with three value (TRUE, FALSE, and NULL), and have definitions of the standard logical operations (AND, OR, and NOT) that allow for NULL valued inputs. Treating NULL as meaning "unknown value", we can define the normal operations by keeping the traditional definitions and adding NULLs, as shown in the following truth tables:
(If x is unknown, then !x is unknown)
(Short-circuit logic gives FALSE AND anything == FALSE, but TRUE AND NULL is unknown (i.e. NULL.) NULL AND NULL == NULL, obviiously.)
(Short-circuit logic at work again, TRUE OR anything == TRUE, but FALSE OR NULL is unknown.)
(I could remove the short-circuit logic and say that is any input is NULL then the output is NULL, a convention which can be used in asynchronous hardware and interpreting NULL as "data not ready yet". If you're bored, have a look at this paper on NULL-convention logic. I will be utterly amazed if my ascii-art tables come out correctly, but I tink I've got more chane than getting the forum software to do HTML tables right.)
Looking at the example expression you give, "x || !x", which is a tautology in traditional boolean logic, using the definitions I have just given this expression is TRUE when x is TRUE or when x is FALSE, and is NULL when x is NULL. There's nothing wrong with that, as long as you can cope with "x || !x" no longer being a tautology.
As for your other point, about what happens with NULLs and equality, that is just as much of an issue whether you're looking at a nullable boolean or a nullable integer or float or string or any other data type you care to name.
In short, other than the obvious semantic argument (that a boolean data type has two values so it's not allowed to have the possibility of a third) I see no reason why not to have a nullable (i.e. tri-valued) "boolean" type.
Admin
And clearly, I should have more faith in my ability to force the forums to do my bidding, and less faith in my ability to spell...
Admin
Admin
This normalisation malarky is vastly overrated[:D]
Admin
In SQL Server, bit column, values = 1, 0 or null.
Admin
Sorry, guess I should have read all of the posts before giving a reply!
Interestingly though, M$ SQL allows bit with null (1/0/null) though .net only allows boolean true/false (until v2 which allows bool?)
Admin
Once again, my knowledge has been incremented. By 11, no less.
Thanks, dailyWTF!
Admin
This kind of logic actually exists, is well researched and has been put to good use. Plese refer to this explanation of Fuzzy Logic: http://en.wikipedia.org/wiki/Fuzzy_logic.
Admin
Surely there's no useful difference between a type and a constrained domain? They're both just sets, and can both be used as the concrete representation of the abstract Boolean type. After all, what is a type except the set of values of that type?
Admin
I can't post a new WTF because I don't have access to the code, but sometimes binary logic get's pushed to some incredible highs.
This is in a appl from an insurance company, the field SEX there have not the usual 2 states but 3 (no, not the obvious Y (yes) answer to the SEX? question).
In this case sex can be:
'M' for "masculino" (male)
'F' for "feminino" (female)
but it can be 'H' (no, it's not what you're thinking).
'H' here is for 'homem' (men) [homo in Latin... LOL], in wich case 'M' will mean "mulher" (woman).
After a few records, they have to start looking into the name to know what a 'M' in sex means...
But then they upgraded the appl.
The 'H' is still supported, but now you can have 'E' for "empresa" (company), 'P' for "empresa publica" (state own company), 'B' for "banco" (bank)... and so on, and so on, and so on... I'd love to look into this code...
Admin
Using your logic (or lack thereof), no database that supports null could possibly have an integer data type, because null is not an integer.
Admin
To clarify this statement, you can have a boolean data type - a data type that supports the values true and false (not "true" and "false"). But it will have the additional possibility of null. I don't think that makes it any less a boolean field than an integer is an integer.
Admin
Don't use float! You need a lot of precission when dealing with booleans. Use double instead. :p
Admin
Port, Starboard and Lost?
Admin
Sadly, it seems you have no sense of humor. Bummer for you. A friendly word of advice; keep your low opinion of great works of modern literature to yourself. It generally is a bummer to learn that someone lacks an appreciation for humor.
Is there a funny book that you do like?