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

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

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

 

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

 -Статистика

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


CodeSOD: Not the Shortest Shortener

Понедельник, 03 Октября 2016 г. 13:30 + в цитатник

Going through TDWTF inbox, Ive built a sort of mental taxonomy of bad code. For example, theres the kingdom of Tempus Malum: home-brew date manipulation functions, a rather profligate branch of bad code. Or the Order of Linguan Ignorans- bad code developed out of a complete ignorance of the available language features.

Theres another category that I always consider a treat. Its related to Linguan Ignorans, but also borrows from Quaesto Ignorat (ignorant of the problem being solved): Filo Annexa, or Knotted String, also known as String All the Things!

Which brings us to todays C# code, from Aaron.

    public string ShortenFloatString(float valueToShorten)
    {
        string xString;
        string xString2 = "";

        xString = valueToShorten.ToString();

        if (xString.IndexOf('.') > -1)
            xString2 = xString.Substring(xString.IndexOf('.'));

        if (xString2.Length > 3)
            xString2 = xString2.Substring(0, 3);

        if (xString.IndexOf('.') > -1)
            xString = xString.Substring(0, xString.IndexOf('.'));

        if(xString2.Length > 2)
        {
            if(xString2[2] == '9')
            {
                int num = int.Parse(xString2[1].ToString());
                if (num <= 9)
                {
                    num++;
                    xString2 = "." + num.ToString();
                }
                else
                {
                    num = 0;
                    xString2 = "." + num.ToString();

                    int firstNum = int.Parse(xString);
                    firstNum++;
                    xString = firstNum.ToString();
                }
            }
        }
        return (xString + xString2);
    }

Ive never had to spend so much time looking at a rounding function to confirm that it is, in fact, a rounding function. Well, sort of. It doesnt enforce any rounding rule that Ive ever heard of.

Like all bad code, it eschews the built in functions for solving this problem. But it takes it to another level, by doing all the rounding through string manipulation- at least as much as it can. Strings, of course, cant be effectively rounded, so of course theres a moment where we need to int.Parse bits of the strings back into numbers so that we can actually do rounding.

Bonus points are awarded for excellent variable names, and also for using Substring instead of Split, which is also a built-in method.


[Advertisement] Atalasoft’s imaging SDKs come with APIs & pre-built controls for web viewing, browser scanning, annotating, & OCR/barcode capture. Try it for 30 days with included support.

http://thedailywtf.com/articles/not-the-shortest-shortener

Метки:  

 

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

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

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

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