Article 1B97T Coded Smorgasbord: Mysterious Mysteries of Strange Mystery

Coded Smorgasbord: Mysterious Mysteries of Strange Mystery

by
Remy Porter
from The Daily WTF on (#1B97T)

Code is a window into the programmer's mind. Our thought processes are laid bare, exposed and cemented for all eternity in keywords and symbols. It's left there, waiting for another programmer to come by and wonder: "What were they thinking?"

That's exactly what "seebs" was wondering, when he found this PHP code.

function startsWith($haystack, $needle) { // search backwards starting from haystack length characters from the end return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;}function endsWith($haystack, $needle) { // search forward starting from end minus needle length characters return $needle === "" || strpos($haystack, $needle, strlen($haystack) - strlen($needle)) !== FALSE;}

Now, the "normal" solution here would be to look at the length of your "needle" and chop off that many characters from the "haystack" using substr, then compare the two strings. Using strpos and strrpos is odd, but not half so odd as comparing the result to FALSE instead of zero.

mysteriousmysteries.jpg

While I was checking on PHP starts/ends-with functions, I did figure out where this code came from. It's the most popular answer on StackOverflow, showing us another case of "programming by copy paste".

Etta overheard some co-workers talking about issues with the "Entity" class. That was exciting, since their business objects were a mess, and Etta was excited about the thought that they might build some classes that were business objects that represented a single entity, which would be a change of pace.

Instead, Etta found a 4,000 line Entity class that was actually a "God Object" with every feature and function crammed into it. But it was even worse than that.

public class Entity{// 4000 lines and 3 inner classes later public string getFirstOpenApptHtml(final CSREntity csr) throws Exception { final StringBuilder sb = new StringBuilder(); sb.append("<a href='javascript:showNotePopup("); sb.append(getEntityID()); sb.append(","); sb.append(this.firstOpenAppt_AbbreviatedData.getNoteID()); sb.append(")'><span style='color: #3333CC; text-decoration: underline; font-weight: bold;'>Appt.: "); // add 300 more lines of stuff to the string builder return sb.toString(); }}

There are a few fun things about this method. First, there's the obvious: it uses a StringBuilder to construct HTML, JavaScriptand CSS to inject into the page body. That's pretty ugly. Do you see that input parameter, CSREntity? That's actually a class that inherits from Entity- yes, this method is written to accept a parameter of its own time, and in fact, it's usually called by running csr.getFirstOpenApptHtml(csr);

Finally, Jess was wondering, "What if I needed to sort a list of integers? Could I use the built in functionality, or is there a better way?"

Actually, Jess wasn't wondering that at all. But a co-worker apparently was.

namespace MyCompany.Utilities{ using System; [Serializable] public class Integer : IComparable { private int _value; public Integer(int value) { _value = value; } public int Value { get { return _value; } set { _value = value; } } public int CompareTo(object obj) { if (obj is Integer) { Integer integer = (Integer)obj; return _value.CompareTo(integer._value); } else { throw new ArgumentException("Object is not of type Integer"); } } }}

For the unfamiliar, C# and all the .NET languages autobox primitives, and integers already implement the IComparable interface, making this code 100% redundant. The code was also 100% unused, and had never been used- despite being the first check-in to the codebase.

otter-icon.png [Advertisement] Otter allows you to easily create and configure 1,000's of servers, all while maintaining ease-of-use, and granular visibility down to a single server. Find out more and download today! TheDailyWtf?d=yIl2AUoC8zAtxGDTxLd4F0
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