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

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

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

 

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

 -Статистика

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


CodeSOD: Take a Moment to Reflect

Вторник, 02 Августа 2016 г. 13:30 + в цитатник

Modern object-oriented languages tend to support the concept of reflection. Reflection lets you inspect an object and find out its methods and invoke them dynamically. If youre building extensible frameworks where youre handling objects where their type might not be known until runtime, it can be very useful. On the other hand, if youre using a strongly typed language and find yourself in this situation frequently… youre probably doing something wrong.

For that reason, when Adam encounters calls to method.invoke() in Java programs, he gets suspicious. So, when he saw this:

Method method = generator.getClass().getMethod(methodName, null);
customerListList = (ArrayList) method.invoke(generator, null);

Adam knew he had to investigate. First, he looked up how the methodName variable was being populated. A few lines up, he saw:

String methodName = "getCustomerListFor"+ alphaBet.trim().toUpperCase();

And then, he checked the generator class:

    public ArrayList getCustomerListForA() {...}
    public ArrayList getCustomerListForB() {...}
    public ArrayList getCustomerListForC() {...}
    public ArrayList getCustomerListForD() {...}

There were twenty six copy-pasted versions of the getCustomerList method, where the only difference was that each had a hard-coded string which defined how to filter the data set.

When Adam asked the developer why they didnt simply use a parameter, the developer replied. I didnt want to have to write a switch statement.

[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today!

http://thedailywtf.com/articles/take-a-moment-to-reflect

Метки:  

 

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

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

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

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