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

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

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

 

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

 -Статистика

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


Coded Smorgasbord: Classic WTF - The Long Way

Четверг, 01 Января 2015 г. 16:00 + в цитатник

2014 was a great year for us and hopefully for you too! Happy 2015 everybody! Enjoy this popular WTF from all the way back in May.


Sometimes, a developer just needs to take the long way around. Sure, a line of code like DateTime StartTime = DateTime.Now looks simple and readable, but what happens if you want the StartTime variable to be not exactly now?

Craigs co-worker figured out a better solution:


public string SomeFunc() {
  DateTime StartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 
    DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
  //… do stuff
}

So much better.

Now, though, we have a string thats guaranteed to exist and be at least 6 characters long. We need to get the last six characters of that string. We could use a substring function, but that wouldnt give us an opportunity to use our knowledge of LINQ functions. Kelly found this superior solution:

public string LastSixDigits
{
 get
 {
   if (string.IsNullOrWhiteSpace(this.Number) || this.Number.Length < 6)
     return string.Empty;
   return this.Number.Reverse().Take(6).Reverse().Aggregate(string.Empty, (s, c) => s += c);
 }
}

Finally, one of Jonathans peers needed to store a pair of strings as a single object. They could have used one of the many built-in types for that task, like a Tuple or KeyValuePair, but would that have provided a suite of overridden operators?

  public struct StringString
   {
       public string Key;
       public string Value;

       public static bool operator ==(StringString dataItem1, StringString dataItem2)
       {
           return (dataItem1.Key == dataItem2.Key && dataItem1.Value == dataItem2.Value);
       }

       public static bool operator !=(StringString dataItem1, StringString dataItem2)
       {
           return (dataItem1.Key != dataItem2.Key || dataItem1.Value != dataItem2.Value);
       }

       public override bool Equals(object obj)
       {
           if (!(obj is StringString))
           {
               return false;
           }

           return (this == (StringString)obj);
       }

       public override int GetHashCode()
       {
           return (Key.GetHashCode() + Value.GetHashCode());
       }
   }
[Advertisement] Use NuGet or npm? Check out ProGet, the easy-to-use package repository that lets you host and manage your own personal or enterprise-wide NuGet feeds and npm repositories. It's got an impressively-featured free edition, too!

http://thedailywtf.com/articles/classic-wtf-the-long-way

Метки:  

 

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

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

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

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