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

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

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

 

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

 -Статистика

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


CodeSOD: Negative Creativity

Понедельник, 16 Марта 2015 г. 14:00 + в цитатник

Pop quiz: how do you ensure that a number is negative? You could do something like:

Big Ben Negative


if (count > 0) {
	count = count * -1
}

Or you could use a ternary:


count = count > 0 ? count * -1 : count;

Or, if you're feeling fancy, use absolute value, like


count = Math.Abs(count) * -1;

If you're Justin's coworker, however, that's not good enough. No, what you REALLY want to do is coerce to a string, prepend a negative sign, then remove any double-negatives to make a positive again:


if (count > 0 || count < 0)
{
 string tCountDelta = "-" + count;
 tCountDelta = tCountDelta.Replace("--", "");

 // Some time later...
 int countDelta = int.Parse(tCountDelta);
}

Now that's fancy.

[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/negative-creativity

Метки:  

 

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

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

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

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