The single worst line of code I have written

As much as I would love to think I am the perfect programmer who makes no mistakes and whose code is 100% bug free… I am reminded way too well that I am not all that and make lots of code mistakes. I was reminded by this sad truth ever so clearly while working on some older JavaScript on the 29th of May. After extensive testing and analyzing, I concluded that a single line of code in the script was the single worst line of code I had ever written. Are you ready for it?

// The higher the severity rating, the longer the reply will be.
// Default to three random messages if rating comes out as zero.
rating = rating > 0 ? rating *= Math.floor(words.length / 7) : 3;

This ternary operator is the most fail worthy line of code in any of my recent work. This line along with a corresponding comment, when put into context of the script, is the source of two bugs, one violation of code comments, and other actions that made me facepalm more than I normally would when looking at other poorly written code. Let us begin dissecting this line and its many issues, shall we?

  1. It is scary looking. Normally this is a not an issue as all code can and will look scary at any given time. For some reason, this line is scarier looking than other code I have written. That could be because I determined how it operates, and I am scared that I managed to write such a bad line.
  2. The code comment is incorrect. Before I can explain anymore, I must provide some context.

    This project contains an array of “hotwords”, a variety of words and phrases that are searched for in the user’s entered message (words). The ternary then boosts a “rating” of the submitted message, which was partially generated earlier when the hotwords were counted.

    Originally, it seems the new rating value would be used as the number of randomly selected messages to present to the user (issue 2.5: looking at revision history, this theory does not make sense as I had other code in place that would do just that).

    Yet for some reason I wrote this comment, which contradicts what was really going on:

    // Default to three random messages if rating comes out as zero.

    There is a possibility I was attempting late-night coding, but even when I attempt that I usually make more sense than this…

  3. If the initial rating ends up equaling zero, the entire value is reassigned to 3. This looks to tie back into my incorrect comment, but the grading scale that uses result slots that value into an else clause. Again, what was I thinking?
  4. If the initial rating is <= 13, there will be no change. Forget the Math.floor(words.length / 7) magic number wizardry or associative multiplication, they have no effect if our initial rating is so low. This case completely nullifies the purpose of the ternary existing in the first place!
  5. If the initial rating is >= 14, the revised value will be rating * 2. I have thoroughly tested this case with many numbers. No matter the number, if it is greater than or equal to 14, the number magic tricks will simply double it. Let that sink in for a moment. I have no clue where I got the whole equation from, much less how I managed to pull this off.

I am… appalled and rather speechless at my failure here. I am sure there are other noteworthy fails in my large amount of code from the past 2.5 years of programming, but this is the first one I have found and really examined. 😛

Do you have any single-line-of-code monsters you are embarrassed about and would like to dissect and share? Let me know in the comments! I would love to hear them! 😀



5 thoughts on “The single worst line of code I have written

  1. Disclaimer: bunch of guesses, based on your one line code fragment and limited description.

    It seems that in your description (points 3 and 4), you use “rating” as synonym for “words.length”, is it correct assumption on my side that somewhere earleir in the code you have initializer statement to that effect (rating = words.length) ?

    Secondly is that line of code called in some sort of iterative manner? If it is called more than once for a single user message containing 6 words or less, it will converge to the value 3 on second iteration.

    1. Actually, rating is initialized as zero then incremented by 1 for each hotword present in the user’s message (words) as separated by a modified version of this function. No, it is it not called in an iterative manner. It’s sole purpose was to “puff up” rating, but clearly failed to do so. Thanks for pointing out that bug, I had no idea it would do that!

    1. How can you blame how a language works and acts when this whole post is nothing but logic errors I implemented? Blaming JavaScript for my failure would be a cheap, scapegoat way for me to excuse my faults.

  2. I don’t have any specific examples, but anything that involves runtime type casting or function pointers/iterators always finishes as an awful-looking piece of code.

Comments are closed.