CodeSOD: The Story of Things
Every line of code tells a story. It never just" appears. Someone made and crafted that code. There's a story, and an explanation for how that code could be. The world, even the bad, awful corners of it, makes sense and can be understood.
For example, Luke sends us this block.
/** * Format a string as a phone number mm/dd/yyyy */ public static String formatPhone(String phone) { String s = ""; if (phone != null) { s = phone.trim(); } return s; }
I imagine, the thinking went like this: "I need to write a function to format a phone number, so I'll copy the code for the function to format dates, and I'll replace every instance of date with phone. Oh, wait, the comment is unclear. I'll make that phone number"
Similarly, this Anonymous PHP code tells its own story:
if(!$this->auth_model->in_group('Manager')) { $data->userLevel = 1; } else { $data->userLevel = 3; } if($data->userLevel == 3) { redirect('index', 'refresh'); //In CodeIgniter, this triggers a 301 redirect to another page. }
The story, I imagine here, was a case of conflicting terminology. Once upon a time, someone came up with the idea of "User Levels". Someone else thought that was needlessly cryptic, and wanted to give them names. Then this developer got confused, and decided to translate everything back into user levels. This particular if block appeared dozens of times in the code, and it was the only place that userLevel was eveer used. Our anoymous submitter ripped them out.
Finally, a different Anonymous contributor found this block:
def formatDate(date): date = time.strptime(str(date), '%Y-%m-%d') date = str(date.tm_year)+'-'+str(date.tm_mon)+'-'+str(date.tm_mday)
Now, the developer responsible for this knew that they needed a date in a Year-Month-Day(%Y-%m-%d) format. So they started out by taking a date object, and converting it to a string, using str(date). This converts it into a %Y-%m-%d format. They then parse that into a date object- specifically a struct_time, using time.strptime. Once they had that, they could then use the various properties of the struct_time to build a %Y-%m-%d format.
I take back what I said. The world is a chaotic, disordered place, effects may proceed without cause. Sense is nonsense, and paradoxes are the only form of truth. There's no explanation for this code, or for" anything, is there?
[Advertisement] Release!is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped!