- 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
ROTFLMAO!!! I love it. His code WAS technically correct...he had the SWITCH [:P]
Admin
There are poeple who persist in the belief that elseif structures are "just better".
if elseif elseif else [:@]
Admin
Admin
That... is fantastic.
Admin
No because if the result of $_GET['action'] is not one of the specified values it should default to $_GET['id'] which your example won't do.
Admin
It once more goes to show that you have to be careful with what you ask for: you might get it...
Admin
I'm sure that's easier to fix, and with a lot less possible typing errors and a lot more flexibility.
Admin
Might wanna wrap the switch block in an
if(true)
block just to be sure.
Admin
how about
switch ($_GET["action"])
{
case "delete" :
case "edit" :
case "save" :
$editid = (int) $_GET[$_GET["action"]];
break;
default :
$editid = (int) $_GET["id"];
}
Admin
No, no, no. That is bad code. For God's sake, man: if you want to do something, do it right:
if(IsTrue(true))
Admin
I suggest that that transitioning programmer is more than a little clueless. The first language that I programmed in did not have switch/case either. When I first read about it, it was simple to see how it worked and simple to use.
Sincerely,
Gene Wirchenko
Admin
But Gene, you're so much smarter than everyone else. As evidenced by the fact that you sign all your posts even though your username is displayed prominently next to your posts. I mean, that's operating on a whole different plane of existence and thought. It's not fair to use you as a point of reference as you are so obviously superior to everyone else.
Sincerely,
Richard Nixon
Admin
$editid = (int) (isset($_GET[$_GET['action']]) ? $_GET[$_GET['action']] : $_GET['id']);
Admin
I don't know what isset but if it is just checking the hash for a key existance that that's wrong, suppose if $_GET['action'] = 'blah' for example you would still want to run $_GET['id']
Admin
or, if it really has to be one of those values you could $values = array('delete', 'edit', 'save')
then check if is_in_array() (or whatever that function is, I'm too lazy to look it up (a great virtue))
Admin
Wow. Almost unbelievable. And to think I once belonged to the camp that said that if there was sufficient test coverage, the code would naturally be fairly good. No way for a unit test to sniff this sucker out - and yet... the mind reels at how anyone could invent this. A classic WTF.
public String getMessage() {
String result = "";
if (isTrue(new Paula().getPaula().equals("Brillant")) {
switch ("Brillant'.hashCode()) {
default:
for (int i = 0; i < 3; i++) {
switch(i) :
case 0: result = result + "W"; break;
case 1: result = result + "T"; break;
case 2: result = result + "F"; break;
}
}
}
return result;
}
Admin
You picky bastard...
$editid = (int) ((isset($_GET[$_GET['action']]) && (in_array($_GET['action'], array('save', 'edit', 'delete'))) ? $_GET[$_GET['action']] : $_GET['id']);
Admin
syntax error ... wtf
Admin
Or, better:
if( ! checkFalse( new Boolean( true ) ) )
http://www.thedailywtf.com/forums/39325/ShowPost.aspx
Admin
Apart from the lesser problem (the utterly nonsense switch usage - it is weird that the developer didn't know how to use it, but at least that is a thing that can be easily fixed by pointing the dev at the right page of php manual), this shows the general bad coding approach of the php developers - not enough input validation, and hoping that everyone will have error_reporting(E_NONE) turned off, and when you try to do the right thing, and check the site with error_reporting(E_ALL), and don't include, let's say, that ID parameter, just look at the page crawling with warnings and notices.
It is actually quite frightening how many php projects fall to pieces with error reporting turned on to the max (or rely on one or another specific php configuration settings; register_globals anyone? or fall when magic_quotes_gpc are turned off).
Admin
I don't know what isset but if it is just checking the hash for a key existance that that's wrong, suppose if $_GET['action'] = 'blah' for example you would still want to run $_GET['id']
You picky bastard...
$editid = (int) ((isset($_GET[$_GET['action']]) && (in_array($_GET['action'], array('save', 'edit', 'delete'))) ? $_GET[$_GET['action']] : $_GET['id']);
Admin
that was SUPPOSED to be a quote and a "And somehow this is better?!?!?" bah.
Admin
Way to use the forum! ;)
Yeah, it's better. It's all on one simple line. In real life I'd probably break it out onto seperate lines for readability (but still one statement). TMTOWTDI.
Admin
I hope that you don't actually make ternary statements that look like that. It's error prone and a maintenance nightmare, not to mention it gains you nothing over using a couple of IF statements.
Admin
You're just not used to being around good programmers. Consice statements are nothing to fear. It saves space, and any programmer of any worth can easily tell what's going on there. I'm not doing anything unusual here. Even if I were afraid of dorkface down the hall who doesn't know how to code, I could, like I said, break it out into several lines and even /* comment */ the sections. It DOES add value to the code, as it's simply a matter of two conditions instead of multiple ifs, which you'd probably want me to deeply nest.
Admin
There's another WTF here, but this one is subtle:
Instead of
.../stupid.php?action=save&save=69
.../stupid.php?action=edit&edit=69
.../stupid.php?action=delete&delete=69
[...]
Shouldn't be:
.../clever.php?action=save&id=69
.../clever.php?action=edit&id=69
.../clever.php?action=delete&id=69
I know this is nitpicky, but makes hell lot easier to parse.
I'm having similar problems where I'm working: very similar structures, with the same operations and field types, but the names of the fields are completely different! I waste most of the time COPY-PASTING operations and replacing with the proper fields, which is, naturally, a error-prone approach... but its a giant system and I can't change it. =/
Admin
Hah!
This code made me laugh out loud. It's hideous but technically correct!
BRILLANT! [H]
Admin
OneFactor's code, I meant, although the WTF had the same effect.
Admin
Aren't you the Richard Nixon from Futurama?
Admin
Who needs validation when you can provide your customers with superb added value like the following:
http://www.bootbarn.com/multiview/men/bootsshoes/view/view/0/48/popular/All?size=12.0&width=Narrow
I.e. ENTERTAINMENT!
Admin
In case bootbarn fixes their site, here's what happens when you go to
http://www.bootbarn.com/multiview/men/bootsshoes/view/view/0/48/popular/All?size=12.0&width=Narrow
<FONT face=Verdana color=#003399 size=1>Home</FONT> <!-- BREAD CRUMB VIEW START --><FONT face=Verdana><FONT size=2>›› </FONT><FONT color=#003399 size=1>Men</FONT></FONT> <FONT face=Verdana><FONT size=2>›› </FONT><FONT color=#003399 size=1>Boots & Shoes</FONT></FONT> <!-- BREAD CRUMB VIEW END -->
<!-- START MULTIVIEW TAG HERE --><!-- viewbrand: () viewbrandtype: () --><!--QUERY <pre><code>SELECT DISTINCT i.item_id, i.local_id, i.description, i.price, i.thumbnail_img, i.detail_page_img, i.sale_status_id FROM inventory_items i, inventory_status ix , families f, families_to_styles fs WHERE i.suppress = 'n' AND f.family_id = i.family_id AND f.family_id = fs.family_id AND fs.category_id = ? AND ( ix.size = ? ) AND ( ix.width = ? OR AND i.item_id = ix.item_id AND (ix.stock_avail = 'X' OR ix.stock_avail > 0) ORDER BY i.best_seller_sum DESC, i.last_updated </code></pre> <p>--><FONT color=red><TT>DBD::mysql::st execute failed: You have an error in your SQL syntax near 'AND i.item_id = ix.item_id AND (ix.stock_avail = 'X' OR ix.stock_avail > 0) ' at line 13 at /www/bootbarn.com/htdocs/libraries/multiview-tags.psp line 227. </TT></FONT></TD></TR></TBODY></TABLE></P></p>Admin
Sorry to be hijacking this thread with validation-related posts. I'm taking it to its own section here:
http://thedailywtf.com/forums/47047/ShowPost.aspx#47047Admin
Why is everyone so sure this guy wasn't just being smartass?
Admin
If you are using this code in several places you could, of course, make a function, so you can reuse the code in other pages. But maybe I'm a bad programmer :)
This code will probably won't compile, but it is just so you get the idea. I assume _GET is global, but you could pass it by parameter. Q are the postrequisites.
Admin
Darn it, you stole my thunder while I was busy composing my brilliant reply. [:O]
Anyway, the WTF code would end up looking like this:
Admin
dude .... I don't even know where to begin here .... just forget it ....
Admin
My thought exactly, and I thought it was pretty clever.
Admin
for the obligatory python one-liner, assuming a global '_get':
<FONT face="Courier New" size=2>def actionIdParamater(): return _get[_get.get('action','id')]</FONT>
Admin
LMAOF -- god that is good. and if he/she is being a smart ass, then all the better!
Admin
1. accessing global $_GET within a private method (if $_GET is just the Request object then that's okay)
2. not declaring variables inside method
3. if ( (int) ... ) ?
4. Remove Prequisites and Exceptions, or put them in xml tags to be parsed by a document generator (I'd say just remove them as they will inevitably become out of date as program Y goes in and adds an exception but doesn't read/update the little header)... method summarys are not likely to change
5. Along that, you say there are no exceptions yet what if $_GET['action'] does not exist... whamo exception (maybe not in this language?)
6. Actually along this theme you say there are no prerequisites... yet $_GET is expected to be exist (not so bad if as in point #1 this is the Request object)
Admin
Uuuh, you don't like it? :)
If you are only checking the action parameter on one place, you don't need to build a generic, parametrized function, of course :P
But I'm used to building big websites, with lot of repetitive code, so I'm calling quite but not exactly similar code from a pair of hundred different web pages in several different places and contexts.
From my experience, sooner or later my boss will ask me to add a "rename" action in every single page, and using one-liners like yours, I'll have to edit every single one-liner in the website. Last time I had to edit by hand like some 80 pages, because, of course, I had slightly modified some of the one-liners, because different pages would accept different arrays of actions, and I had to unify all the behaviour.
Next time I had to a add a different action, I only needed to change a few lines of code, because all the code was in a single place.
After several months expanding the same website, code placed in functions get naturally expanded, then gets placed inside classes and ends up making lots of complicated work, so "actionIdParameter" could be taking into account the user ID and several fields in the database, to check if the user can do that action. At the end, they allow me to change the behaviour of all of the website by only modifying a few lines.
One-liners, on the other hand, just get bloated and finally replaced.
</rant>
Admin
hehe .. that made my day. now i can go with a smile to bed ^^;;
Admin
Rather than a clueless programmers, that had to be an intentional "F U" to his boss. You don't just happen to figure out how to use the switch statement along with the default case, without somehow reading about the user-defined cases.
That guy rules.
Admin
No, it's better; it's BRILLANT.
Admin
I won't tear you to pieces here as I can see you're still learning, but you're saying I shouldn't use one-liners because they will be replaced. That's terrible ... just ... terrible thinking. I don't think there's anything wrong with using a function, and I won't even comment on the faults of the code, and your thinking in writing it. I do take personal offense though when you try to tell me that I'd use a one-liner EVERYWHERE in the site. How did you get that impression? I'd use it as nesscary, like anything else.
It's not freakin' rocket science, hell it ain't even computer science! Write the code to be simple, clean, and concise. Branch out as needed, following these rules. I can't believe any of you would knock a perfectly good one-liner.
All you newbs out there, listen up: if you can get it done in one line without sacrificing readability, then do it! Programming is an art form, not politics.
Admin
Am I the only one who finds the switch statement as nasty as a ternary statement? I've never understood why something like:
<font class="fixed_width" face="Courier, Monospaced">if x = 5:
do_this
elif x = 6:
do_that
else:
do_something_else
</font>
is supposed to be "bad", but
<font class="fixed_width" face="Courier, Monospaced">case of:
x = 5:
do_this
x = 6:
do_that
otherwise:
do_something_else
</font>
is supposed to be "good."
Admin
<font size="-1">Never attribute to malice that which is adequately explained by stupidity.
</font>
Admin
$_GET always exists. Like you say, it's like the request object in JSP, only it's just a String[][] array. It's generated and filled by the interpreter and available as a global variable. I don't remember how to pass $_GET by parameter in PHP. In PHP it's very normal to just access the global GET and POST arrays for reading parameters from them.
In PHP you can declare variables with no type, so you maybe didn't notice them. I actually mistyped some of the declarations, I re-write them here:
PHP is fun because if you mistype a variable name anywhere then PHP just creates a new empty variable for you with the mistyped name. Hours of fun guaranteed tracking down why your variable is not getting inicializated.
I try to keep the requisites up to date. But I don't try too hard. In Java you are forced to declare all exceptions, so that line can be taken out. Sometimes I forget what the little buggers do, and thore requisites give some clue, even if they are a bit out of date :) As the function gets called by more and more code, it's more and more important to be able to know fastly what it is SUPOSSED to do. You can then look at the code to see it still does what it is SUPOSSED TO DO, and change it back, or change the requisites.
Getting the "action" parameter doesn't give an exception because I first check the existance of it with isset(). However, I'm not checking the existance of the "id" parameter either, but PHP will probably just return a non set variable. The only posible exception would come from the (int) conversion, if I'm getting passed a string with alphabetic characters on it. I'm not sure what happens then.
"if ( (int) ... )" Ooooh, I just copy&pasted from the other code. Anyways, it probably works the same since the "if" will probably just convert the int back to boolean to check it. PHP is very forgetful of this kind of mistakes :)
You have to try PHP at least once in your life :)
Admin
Dude... programming is not an 'Art Form' it is just using tools to accomplish results... no different than using a hammer and a nail to make sure a piece of wood stays in place...
Class, please repeat after me... "Programming is not an art... "
Admin
That is the single most foolish remark I have ever heard. You must be very, very, young. You do know there are many houses that are art forms in thier own right? Don't believe me? Look up woodstock (not the hippie fest).