Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

25 Mar 2015

Are You Down With PHP?

by Dan J. in CodeSOD on 2015-03-25

Who’s Down With PHP?

PHP often gets a bad rap. A lot of the time, that’s because it’s used by developers that don’t know what they’re doing, just like there’s nothing inherently wrong with spandex, but there are times, places and people where it is inappropriate. And don’t get me wrong, the language has made big strides in recent years (good luck finding a web server hosting one of those versions, though). But there are just uses of PHP that reinforce that reputation. Robert Osswald provides this example from the contact-form editing code of a domain registrar database.

Let’s say you have some JSON data from an AJAX request, and it looks like this:

92 Comments - Last Comment @ 2015-03-28
23 Mar 2015

Regularly Expressing Hate

by Remy Porter in CodeSOD on 2015-03-23

Perl is jokingly referred to as a “write-only language”. This is because Perl’s primary solution to any problem is to throw a regular expression at it. Regexes are powerful, but cryptic.

Metamucil

Imagine RJ’s joy at starting a new contract for an OCR/document-management system that makes heavy use of regexes. Even better, the system doesn’t use widely implemented “Perl-compatible regular expressions” syntax, but instead, uses its own, slightly tweaked version.

81 Comments - Last Comment @ 2015-03-27
18 Mar 2015

A Deeply Nested Mystery

by Maciej Stachowski in CodeSOD on 2015-03-18

As any competent developer knows, not all Web browsers were created equal. In a stew of standards, drafts, vendor extensions, and JavaScript engine quirks, each of them can behave a little differently under various circumstances. Many people tried to resolve this situation, but "creative" coders are always one step ahead, finding new and amusing ways to mandate the dreaded "runs only under Internet Explorer 6" popup on their webpages.

This time, our submitter found out that his bus timetable was working just fine on Safari:

43 Comments - Last Comment @ 2015-03-23
16 Mar 2015

Negative Creativity

by Jane Bailey in CodeSOD on 2015-03-16

Pop quiz: how do you ensure that a number is negative? You could do something like:

Big Ben Negative

76 Comments - Last Comment @ 2015-03-19
11 Mar 2015

Waiting for File IO…

by Remy Porter in CodeSOD on 2015-03-11

Trisha had the misfortune to inherit some old Cold Fusion code. One particular block needed to see if a file existed before it continued, and apparently the original developer really wanted to be patient and make sure the file was there, so they wrote this:

26 Comments - Last Comment @ 2015-03-22
04 Mar 2015

SearchGuard

by Jane Bailey in CodeSOD on 2015-03-04

In a professional situation, an incorrectly selected record can result in a mortifying loss of data. Therefore, it behooves one to select the proper row carefully in one's stored procedure. That's why Charles's development team uses SearchGuard, the premier design pattern for selecting just the right row:


DELETE FROM [dbo].[tbl_employee] WHERE ((
	[employee_id] = @Original_employee_id) 
	AND ([salesperson_flag] = @Original_salesperson_flag) 
	AND ([first_name] = @Original_first_name) 
	AND ([last_name] = @Original_last_name) 
	AND ((@IsNull_job_title_type_id = 1 
	AND [job_title_type_id] IS NULL) OR ([job_title_type_id] = @Original_job_title_type_id)) 
	AND ((@IsNull_address_id = 1 AND [address_id] IS NULL) OR ([address_id] = @Original_address_id)) 
	AND ((@IsNull_work_extension = 1 AND [work_extension] IS NULL) OR ([work_extension] = @Original_work_extension)) 
	AND ((@IsNull_home_phone_number = 1 AND [home_phone_number] IS NULL) OR ([home_phone_number] = @Original_home_phone_number)) 
	AND ((@IsNull_alternate_phone_number = 1 AND [alternate_phone_number] IS NULL) OR ([alternate_phone_number] = @Original_alternate_phone_number)) 
	AND ((@IsNull_relative_name = 1 AND [relative_name] IS NULL) OR ([relative_name] = @Original_relative_name)) 
	AND ((@IsNull_relative_phone_number = 1 AND [relative_phone_number] IS NULL) OR ([relative_phone_number] = @Original_relative_phone_number)) 
	AND ((@IsNull_hire_date = 1 AND [hire_date] IS NULL) OR ([hire_date] = @Original_hire_date)) 
	AND ((@IsNull_termination_date = 1 AND [termination_date] IS NULL) OR ([termination_date] = @Original_termination_date)) 
	AND ((@IsNull_team_type_id = 1 AND [team_type_id] IS NULL) OR ([team_type_id] = @Original_team_type_id)) 
	AND ((@IsNull_comment = 1 AND [comment] IS NULL) OR ([comment] = @Original_comment)) 
	AND ((@IsNull_social_security_number = 1 AND [social_security_number] IS NULL) OR ([social_security_number] = @Original_social_security_number)) 
	AND ((@IsNull_drivers_license_number = 1 AND [drivers_license_number] IS NULL) OR ([drivers_license_number] = @Original_drivers_license_number)) 
	AND ((@IsNull_badge_number = 1 AND [badge_number] IS NULL) OR ([badge_number] = @Original_badge_number)) 
	AND ((@IsNull_hourly_wages = 1 AND [hourly_wages] IS NULL) OR ([hourly_wages] = @Original_hourly_wages)) 
	AND ((@IsNull_w2_claim = 1 AND [w2_claim] IS NULL) OR ([w2_claim] = @Original_w2_claim)) 
	AND ((@IsNull_insurance_start_date = 1 AND [insurance_start_date] IS NULL) OR ([insurance_start_date] = @Original_insurance_start_date)) 
	AND ((@IsNull_date_of_birth = 1 AND [date_of_birth] IS NULL) OR ([date_of_birth] = @Original_date_of_birth)) 
	AND ((@IsNull_shirt_size_type_id = 1 AND [shirt_size_type_id] IS NULL) OR ([shirt_size_type_id] = @Original_shirt_size_type_id)) 
	AND ([active_flag] = @Original_active_flag) AND ([network_userid] = @Original_network_userid) 
	AND ((@IsNull_vacation_days = 1 AND [vacation_days] IS NULL) OR ([vacation_days] = @Original_vacation_days)) 
	AND ((@IsNull_sick_days = 1 AND [sick_days] IS NULL) OR ([sick_days] = @Original_sick_days)) 
	AND ((@IsNull_last_vacation_update = 1 AND [last_vacation_update] IS NULL) OR ([last_vacation_update] = @Original_last_vacation_update)) 
	AND ([part_time_flag] = @Original_part_time_flag) 
	AND ((@IsNull_part_time_hours = 1 AND [part_time_hours] IS NULL) OR ([part_time_hours] = @Original_part_time_hours)) 
	AND ([first_eligible_bonus_received] = @Original_first_eligible_bonus_received) AND ((@IsNull_first_eligible_bonus_received_date = 1 AND [first_eligible_bonus_received_date] IS NULL) OR ([first_eligible_bonus_received_date] = @Original_first_eligible_bonus_received_date)) 
	AND ([awa_location_id] = @Original_awa_location_id) 
	AND ([rowguid] = @Original_rowguid) 
	AND ((@IsNull_bonus_view_date = 1 AND [bonus_view_date] IS NULL) OR ([bonus_view_date] = @Original_bonus_view_date)) 
	AND ((@IsNull_ceiva_flag = 1 AND [ceiva_flag] IS NULL) OR ([ceiva_flag] = @Original_ceiva_flag)) 
	AND ([ethnicity_id] = @Original_ethnicity_id) 
	AND ((@IsNull_sex = 1 AND [sex] IS NULL) OR ([sex] = @Original_sex)))

86 Comments - Last Comment @ 2015-03-17
02 Mar 2015

Polish Elections

by Maciej Stachowski in CodeSOD on 2015-03-02

Far away across the Atlantic, in the mythical land of Eastern Europe, where the sun don't shine and wild beasts roam the roads, lies a little country called Poland. Known in the world for its cheap manual labor and fondness for strong alcohol, it has for years been the butt of every national joke in almost all parts of the globe. But people here (or at least those who haven't run away yet) have been working hard to combat those pesky Eastern Bloc stereotypes, and as such, the country has in recent years seen a lot of social and technological progress. That last one, of course, comes with one notable exception: the government sector.

2010 Poland elections round 2 ballot box

61 Comments - Last Comment @ 2015-03-04