A Single Article
Read it, comment, and share it with your friendsAnother headdesk story from the front lines
I spend more time fixing bad code than writing good, new code. This is probably because well-written code doesn’t take long to produce and there’s a ton of bad code out there to take up my time. Today I ran into yet another script that really made my jaw drop when I saw it. Let me give you an idea of why:
There is a form where a user can sign up for 4 items, all checkboxes. So they can sign up for none or 1 or all 4, doesn’t matter. Each item is different.
The script that processes this form says, (and I’m paraphrasing here),
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | $c = 0; if($input{'item1'}) { $c += 1; } if($input{'item2'}) { $c += 2; } if($input{'item3'}) { $c += 4; } if($input{'item4'}) { $c += 8; } if($c = 15) # all four items { # do ... } if($c = 14) # items 2, 3, 4 { # do ... } if($c = 13) # items 1, 3, 4 { # do ... } if($c = 12) # items 3, 4 { # do ... } |
And yes, that went on down to 1. For the record, all that the script does is tell you which items you selected and then allow you another chance to select the ones you didn’t pick… that’s it! Why is there a need to tell which combinations were chosen? There isn’t! Besides, had the person who wrote this never heard of hashes? Foreach? Anything? Did they think this was a clever way of handling this form? Are they still out there, writing bad code and mucking things up for good developers who have to clean up in their wake? I don’t know, but I do know that I was asked to add another item to the list of checkboxes on this form… can you guess what I decided?
Yes, I’m going to rewrite the whole thing the right way, and it will probably require hundreds fewer lines of code.
Now, a quiz: how many conditional statements would be added to this script in order to handle another item? First person to get the answer gets a prize, and yes, it’s just going to be three exclamation marks again. I’ll give out real prizes when I’m not in debt.
Here’s to less headdesks in the future…
Get a Trackback link
7 Comments
Responses to my articleIt’s early in the morning here, but I’ll be having a go for those exclamation marks myself.
Another item would be item5 and $c += 16, necessitating 16 more conditions (all of the original 15 plus item5 and one for item5 on its own).
Of course, I bet the original programmer thought they were being very clever using base 2 to uniquely identify any combination of checkboxes. Sad that the cleverest of things turn out looking the most stupid (and causing you the most work)!
To less headdesks!
Even after 15 months in my job I’m still dealing with more shit code on a daily basis than I am writing new, good code. The sad thing is, some of the shit code is my own, done in a hurry when I haven’t got time to be a perfectionist
I’m going to say a total of 32: 1 combination of 5, 5 of 4, 10 of 3, 10 of 2, 5 of 1, 1 of 0. Using factorials and pascals triangle.
Don’t forget you can also have all 5 selected.
I’ve written my share of bad code (looking at some of my old code makes me cringe), but that made me literally laugh out loud.
Phil wins for being first. Here’s your prize Phil: !!!
You can put this on your blog, website, or social profile page.
Aaron: There’s already 16 conditions, and one more item means 16 more conditions, the maximum total being 31. The case where only item 5 is selected is 16.
Jem: I hear what you mean, sometimes I’ve written code I’m not proud of because I was in a hurry to get it done, but seriously, even my poorest code is still ten times better than stuff like this -.-;;
Phil, Aaron: Oh, one more thing, I’m sure you noticed this, but it’s 17 instead of 16 since there’s also the first condition to say “if item5, c += 16.”
Woops, the ‘c’ can equal up to 31, but the total combinations, which is what I meant, is 32 because c can equal 0.
nasty…
Leave a comment
Share your thoughts with the worldYou can use Markdown, or you can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>Please keep comments respectful and on topic.
This form is guarded by Akismet, so don't waste your time trying to submit spam. It won't work. Ever.