Article 3FPA8 Coded Smorgasbord: If It's Stupid and It Works

Coded Smorgasbord: If It's Stupid and It Works

by
Remy Porter
from The Daily WTF on (#3FPA8)

On a certain level, if code works, it can only be so wrong. For today, we have a series of code blocks that work" mostly. Despite that, each one leaves you scratching your head, wondering how, exactly this happened.

Lisa works at a web dev firm that just picked up a web app from a client. They didn't have much knowledge about what it was or how it worked beyond, "It uses JQuery?"

Well, they're technically correct:

if ($(document.getElementById("really_long_id_of_client_side_element")).checked) { $(document.getElementById("xxxx1")).css({ "background-color": "#FFFFFF", "color": "Black" }); $(document.getElementById("xxxx2")).css({ "background-color": "#FFFFFF", "color": "Black" }); $(document.getElementById("xxxx3")).css({ "background-color": "#FFFFFF", "color": "Black" }); $(document.getElementById("xxxx4")).css({ "background-color": "#FFFFFF", "color": "Black" });};

In this case, they're ignoring the main reason people use jQuery- the ability to easily and clearly fetch DOM elements with CSS selectors. But they do use the css function as intended, giving them an object-oriented way to control styles. Then again, one probably shouldn't set style properties directly from JS anyway, that's what CSS classes are for. Then again, why mix #FFFFFF and Black, when you could use white or #000000

Regardless, it does in fact use JQuery.

Dave A was recently trying to debug a test in Ruby, and found this unique construct:

if status == status = 1 || status = 2 || status = 3 @msg.stubs(:is_reply?).returns trueelse @msg.stubs(:is_reply?).returns falseend

This is an interesting case of syntactically correct nonsense that looks incorrect. status = 1 returns a 1, a "truthy" value, thus short circuiting the || operator. In this code, if status is undefined, it returns true and sets status equal to 1. The rest of the time it returns false and sets status equal to 1.

What the developer meant to do was check if status was 1, 2 or 3, e.g. if status == 1 || status == 2", or, to use a more Ruby idiom: if [1, 2, 3].include? status. Still, given the setup for the test, the code actually worked until Dave changed the pre-conditions.

Meanwhile, Leonardo Scur came across this JavaScript reinvention of an array:

tags = { "tags": { "0": {"id": "asdf"}, "1": {"id": "1234"}, "2": {"id": "etc"} }, "tagsCounter": 3, // " below this are reimplementations of common array methods built to work on `tags`}

This was part of a trendy front-end framework he was using, and it's obvious that arrays indexed by integers are simply too mainstream. Strings are where it's at.

This library is in wide use, meant to add simple tagging widgets to an AngularJS application. It also demonstrates a strange way to reinvent the array.

release50.png[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! TheDailyWtf?d=yIl2AUoC8zAMdL4xeQ5EZE
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments