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

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

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

 

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

 -Статистика

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


Best of…: Best of 2016: Tern Around…

Пятница, 30 Декабря 2016 г. 14:30 + в цитатник
While looking at our most popular CodeSODs this year, I noticed we had two popular ones that both involved the ternary operator. So, what the heck, this one's a twofer. Originally, "As the World Ternaries" and, "Returnary". -- Remy

As the World Ternaries

Ah, the ternary operator. At their worst theyre a way to obfuscate your code. At their best, theyre a lovely short-hand.

For example, you might use the ternary operator to validate the inputs of a function or handle a flag.

Adam Spofford found this creative use of the ternary operator in a game hes developing for:

    this.worldUuid = builder.worldId == null ? null : builder.worldId;
    this.position = builder.position == null ? null : builder.position;
    this.rotation = builder.rotation == null ? null : builder.rotation;
    this.scale = builder.scale == null ? null : builder.scale;

    this.worldUuid = builder.worldId;
    this.position = builder.position;
    this.rotation = builder.rotation;
    this.scale = builder.scale;

Curious about how this specific block came to be, Adam poked through the Git history. For starters, the previous version of the code was the last four lines- the sane lines. According to git blame, the project lead added the four ternary lines, with a git comment that simply read: Constructing world details. That explains everything.


Returnary

Theres a certain class of bad code weve all seen before:

boolean someFunction() {
    if (someBooleanExpression) {
        return true;
    } else {
        return false;
    }
}

Its dumb, but largely harmless. Weve posted variations on this idea in the past. Scott P, however, found a new variation that may be the pinnacle of this kind of simple-minded programming. Ill let him introduce it.

I inherited a project that was developed in isolation by a lone developer that didnt do code reviews. I find something new to be horrified about every day. The developer responsible has a knack for adding unnecessary complexity to what should have been a much simpler application. This method perfectly sums up what the architecture of this application is like on every level. Its layer upon layer of obfuscation or redundant code.

boolean hasFeatures() {
        if (license.getFeatures().size() > 0 ? true : false) {
                return true;
        }
        return false;
}

Honestly, I think the ternary operator doesnt get enough use. We should just start putting ternary expressions all over our code, even if it means writing things like: myvar = true ? valueReturningFunction() : valueReturningFunction();. Isnt that pretty great? I love it.

[Advertisement] Release! is a light card game about software and the people who make it. Play with 2-5 people, or up to 10 with two copies - only $9.95 shipped!

http://thedailywtf.com/articles/best-of-2016-tern-around

Метки:  

 

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

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

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

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