1. Лучший говнокод

    В номинации:
    За время:
  2. Perl / Говнокод #13127

    −172

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    sub company_stuff_add_array_elems
    {
            my $list = shift;
    
            my $count = 0;
    
            foreach ( @$list )
            {
                    $count += $_;
            }
    
            return $count;
    }

    List::Util::sum
    http://perldoc.perl.org/List/Util.html

    kainwinterheart, 07 Июня 2013

    Комментарии (20)
  3. Ruby / Говнокод #13027

    −89

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    def set_path
          if name && (!path || path == "/")
            self.path = self.parent.present? ? "#{self.parent.path}/#{name}" : "/#{name}"
          elsif !new_record? && name && path && name_was != name
            parts = path.split("/")
            parts.pop
            self.path = [parts.join("/"), name].join("/")
          elsif !new_record? && name && self.parent_id_changed?
            self.path = self.parent.present? ? "#{self.parent.path}/#{name}" : "/#{name}"
          elsif new_record? && name && path
            self.path = [path, name].join("/")
          end
    
          if path && self.parent.blank?
            parts = path.split("/")
    
            self.name = parts.pop
    
            parent_path = parts.join("/")
            if parent_path.blank? || parent_path == "/"
              self.parent = nil
            else
              possible_parent = site.asset_folders.find_by_path(parent_path)
              self.parent = possible_parent.present? ? possible_parent : self.class.create(path: parent_path, site: site)
            end
          end
          true
         end

    Как не нужно работать с путями в Rails-приложении. Это и еще примерно 500 строк было заменено на 11 строк кода, включая пустые.

    whitequark, 20 Мая 2013

    Комментарии (20)
  4. C++ / Говнокод #12956

    +10

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    static int
          _S_compare(size_type __n1, size_type __n2)
          {
      const difference_type __d = difference_type(__n1 - __n2);
    
      if (__d > __gnu_cxx::__numeric_traits<int>::__max)
        return __gnu_cxx::__numeric_traits<int>::__max;
      else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
        return __gnu_cxx::__numeric_traits<int>::__min;
      else
        return int(__d);
          }

    LispGovno, 01 Мая 2013

    Комментарии (20)
  5. Java / Говнокод #12772

    +71

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    public static Connection getDBConnectionReader() throws Exception {
            Connection conn = null;
            try {
                conn = enrollmentsDataSource.getConnection();
            } catch (SQLException e) {
                LOGGER.warn("SQL Exception: get DB connection reader", e);
                resetConnectionReader();
                try {
                    conn = enrollmentsDataSource.getConnection();
                } catch (SQLException e1) {
                    throw new Exception("Exception: get DB connection reader", e1);
                }
            }
            return conn;
    }

    Фрактал...

    amarfey, 20 Марта 2013

    Комментарии (20)
  6. C++ / Говнокод #12736

    +28

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    class atoi_func
    {
    public:
        atoi_func(): value_() {}
    
        inline int value() const { return value_; }
    
        inline bool operator() (const char *str, size_t len)
        {
            value_ = 0;
            int sign = 1;
            if (str[0] == '-') { // handle negative
                sign = -1;
                ++str;
                --len;
            }
    
            switch (len) { // handle up to 10 digits, assume we're 32-bit
                case 10:    value_ += (str[len-10] - '0') * 1000000000;
                case  9:    value_ += (str[len- 9] - '0') * 100000000;
                case  8:    value_ += (str[len- 8] - '0') * 10000000;
                case  7:    value_ += (str[len- 7] - '0') * 1000000;
                case  6:    value_ += (str[len- 6] - '0') * 100000;
                case  5:    value_ += (str[len- 5] - '0') * 10000;
                case  4:    value_ += (str[len- 4] - '0') * 1000;
                case  3:    value_ += (str[len- 3] - '0') * 100;
                case  2:    value_ += (str[len- 2] - '0') * 10;
                case  1:    value_ += (str[len- 1] - '0');
                    value_ *= sign;
                    return value_ > 0;
                default:
                    return false;
            }
        }
    private:
        int value_;
    };

    standard atoi()
    79142 milliseconds

    class atoi_func
    131 milliseconds.

    Если приходится велосипедить стандартные функции, то это камень в огород С++. Видать кресты писали гении ассемблерной оптимизации.

    LispGovno, 13 Марта 2013

    Комментарии (20)
  7. C++ / Говнокод #12555

    +27

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    inline double poly(double x, const double *c, int k) const {
            double y = c[k];
            switch (k) {
                case 15: y = y * x + c[14];
                case 14: y = y * x + c[13];
                case 13: y = y * x + c[12];
                case 12: y = y * x + c[11];
                case 11: y = y * x + c[10];
                case 10: y = y * x + c[ 9];
                case  9: y = y * x + c[ 8];
                case  8: y = y * x + c[ 7];
                case  7: y = y * x + c[ 6];
                case  6: y = y * x + c[ 5];
                case  5: y = y * x + c[ 4];
                case  4: y = y * x + c[ 3];
                case  3: y = y * x + c[ 2];
                case  2: y = y * x + c[ 1];
                case  1: y = y * x + c[ 0];
                case  0: break;
            }
            return y;
        }

    Схема Горнера для вычисления значения многочлена в точке

    uranix, 08 Февраля 2013

    Комментарии (20)
  8. JavaScript / Говнокод #12541

    +151

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    function renderLayout(layout, column, colidx, restrictions) {
            //...
            if (!colidx) {
                    var flag = 1 << 0 | 1 << 1 | layout.title.type << 3;
                    if (layout.title.split) flag |= 1 << 2;
            }
            //...
    }

    Магические преобразования... или как стать незаменимым сотрудником!

    Eugene, 05 Февраля 2013

    Комментарии (20)
  9. C# / Говнокод #12536

    +103

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    private static bool IsSourceField(DataRow row, string fieldName)
            {
                try
                {
                    object fieldValue = row[fieldName];
    
                    return false;
                }
                catch
                {
                    return false;
                }
            }

    Бизнес логика.

    kore_sar, 05 Февраля 2013

    Комментарии (20)
  10. SQL / Говнокод #12427

    −139

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    CREATE TABLE `forums_list`(
        `forum_id` int unsigned NOT NULL AUTO_INCREMENT,
         ...
        `created` datetime NOT NULL DEFAULT NULL,
        `updated` datetime NOT NULL DEFAULT NULL,

    Alex_Slubsky, 16 Января 2013

    Комментарии (20)
  11. PHP / Говнокод #12400

    +52

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    //--Корзина заказа
        $misc_basket=array();
        $misc_basket['num']=0;
        
        //--проверка наличия в корзине товаров
        $b_sum=0;
        $b_num=0;
        //var_dump($_SESSION['goods']);
        if(isset($_SESSION['goods']))
        {       
         //   echo "11";
            foreach(explode("^^^",$_SESSION['goods']) as $item)
            {
                $arr1=explode(":::",$item);
                $type="";
                if($arr1[2]=='disc')$dbs="disc";
                elseif($arr1[2]=='tire') $dbs="shina";
                else $dbs="truck_shina";
                $iddb=abs($arr1[0]);
                
               // echo "select `price` from `".PREFIX."$dbs` where `id`=$iddb";
                $res=$db->query("select `price` from `".PREFIX."$dbs` where `id`=$iddb");
                if($res->num_rows)
                {
                     $arr=$res->fetch_array();
                     $b_sum+=intval($arr1[1])*correct_double($arr['price']);
                     $b_num+=$arr1[1];
                }
            }
        }
        $misc_basket['num']=$b_num;
        $misc_basket['sum']=$b_sum;

    alex_ok, 10 Января 2013

    Комментарии (20)