-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


Coded Smorgasbord: Mysterious Mysteries of Strange Mystery

Четверг, 21 Апреля 2016 г. 13:30 + в цитатник

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

Thats 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.

While I was checking on PHP starts/ends-with functions, I did figure out where this code came from. Its 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("Appt.: ");
        // add 300 more lines of stuff to the string builder
        return sb.toString();
    }
}

There are a few fun things about this method. First, theres the obvious: it uses a StringBuilder to construct HTML, JavaScript and CSS to inject into the page body. Thats pretty ugly. Do you see that input parameter, CSREntity? Thats actually a class that inherits from Entity- yes, this method is written to accept a parameter of its own time, and in fact, its 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 wasnt 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.

[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!

http://thedailywtf.com/articles/mysterious-mysteries-of-strange-mystery

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку