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.

Mar 2015

Rube Goldberg's Password Generator

by in CodeSOD on

One of the well-known rules of life is that the most straightforward solution is usually the best solution. Obviously it's not always possible to "keep it simple, stupid," but one should aim to make their creations as self-explanatory and to-the-point as possible- otherwise it's easy to end up with a nightmare in terms of both maintainability and performance.

Photo of the Week- More than One Way to Hammer a Nail (8722944827)

Some people, however, have chosen to defy that rule. One of them was Rube Goldberg. This engineer turned cartoonist became famous for inventing ridiculously complex contraptions to achieve the simplest tasks. And while Mr. Goldberg passed away in 1970, the concept of a "Rube Goldberg machine" outlived him, showing up in hundreds of cartoons, events, and comedy movies.


Are You Down With PHP?

by in CodeSOD on

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:


Regularly Expressing Hate

by in CodeSOD on

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.


A Deeply Nested Mystery

by in CodeSOD on

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:


Negative Creativity

by in CodeSOD on

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

Big Ben Negative


Waiting for File IO…

by in CodeSOD on

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:


            

SearchGuard

by in CodeSOD on

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)))


Polish Elections

by in CodeSOD on

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