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

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

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

 

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

 -Статистика

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


CodeSOD: Hanging By a String

Понедельник, 11 Июля 2016 г. 13:30 + в цитатник

We all know that truth is a flexible thing, that strict binaries of true and false are not enough.

Danas co-worker knew this, and so that co-worker didnt use any pidling boolean values, no enums. They could do one better.

Now, were missing a lot of the code, but the pieces Dana shared with us are enough to get the picture…

    public CustomerRecord fetchNextCustomer()
    {
            //…
            String   yesNoString = String.valueOf(BusinessDAO.custFlagIsSet());

            if(yesNoString.equalsIgnoreCase("true")) yesNoString="Y";

            //… and later in this same method …
            if (yesNoString.equalsIgnoreCase("Y")) {
                //set a flag on the customer
            }
            //…
    }

True, false, Y, N, its all the same thing, yes? But how does this code actually get used?

    public Vector getCustomers()
    {
            //…

            String a = String.valueOf(BusinessDAO.custFlagIsSet());
            if (a.equalsIgnoreCase("TRUE"))
            {
                while(true) {
                    CustomerRecord aCustomer = fetchNextCustomer();

                    if (null != aCustomer) {
                        records.add(aCustomer);
                    }
                    else {
                        break;
                    }
                }
            } else {
                while(true) {
                    CustomerRecord aCustomer = fetchNextCustomer();

                    if (null != aCustomer) {
                        records.add(aCustomer);
                    }
                    else {
                        break;
                    }
                }
            }
            return records;
         }

Thats an excellent use of if statements. Theyre future proofed- if they ever do need to branch on that flag, theyre already done with that.

But what about that custFlagIsSet code? What on Earth is that doing?

    public String custFlagIsSet()
    {
        BusinessConfig domainObject;
        try
        {
            domainObject = getDomainObject;
        }
        catch (Exception e)
        {
            logger.error("",e);
        }

        boolean isFlag = domainObject.custFlagIsSet();

        String isFlagString = String.valueOf(isFlag) ;

        return isFlagString;
    }

Obviously, what were seeing here is a low-level API- that domainObject- being adapted into a more abstract API. Where that low-level API only uses little booleans, our custFlagIsSet object makes sure it only ever exposes strings- a much more flexible and robust datatype. Now, we can see some of the history in this code- before that custFlagIsSet modernized the underlying API, the other methods still needed to use String.valueOf, just in case a boolean accidentially came back.

If you say it carefully, it almost sounds like it makes sense. Almost.

[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/hanging-by-a-string

Метки:  

 

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

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

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

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