Comment On Online Offline

Ben S. submitted the following piece of SQL. It's only a few lines of code, but both the variable names and values are a bit... curious. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Online Offline

2007-01-23 09:06 • by fennec
Easy enough.

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline

(forgive my weak variables-in-SQL-fu if there's anything wrong in that syntax.)

speaking of wtf's

2007-01-23 09:13 • by the forum software is really really bad (unregistered)
how about this forum software?

Re: Online Offline

2007-01-23 09:26 • by Dave (not that one) (unregistered)
I like the way both variables are declared varchar(100)just in case you want them to be 'YYYYYYYYEEEEEEESSSSSSS' or something like that. Why send a bit when 100 bytes will work? Unfortunately, he didn't use nvarchar(100) so this is clearly not suitable for international applications.

Re: Online Offline

2007-01-23 09:31 • by pFOOTi (unregistered)
For the first time in ages, I stared at the code and actually said "What the F!"

Re: Online Offline

2007-01-23 09:31 • by denz (unregistered)
"Both" is obviously the authors implementation of the third boolean value...

Re: Online Offline

2007-01-23 09:35 • by CDarklock
Theory.

The application was written with separate variables for Online and Offline, because the author was stupid. Thousands of features were added which depend on having these two variables.

A smarter person said "these are the same variable" and created the OnlineOffline variable. For compatibility, he used it to set the original two variables, so he didn't have to fix the old code.

Along comes someone either stupid or brilliant (or more likely brillant), who says "there's actually a third mode which is both online AND offline" and creates the "Both" value.

And of course, a rarely-used function deep in the bowels of the system sets the OnlineOffline variable to "FileNotFound", which breaks everything.

Re: Online Offline

2007-01-23 09:40 • by dhromed
112806 in reply to 112804
It is a case of enmeshed double-backloop logic, in which the truth of the opposite does not necessarily imply the falsness of the other, nor does it negate the other way around if the same becomes truly false.

See? Simple!

It's that higher system Godel implied which can express the contradictory/recursive/closured logic found in our current system.

Re: Online Offline

2007-01-23 09:42 • by Slacker (unregistered)
Welcome to the world of quantum computing, where a system can be online and offline at the same time. Until you measure it that is.

Re: Online Offline

2007-01-23 09:47 • by Jax (unregistered)
-True
-False
-FileNotFound

The CAPTCHA is "ewww" which is exactly my thoughts about this section of code.

Re: Online Offline

2007-01-23 09:53 • by Pol (unregistered)
I'm confused how someone can be online and offline at the smae time!

Re: Online Offline

2007-01-23 09:55 • by Unomi (unregistered)
The WTF is actually:

He states 'Both':

Online = 'Y' // Thus it's online
Offline = 'N' // Thus it's NOT offline

He states 'Y':
Online = 'Y' // Thus it's online

He states 'Y':
Offline = 'N' // Thus it's NOT offline

So.... It is ALWAYS online!!!! There is nothing to check, since the answer is always the same!!!! Simply saying:

Online = TRUE

Would be the only answer possible and saves a lot of lines.

Captcha: howdy (I'm fine thank you)

- Unomi -

Re: Online Offline

2007-01-23 09:56 • by cout
112812 in reply to 112809
Pol:
I'm confused how someone can be online and offline at the smae time!


I'm confused about someone can be online and not be offline. Sounds like the Matrix to me.

Re: Online Offline

2007-01-23 10:00 • by Unomi (unregistered)
112813 in reply to 112811
Unomi:
The WTF is actually:
He states 'Y':
Offline = 'N' // Thus it's NOT offline


Should be:

He states 'N':
Offline = 'N' // Thus it's NOT offline

Sorry about that...

Captcha: ewww (I already said sorry, 'K??)

- Unomi -

Re: Online Offline

2007-01-23 10:00 • by TheJasper
112814 in reply to 112812
cout:


I'm confused about someone can be online and not be offline. Sounds like the Matrix to me.


unfortunately, no one can be told what offline is.


I suggest the blue pill.

Re: Online Offline

2007-01-23 10:00 • by DigitalLogic
FOOLS!!! It's simple ternary logic. Using varchar as the data type to represent it is the real WTF.

Captcha: doom (you didn't need to tell me)

Re: Online Offline

2007-01-23 10:05 • by DDT (unregistered)
112816 in reply to 112797
fennec:
Easy enough.

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline

(forgive my weak variables-in-SQL-fu if there's anything wrong in that syntax.)


I think you got it.

Taking snippets out of context can cause them to not make any sense, but in context you understand the code easily.

Sure, in this particular case, it may not be a perfect solution, but it works quite well if you think about it.

Re: Online Offline

2007-01-23 10:09 • by Matt (unregistered)
112818 in reply to 112816
It's obvious that this is for a high availability system that must always be online.

Re: Online Offline

2007-01-23 10:15 • by Trash (unregistered)
I dont know this code context...but..I hope those values arent being used later on (umm..then, what's the point to initialize them?) because the possible values of @Online and @Offline are: ('Y','N','').

So, it's a FileNotFound inside a FileNotFound.In fact, if @OnlineOffline='Both',at least it gets "consistent" values for @Online and @Offline...

Re: Online Offline

2007-01-23 10:16 • by webdev101 (unregistered)
112821 in reply to 112804
CDarklock:
Theory.

The application was written with separate variables for Online and Offline, because the author was stupid. Thousands of features were added which depend on having these two variables.

A smarter person said "these are the same variable" and created the OnlineOffline variable. For compatibility, he used it to set the original two variables, so he didn't have to fix the old code.

Along comes someone either stupid or brilliant (or more likely brillant), who says "there's actually a third mode which is both online AND offline" and creates the "Both" value.

And of course, a rarely-used function deep in the bowels of the system sets the OnlineOffline variable to "FileNotFound", which breaks everything.


As far as I can see the @Online variable will always have one of the two values null or 'Y' and @Offline variable will always have one of the two values null or 'N'. I am not sure what he does with the values.

Captcha: smile :)

Re: Online Offline

2007-01-23 10:18 • by Thimp (unregistered)
112822 in reply to 112806
Yes, I believe you are right, it's a godel thingy.
Still .. if you are neither online, nor offline, and i accept that is follows that you have to be both, I still dont accept you are either ...

At least that's wat my tortoise named Achilles claimes

Re: Online Offline

2007-01-23 10:31 • by Pablo (unregistered)
That is NOT a piece of SQL

Re: Online Offline

2007-01-23 10:32 • by Rich (unregistered)
112826 in reply to 112807
Welcome to the world of quantum computing, where a system can be online and offline at the same time. Until you measure it that is.


So we're checking online/offline status of Schroedinger's cat?

captcha: sanitarium

Re: Online Offline

2007-01-23 10:38 • by akatherder
112828 in reply to 112816
DDT:
fennec:
Easy enough.

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline

(forgive my weak variables-in-SQL-fu if there's anything wrong in that syntax.)


I think you got it.

Taking snippets out of context can cause them to not make any sense, but in context you understand the code easily.

Sure, in this particular case, it may not be a perfect solution, but it works quite well if you think about it.


That still begs the questions why you need to check for onlinestatus. You are either online or you aren't. That would make these two SQL statements identical in functionality:

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline //always returns all records

SELECT * FROM some_user_table //always returns all records

Re: Online Offline

2007-01-23 10:40 • by DDT (unregistered)
112829 in reply to 112826
Ok, let me spell it out for you all.

Imagine this :


CREATE PROCEDURE GetUsersByOnlineStatus
@OnlineOffline VARCHAR(100)
AS

DECLARE @Online varchar(100)
DECLARE @Offline varchar(100)

IF @OnlineOffline = 'Both'
BEGIN
SET @Online = 'Y'
SET @Offline = 'N'
END

IF @OnlineOffline = 'Y'
SET @Online = 'Y'

IF @OnlineOffline = 'N'
SET @Offline = 'N'


SELECT *
FROM users
WHERE
isOnline = @Online OR
isOnline = @Offline

END


So when you call GetUsersByOnlineStatus with 'Y' you get online users, with 'N' you get offline users, and with 'Both' you get... both.

Re: Online Offline

2007-01-23 10:42 • by DF5 (unregistered)
112830 in reply to 112824
Oh! That's the WTF.

quake - ahh, those were the days.

Re: Online Offline

2007-01-23 10:50 • by Pon (unregistered)
112831 in reply to 112828
akatherder:


That still begs the questions why you need to check for onlinestatus. You are either online or you aren't. That would make these two SQL statements identical in functionality:

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline //always returns all records

SELECT * FROM some_user_table //always returns all records


Take MSN for example. You can be online (i.e. signed in), and appear offline at the same time. So, you would be both online and offline... Depending on your frame of reference ;)

CAPTCHA: relativity ftw

Re: Online Offline

2007-01-23 10:55 • by maweki
We should consider, that it could be online or not in about 6,6*10^240 Ways. So, about 256^100. And we also consider that it is the same with offline (or not).

I want to see anybody working out that much ways of beeing online or offline ;)

Uh.

2007-01-23 10:58 • by sammybaby
I think this SQL fragment is the equivalent of Abbot & Costello's "Who's on First" skit. Or maybe that scene from Airplane 2: "Unger, didn't you serve under Oveur in the Air Force?" "Not directly. Technically, Dunn was under Oveur and I was under Dunn." Et cetera.

Re: Online Offline

2007-01-23 11:01 • by Asd (unregistered)
112834 in reply to 112829
DDT:
Ok, let me spell it out for you all.
So when you call GetUsersByOnlineStatus with 'Y' you get online users, with 'N' you get offline users, and with 'Both' you get... both.


That actually is kind off clever, in a really stupid way.

Re: Online Offline

2007-01-23 11:10 • by Andy (unregistered)
It looks like the @OnlineOffline field is used to indicate which of the other two fields, or both, is to be set.

The code is called when the status becomes online.

There should be a similar block of code when the status becomes offline.


captcha: Tesla. But give the credit to Edison since he had the business case right.

Re: Online Offline

2007-01-23 11:23 • by Anonymous (unregistered)
112838 in reply to 112836
Is like Womens.
Womens are online. Only not available to YOU.

Re: Online Offline

2007-01-23 11:24 • by s0be
112839 in reply to 112829
DDT:
Ok, let me spell it out for you all.

Imagine this :


CREATE PROCEDURE GetUsersByOnlineStatus
@OnlineOffline VARCHAR(100)
AS

DECLARE @Online varchar(100)
DECLARE @Offline varchar(100)

IF @OnlineOffline = 'Both'
BEGIN
SET @Online = 'Y'
SET @Offline = 'N'
END

IF @OnlineOffline = 'Y'
SET @Online = 'Y'

IF @OnlineOffline = 'N'
SET @Offline = 'N'


SELECT *
FROM users
WHERE
isOnline = @Online OR
isOnline = @Offline

END


So when you call GetUsersByOnlineStatus with 'Y' you get online users, with 'N' you get offline users, and with 'Both' you get... both.


Rather than crazy logic like that, why not just roll the checks into the sql query:



CREATE PROCEDURE GetUsersByOnlineStatus
@OnlineOffline VARCHAR(100)
AS
BEGIN

SELECT *
FROM users
WHERE
@OnlineOffline = 'Both' OR
(
( @OnlineOffLine = 'Y' and isOnline = 'Y' )
OR
( @OnlineOffline = 'N' and isOnline = 'N' )
)

END

Re: Online Offline

2007-01-23 11:29 • by DDT (unregistered)
112840 in reply to 112839
s0be:
Rather than crazy logic like that, why not just roll the checks into the sql query:



CREATE PROCEDURE GetUsersByOnlineStatus
@OnlineOffline VARCHAR(100)
AS
BEGIN

SELECT *
FROM users
WHERE
@OnlineOffline = 'Both' OR
(
( @OnlineOffLine = 'Y' and isOnline = 'Y' )
OR
( @OnlineOffline = 'N' and isOnline = 'N' )
)

END


Well if you want to be picky...


WHERE
@OnlineOffline = 'Both' OR
@OnlineOffLine = isOnline


And while we're at it, isOnline should be a BIT to begin with.

As I said in my first post, the solution is less than perfect, but it works pretty well. It is clearly not a WTF.

Re: Online Offline

2007-01-23 11:58 • by snoofle
112849 in reply to 112809
Pol:
I'm confused how someone can be online and offline at the smae time!

what about systems that allow multiple connections? One could be online, the other offline; Of course, then an intelligent person would have 2 flags: loggedInChannelOne and loggedInChannel2, but hey, what do I know?

Re: Online Offline

2007-01-23 12:18 • by DM (unregistered)
112857 in reply to 112802
pFOOTi:
For the first time in ages, I stared at the code and actually said "What the F!"



For the millionth time....I laughed my @$$ off.

Re: Online Offline

2007-01-23 12:20 • by jKent (unregistered)
112858 in reply to 112822
Thimp:
Yes, I believe you are right, it's a godel thingy.
Still .. if you are neither online, nor offline, and i accept that is follows that you have to be both, I still dont accept you are either ...

At least that's wat my tortoise named Achilles claimes


It's really quite simple: as long as you accept that
x) you have to be both online and offline, and
y) (a AND b) IMPLIES a (similarly, (a AND b) IMPLIES b)
then because ((p IMPLIES q) AND p) IMPLIES q
z) you have to be online (similarly, you have to be offline)

Finally, I have a record for your tortise to listen to...

Re: Online Offline

2007-01-23 12:22 • by Harry (unregistered)
An intelligent person would just keep a list of places where it was online.

The real question is what the complete list of OnlineOffline states are. Both, None, Tuesday etc.

Re: Online Offline

2007-01-23 12:29 • by s0be
112862 in reply to 112840
DDT:
Well if you want to be picky...


WHERE
@OnlineOffline = 'Both' OR
@OnlineOffLine = isOnline


And while we're at it, isOnline should be a BIT to begin with.

As I said in my first post, the solution is less than perfect, but it works pretty well. It is clearly not a WTF.


The reason I did it my way rather than your way is yours does not deal with @OnlineOffline and isOnline both being the same, invalid value. Both could be 'Space Shuttle' and it would be returned. Doing the @a = 'Y' and @b = 'Y' would be the same as @a = 'Y' and @a = @b, with one fewer variable references. I don't have the DB schema, so I don't know whether isOnline would be enum'd into only Y or N values, in which case yours would be the same effect. But for a non enum'd varchar(100), @OnlineOffLine = isOnline could return false positives.

Re: Online Offline

2007-01-23 12:33 • by savar
112863 in reply to 112829
DDT:
Ok, let me spell it out for you all.

So when you call GetUsersByOnlineStatus with 'Y' you get online users, with 'N' you get offline users, and with 'Both' you get... both.


But...

isOnline = null

is not valid SQL, is it?

Re: Online Offline

2007-01-23 12:40 • by twp (unregistered)
112865 in reply to 112840
DDT:
As I said in my first post, the solution is less than perfect, but it works pretty well. It is clearly not a WTF.


Oh, I disagree. The code shouldn't have to be a failure in order to qualify as a WTF. A function or code snippet can be technically exactly functional, may even be a smart solution to a problem in its context, and yet generate that "WTF?" reaction upon reading. I think this is an excellent example: when I saw the post, I could not possibly imagine what the author had intended, and yet you and fennec came up with an entirely plausible explanation. Kudos to you both.

Re: Online Offline

2007-01-23 12:41 • by Ben S. (unregistered)
Hehe, man you guys have WAY too much time on your hands.

Actually it is not regarding users online, it is regarding the status of some piece of data.

Some pieces can be online, others offline. Some users want to see both of these.

The WTF is really that they used a stupid dropdown to provide a parameter for a reporting services stored procedure.

The naming is totally not logical.

Would you like coffe or tea? Yes please!

Re: Online Offline

2007-01-23 12:46 • by emurphy
112869 in reply to 112840
DDT:

And while we're at it, isOnline should be a BIT to begin with.


isOnline should be a BOOLEAN, but (as of SQL:2003) BOOLEAN is optional because people keep arguing about how NULL and/or UNKNOWN (sound familiar?) should be handled. PostgreSQL gets it right (I wish I could use PostgreSQL at work), everyone else has an ugly-but-good-enough equivalent (BIT is MS SQL's), which makes Joe Celko roll in his grave (despite the minor detail that he isn't actually dead).

Re: Online Offline

2007-01-23 14:04 • by GrandmasterB (unregistered)
112888 in reply to 112800
Dave (not that one):
I like the way both variables are declared varchar(100)just in case you want them to be 'YYYYYYYYEEEEEEESSSSSSS' or something like that. Why send a bit when 100 bytes will work? Unfortunately, he didn't use nvarchar(100) so this is clearly not suitable for international applications.


Its a 'varchar', so the db will store _up_ to 100 bytes, depending on what was in the field. Unlike a 'char' type, which would always store exactly 100 bytes, regardless of the field's value. So that in of itself isnt a big deal.

Re: Online Offline

2007-01-23 14:14 • by Twm (unregistered)
112892 in reply to 112803
"Both" I thought I was witnessing a Qubit!

Re: Online Offline

2007-01-23 15:24 • by justin (unregistered)
Out of context or not, here's the results of the three possible(?) values off OnlineOffline:

when OnlineOffline = 'Both'
OnlineOffline Online Offline
------------- ------ -------
Both Y N

when OnlineOffline = 'Y'

OnlineOffline Online Offline
------------- ------ -------
Y Y NULL

when OnlineOffline = 'N'

OnlineOffline Online Offline
------------- ------ -------
N NULL N

I'm guessing there's some appendix code here somewhere. Offline is either 'N' or null, Online is either 'Y' or null. I haven't even touched on the fact that we don't see what may have happened to OnlineOffline prior to this snippet, and assuming it's also varchar(100), the value could be just about anything. Hm, what happens then?

OnlineOffline Online Offline
-------------------- ------ -------
Just About Anything NULL NULL

WTF?

Re: Online Offline

2007-01-23 15:59 • by gwenhwyfaer
112924 in reply to 112863
As I understand it, you can compare things with NULL - it's just that the result of such a comparison is always false. So
SELECT 1 WHERE 1 = null
and
SELECT 1 WHERE 1 != null

should both return 0 rows.

Re: Online Offline

2007-01-23 16:14 • by Gloves (unregistered)
the real wtf is everybody discussing this poor code...

Another wtf is my CAPTCHA test right now... YUMMY!

Gloves, remember and keep for yourself: gloves!
http://thedailywtf.com/Articles/The_Complicator's_Gloves.aspx

screenshot:
http://img256.imageshack.us/img256/8049/yummy6pt.jpg

Re: Online Offline

2007-01-23 16:42 • by fennec
112938 in reply to 112828
akatherder:
DDT:
fennec:
Easy enough.

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline

(forgive my weak variables-in-SQL-fu if there's anything wrong in that syntax.)


I think you got it.

Taking snippets out of context can cause them to not make any sense, but in context you understand the code easily.

Sure, in this particular case, it may not be a perfect solution, but it works quite well if you think about it.


That still begs the questions why you need to check for onlinestatus. You are either online or you aren't. That would make these two SQL statements identical in functionality:

SELECT * FROM some_user_table WHERE onlinestatus = @Online OR onlinestatus = @Offline //always returns all records

SELECT * FROM some_user_table //always returns all records


Mmm, nope. Wrong. You're thinking of @Online and @Offline as the actual state. They're not. They're variables, string variables (varchar) and the way they're set out, it works like this:
@OnlineOffline = 'Y'
=> @Online = 'Y'
=> @Offline = null (I think)
=> SELECT * FROM some_user_table WHERE onlinestatus = 'Y' OR onlinestatus = null

@OnlineOffline = 'N'
=> @Online = null
=> @Offline = 'N'
=> SELECT * FROM some_user_table WHERE onlinestatus = null OR onlinestatus = 'N'



@OnlineOffline = 'Both'
=> @Online = 'Y'
=> @Offline = 'N'
=> SELECT * FROM some_user_table WHERE onlinestatus = 'Y' OR onlinestatus = 'N'

--- this one, and only this one, is equivalent to SELECT * FROM some_user_table

- Of course this does assume a couple of things about the database (mostly that comparing to the default value / null of the variable is okay to do and doesn't return any extraneous rows - presumably the column IS NOT NULL - and 'Y'/'N' are the only values). And it's a fairly lousy way to do things, all told, really. But anywho. :)

Re: Online Offline

2007-01-23 17:22 • by RockinJack (unregistered)
Obviously, the code is an attempt at world domination.

Re: Online Offline

2007-01-23 17:47 • by JGM (unregistered)
112953 in reply to 112826
Nah, just Schroedinger's Mouse in this case.
« PrevPage 1 | Page 2Next »

Add Comment