- 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
I kinda like being on the before-everyone-else-gets-it train.... But also a little guilty for those not in the know? TBH I probably would have expected an
IF...ELSEIF...END
cascade. :fa_thumbs_down: for originality?Admin
Nah, this is not exactly secret. If people aren't in the know, it's pretty much their own fault.
As for the article, itself, there are two explanations, either or some combination of both of which may be involved: 1. Incompetent people are incompetent. 2. Even competent people occasionally write dumb code.
Filed under: Of course, these explanations apply to most code snippets we see here.
Admin
If by working that means "not matching the spec" then ok....
That loop only returns true for
[100,199)
, the sentence prior before can only be interpreted as either[100,199]
(more likely the intention,) or(100,199)
.The code or the article? :cheapshot: :laughing:
<!-- [pjh@thinkpad tmp]$ cat foo.c #include <stdio.h> int foo(int telegramType){ int isPaymentType = 0; int i; for (i = 100; i < 199; i++) { if (telegramType == i) { isPaymentType = 1; } } return isPaymentType; } int main(void){ int tests[] = {0, 99, 100, 101, 198, 199, 200, -1}; int i=0; while (tests[i]>=0){ printf("%d = %d\n", tests[i], foo(tests[i])); ++i; } return 0; } [pjh@thinkpad tmp]$ ./foo 0 = 0 99 = 0 100 = 1 101 = 1 198 = 1 199 = 0 200 = 0 [pjh@thinkpad tmp]$ -->Admin
I'm not sure I see the WTF in the '<scr'+ 'ipt' one. Isn't it standard practice when you need to write a script from inside a <script> tag, because otherwise the HTML parser screws you over?
Of course, dynamically linking to a script from client side might be TRWTF.
Edit: I've broken Discourse! Corrected by replacing angular brackets with HTML enbreasties.
Admin
Actually, this is a well known problem in the html spec reference here
Admin
Most of which seem to be easily fixed by using a suitable library.
Alas, Hanzo'd by 17 minutes.
Admin
This topic is now listed. It will be displayed in topic lists.
Admin
On the last one, I'm afraid you're TRWTF for not understanding the problem.
Admin
.... PAULA!
slacking off on the job again is she....\
why i aughta make a version of SockBot to take her job from her and work a bilionty times better!
Admin
First off the bat...
I don't know what everyone is smoking, but this works (and works and works and works) for 100 through 198. 199 is feeling left out...
Addendum
You know what? Not only that, but this just doesn't feel....enterprisey enough. He needs to create an array and put his list of valid values in the array, then do foreach on the array elements. That way, it can be wrong in the enterprisey way.
Admin
It does have the dubious advantage of taking virtually the same amount of time whether the test succeeds or fails. [spoiler]And proving that the creator of the code is a nincompoop.[/spoiler]
Perhaps he should also fetch the valid values from a database?
Admin
This isn't enterprisey enough either.
(Please imagine the following code correctly indented and without line breaks, didn't find a way to copy and paste and don't have a keystroke simulator handy)
Or we could make use of a nested loop, for nested loops are cool
Admin
Sorry.. no sorry this has to be the biggest WTF piece of code.. ever..
You have to WANT IT to be that way to come up with this.. its like intentionally put the key in the safe to be sure not to lose it.
It hurts my brain just to find an excuse for the poor f*ck..
Admin
No it doesnt work, Coyne. It would work only if the telegramtype is 198. Otherwise, it'll always be false.
Admin
Now that's the TRUE enterprise spirit!
It's the easiest way to enable the user to extend the range by adding a few records to the database. Via a copied and custom-made web interface. No need to worry about SQL injection, of course.
Admin
Yes, but they should be stored as strings, complete with handrolled integer to string conversions
Admin
Why would we? Nobody will try to do that? i mean.. right?
Admin
The assignment to false is done before the loop only.
But you're right in that putting that into the loop would be even more enterprisey. Best like
Admin
I came across an if statement a while ago which tested
Admin
This way it's way more extendable.
Admin
Spot on.. my bad on the false thingy.. but your solution is more elegant.. enterprisey wise.. :smile:
Admin
To be or not to be. That is the condition..
Admin
As opposed to:
?
Admin
Actually, why not roll our own Number class, that way we aren't limited by the precision or ranges of built in types?
Admin
Done that. It's a lot of work to do right.
Admin
At least the actual if statement isn't totally redundant there, only two of the conditions.
Admin
In all seriousness, I completely agree.
Although in the concept of making things as wtf-y as possible, I think it's 100% necessary to do. Like that time my manager wanted me to write my own list implementation, just in case there was a bug in std::list.
Admin
That's easy:
Admin
Boring, everyone goes for the Hamlet conditional. Inspired by:
Let's try Macbeth instead!
Admin
we can speed up the loop with a clever:
telegramType = telegramType | telegramType == i;
Still.....
Admin
FTFY…
Admin
I wonder if, in the world of failed "4th and 5th generation programming languages", there is one that might have generated that code. Having seen the dumb queries that Access generates, nothing would surprise me.
Admin
Or, for brevity and/or obscurity
isPaymentType |= telegramType == i
if your language supports it?Admin
create table telegramtype { smallint id NOT NULL PRIMARY KEY, nvarchar(254) id_description NOT NULL, boolean is_telegramtype DEFAULT false, creationdate datetime DEFAULT CURRENT_TIMESTAMP } INSERT INTO telegramtype(id,id_description,is_telegramtype)VALUES(100, 'some significance',true); INSERT INTO telegramtype(id, id_description, is_telegramtype)VALUES(101,'a different significance',true); /code omitted/ .....("SELECT is_telegramtype FROM telegramtype WHERE id = ?",id);
This is properly enterprisy because it is necessary to test for null as well as false. I am sure a decent system architect could get the telegramtype table up to a lot more fields. How about a modification date and the creator username?
All added value....
Admin
Not necessarily more fields, but a proper "audit trail" table might do the trick
Admin
Well, you know, this also checks that telegramType is an integer in the range. Just checking for >= 100 and <=199 would also include values like 123.5. And we can't be having that.
Admin
Yes, I wasn't thinking Enterprise enough. Also we should log all queries on the table to check for invalid telegramtypes that might need to be added later.
Admin
Okay, please correct me if I'm wrong. I'm only a moderate programmer, but I learn quickly. I've been hounding my international team for constantly writing:
Seriously, if it's true, just leave it as the conditional!
I would hope the compiler optimizes that out, but it just looks weird to me.
Admin
Not enough XML fields in that table...
Admin
There are actually good reasons for this insanity! It all has to do with how browsers parse and interpret the JS. Mostly IE's fault, really...
Suppose you're writing a 3rd party script for pages, a tracking pixel
document.write makes sure your next script is injected immediately after current script -- instead of parsing the rest of HTML beforehand (synchronous JS import). It's a bad idea to use this since it will make the page load slower, but there it is.
Since it's a tracking pixel, you don't want it firing unless the user is actually looking at the page. If you leave the string unbroken,
'<script src="...'
-- the browser's prefetcher will download the script before your inline script is executed!...so you curse your job for making you create a tracking pixel, and do what hundreds of devs had done before....
Admin
Am I the only one to see what's wrong?
FTFY.
Admin
Also I missed out a suitably weird collation and a table descriptor.I'm just not trying, am I?
Admin
But now it won't run in more or less constant time, which may be a desired feature.
Admin
I can imagine having to do it this way if telegramType is a class that implements operator== but not operator< or operator>, but even so it would be easier to fix this (possibly imagined by me) class instead.
<!-- sorry for replying to your post, @pjh, you just happened to be the first to quote the code in question. -->Admin
But you need to list them all! Otherwise how do you show that you missed 142?
Admin
It's possible (thought pretty bloody unlikely) that telegramType is an instance of some sort of custom class that defines an equality test with integers, but isn't sortable. I could see it being done for some kind of enumeration class for which greater-than and less-than operators don't make sense. Of course, if that's what this is TRWTF is why such an oddball class doesn't have its own methods for figuring out if it's a payment type.
Admin
A lot of languages will implicitly convert any expression to a boolean, so "if (condition)" will evaluate as true if condition is a non-zero integer, a non-empty string, etc. That's usually what you want, but maybe you actually want to know if the conditional is actually a boolean with the value true and you want it return false for any normally truthy value that's not strictly boolean true. If that's the case though, you probably ought to be explicit about it. "if (type(conditional) == boolean && conditional == true) {...}"
Admin
Oh dear, you're right! There must be some way to improve it?
Admin
No, it only changes isPaymenType if it finds a match. So it will work for any of the 99 values of i...it's just that the values of i that are tested only go from 100 to 198.
I think that makes it worse...if that were possible....
Well, of course they tested it that way. Must account for FILE_NOT_FOUND.
You're right...that's the way it really should be done. That's the true enterprisey.
Admin
//document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + url + '"></sc'+'ript>');
This isn't really a WTF. If you look at the horrors of SGML, CDATA, Text Nodes, XHTML, HTML and browsers especially historically, then there are good reasons to do something like the above.
That workaround used to be extremely common but I believe now people create and insert proper DOM elements more often.
That might even be my code. I did the same before myself as a quick fix when something broke on a dodgy as hell website I inherited.
If I vaguely remember, the copy pasta from GOOGLE for analytics and other various providers would work this way.
Even today on Google's guide you'll see...
https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingOverview
They use something a little different but basically the same.
Most today will now use a little minified piece of a pure DOM implementation and with support for nice things like //urls for inheriting protocols, etc. Between five to ten years ago, we didn't have such nice things.