1. Java / Говнокод #14514

    +73

    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
    class MyException extends Exception {
    	/* конструкторы, геттеры-сеттеры и прочее-прочее */
    	
    	public void handle() { /* over 9000 строк */ }
    }
    
    class MyClass {
    	public void myMethod() {
    		try { /* ... */ }
    		catch (MyException e) {
    			e.handle();
    		}
    	}
    }

    исключение, которое обрабатывает, можно сказать, само себя, бред?

    evg_ever, 06 Февраля 2014

    Комментарии (19)
  2. PHP / Говнокод #14512

    +153

    1. 1
    2. 2
    3. 3
    <?if($_POST["is_ajax_post"] != "Y"){?>				
    					<input type="hidden" name="is_ajax_post" id="is_ajax_post" value="Y">    			        
    <? }  ?>

    Форма оформления заказа в компоненте sale.order.ajax. Bitrix. Логика.

    TBoolean, 06 Февраля 2014

    Комментарии (4)
  3. Си / Говнокод #14511

    +137

    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
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    #define SPLICE(a,b) SPLICE_1(a,b)
    #define SPLICE_1(a,b) SPLICE_2(a,b)
    #define SPLICE_2(a,b) a##b
     
     
    #define PP_ARG_N( \
              _1,  _2,  _3,  _4,  _5,  _6,  _7,  _8,  _9, _10, \
             _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
             _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
             _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
             _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
             _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
             _61, _62, _63, N, ...) N
     
    /* Note 63 is removed */
    #define PP_RSEQ_N()                                        \
             62, 61, 60,                                       \
             59, 58, 57, 56, 55, 54, 53, 52, 51, 50,           \
             49, 48, 47, 46, 45, 44, 43, 42, 41, 40,           \
             39, 38, 37, 36, 35, 34, 33, 32, 31, 30,           \
             29, 28, 27, 26, 25, 24, 23, 22, 21, 20,           \
             19, 18, 17, 16, 15, 14, 13, 12, 11, 10,           \
              9,  8,  7,  6,  5,  4,  3,  2,  1,  0
     
    #define PP_NARG_(...)    PP_ARG_N(__VA_ARGS__)    
     
    /* Note dummy first argument _ and ##__VA_ARGS__ instead of __VA_ARGS__ */
    #define PP_NARG(...)     PP_NARG_(_, ##__VA_ARGS__, PP_RSEQ_N())
     
    #define NARG(...)     func(PP_NARG(__VA_ARGS__), __VA_ARGS__)
     
    #define INCR 20
     
    #define FIELD_0(...)
     
    #define FIELD_1(field, ...) \
      INCR+field
     
    #define FIELD_2(field, ...) \
      INCR+field, FIELD_1(__VA_ARGS__)
     
    #define FIELD_3(field, ...) \
      INCR+field, FIELD_2(__VA_ARGS__)
     
    #define FIELD_4(field, ...) \
      INCR+field, FIELD_3(__VA_ARGS__)
     
    #define FIELD_5(field, ...) \
      INCR+field, FIELD_4(__VA_ARGS__)
     
    #define FIELD_5(field, ...) \
      INCR+field, FIELD_4(__VA_ARGS__)
     
    #define FIELD_6(field, ...) \
      INCR+field, FIELD_5(__VA_ARGS__)
     
    #define FIELD_7(field, ...) \
      INCR+field, FIELD_6(__VA_ARGS__)
     
    #define FIELD_8(field, ...) \
      INCR+field, FIELD_7(__VA_ARGS__)
     
    #define FIELD_9(field, ...) \
      INCR+field, FIELD_8(__VA_ARGS__)
     
    #define FIELD_10(field, ...) \
      INCR+field, FIELD_9(__VA_ARGS__)
     
    #define FIELD_11(field, ...) \
      INCR+field, FIELD_10(__VA_ARGS__)
    //..... дальше лень ...
     
    #define FIELDS_(N, ...) \
      SPLICE(FIELD_, N)(__VA_ARGS__)
     
    #define FIELDS(...) \
      FIELDS_(PP_NARG(__VA_ARGS__), __VA_ARGS__)
     
     
    a = { FIELDS('p', 'a', 's', 's', 'w', 'o', 'r', 'd') }

    Примитивное компил-тайм шифрование строки через сишный препроцессор. На выходе получаем

    a = { 20 +'p', 20 +'a', 20 +'s', 20 +'s', 20 +'w', 20 +'o', 20 +'r', 20 +'d' };

    Базируется на http://smackerelofopinion.blogspot.com/2011/10/determining-number-of-arguments-in-c.html

    j123123, 06 Февраля 2014

    Комментарии (18)
  4. Java / Говнокод #14510

    +68

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    public static String elvis(String value, String ifNull) {
            return value == null ? ifNull : value;
        }
    
        public static Boolean elvis(Boolean value, Boolean ifNull) {
            return value == null ? ifNull : value;
        }
    
        public static Object elvis(Object value, Object ifNull) {
            return value == null ? ifNull : value;
        }

    - Objects#firstNotNull()?
    - нет, не слышал

    myzone, 05 Февраля 2014

    Комментарии (34)
  5. C# / Говнокод #14509

    +105

    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
    //Невероятные приключения Microsoft'а в Индии:
    
            private string ExtractHttpVerb(XmlDocument configDOM)
            {
                string httpVerb;
    
                string hv = IfExistsExtract(configDOM, "/Config/method", "2");
    
                switch (hv)
                {
                    case "0":
                        httpVerb = HttpVerbs[0];
                        break;
    
                    case "1":
                        httpVerb = HttpVerbs[1];
                        break;
    
                    case "2":
                        httpVerb = HttpVerbs[2];
                        break;
    
                    default:
                        httpVerb = HttpVerbs[2];
                        break;
                }
    
                return httpVerb;
            }

    HellMaster_HaiL, 05 Февраля 2014

    Комментарии (33)
  6. SQL / Говнокод #14508

    −167

    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
    PROCEDURE login_attr(a_id IN CHAR, a_pw IN VARCHAR2, v_ret IN OUT VARCHAR2)
    IS
       v_count INTEGER;
    BEGIN
       v_count := 0;
    
       SELECT NVL (COUNT (attrib_desc), 0)
         INTO v_count
         FROM tab_attr a,
              tab_users b
        WHERE a.msc_cd = b.user_dept
          AND a.attr_cd = 'CD'
          AND lattr_cd = 'ABCD'
          AND UPPER (user_no) = UPPER (a_id)
          AND UPPER (user_pw) = UPPER (a_pw);
    
       IF v_count > 0 THEN
          SELECT attrib_desc || '@' || user_lang || '@' || user_autoimg ||
                 '@' || user_imgtime || '@' || user_loc || '@' ||
                 user_dept
            INTO v_ret
            FROM tab_attr a,
                 tab_users b
           WHERE a.msc_cd = b.user_dept
             AND a.attr_cd = 'CD'
             AND lattr_cd = 'ABCD'
             AND UPPER (user_no) = UPPER (a_id)
             AND UPPER (user_pw) = UPPER (a_pw);
       ELSE
          v_ret := ' ';
       END IF;
    
    END;

    Нам нужно больше селектов.

    P.S. почему в предпросмотре язык Java, хотя выбран SQL?

    slbsomeone, 05 Февраля 2014

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

    −166

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    SELECT ...
            FROM users_account
        ....
        WHERE   
            (T.userid=@userid or (@userid=null and @account!=null)) 
            and (T.account=@account or (@userid!=null and @account=null))

    Есть табличка с юзерами и счетами на которых у юзеров есть игровая валюта.
    Есть индекс по обоим этим полям (userid, account).
    Каждый такой запрос сканирует весь индекс,а не делает по ней поиск, чем и грузит субд.
    Но попытки оптимизировать запрос не увечались успехом,
    т.к. я так и не понял что хотел сказать автор в своём условии where.

    bliznezz, 05 Февраля 2014

    Комментарии (40)
  8. Python / Говнокод #14505

    −99

    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
    for i in range(0,len(m)):
        if i==0:
            f=False
            s += x[m[i]]
        else:
            if i==len(m)-1:
                if not f:
                    f=False
                    s += ',' + x[m[i]]
                else:
                    f=False
                    s += x[m[i]]
                break
            else:
                if m[i]-m[i-1]==1:
                    if m[i+1]-m[i]==1:
                        if not f:
                            f=True
                            s += '-'
                            continue
                        else:
                            continue
                    else:
                        if not f:
                            f=False
                            s += ',' + x[m[i]]
                        else:
                            f=False
                            s += x[m[i]]
                else:
                    f=False
                    s += ','+x[m[i]]

    Была задача: на вход(m) подается массив чисел, например [1,3,5,6,7], а на выходе получаем человекочитаемую строку(s) "пн,ср,пт-вс"
    Вышло такое из меня пару месяцев назад, теперь я никогда не смогу в нем разобраться х_х

    MAaxim91, 05 Февраля 2014

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

    +42

    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
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    #include <time.h>
    #include <string>
    #include <iostream>
    #include <functional>
    
    using namespace std::placeholders;
    
    class F
    {
            int inc;
    
    public:
    
            F( int inc_v ): inc( inc_v ) {};
    
            int calc( int x )
            {
                    return x + inc;
            };
    };
    
    F a1( -10 );
    F a2( 11 );
    
    int f1( int x )
    {
            return x - 10;
    };
    
    int f2( int x )
    {
            return x + 11;
    };
    
            struct my_ftor
            {
                    F *obj;
                    int (F::*meth)(int);
                    int operator()(int x)
                    {
                            return (obj->*meth)( x );
                    };
                    my_ftor() {};
                    my_ftor( F *x, int(F::*y)(int) ) : obj(x), meth(y) {};
            };
    
    template<typename functor_type>
    void test( std::function<functor_type(int)> filler, char *name )
    {
            const int size = 1000;
            const int iters = 10000;
    
            int beg_time, end_time;
    
            functor_type funcs[ size ];
    
            beg_time = clock();
            for ( int i = 0; i < iters; i++ )
            {
                    for ( int j = 0; j < size; j++ )
                    {
                            funcs[ j ] = filler(j);
                    }
            };
            end_time = clock();
            float creation = ((float)(end_time - beg_time) / CLOCKS_PER_SEC);
    
            beg_time = clock();
            int res = 0;
            for ( int i = 0; i < iters; i++ )
            {
                    for ( int j = 0; j < size; j++ )
                    {
                            res = funcs[ j ]( res );
                    };
            };
            end_time = clock();
            float execution = ((float)(end_time - beg_time) / CLOCKS_PER_SEC);
            std::cout << name << " creation time: " << creation << " execution time: " << execution << " result: " << res << "\n";
    }
    
    int main(int c, char * * v) 
    {
            test<int(*)(int)>( [](int i) {return i % 2 ? f1 : f2; }, "simple &function test" );
    
            test<std::function<int(int)>>( [](int i) {return i % 2 ? f1 : f2; }, "functor &function test" );
    
            test<std::function<int(int)>>( [](int i) {return i % 2 ? std::bind( &F::calc, &a1, _1 ) : std::bind( &F::calc, &a2, _1 ); }, "functor &object test" );
    
            test<my_ftor>( [](int i) {return i % 2 ? my_ftor( &a1, &F::calc ) : my_ftor( &a2, &F::calc ); }, "obj->*meth struct test" );
    
            std::cout << "END\n";
            return 0;
    }

    http://ideone.com/1iNzR
    Чем код так долго занимается?
    simple &function test creation time: 0.05 execution time: 0.09 result: 5000000
    functor &function test creation time: 0.51 execution time: 0.14 result: 5000000
    functor &object test creation time: 1.25 execution time: 0.14 result: 5000000
    obj->*meth struct test creation time: 0.12 execution time: 0.05 result: 5000000
    END

    LispGovno, 05 Февраля 2014

    Комментарии (58)
  10. C++ / Говнокод #14503

    +39

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    #include <iostream>
    #include <memory>
    
    struct Test {
        ~Test() { std::cout << "~Test\n"; }
    };
    
    int main() {
      std::shared_ptr<void> ptr( new Test );
      return 0;
    }

    http://ideone.com/xXPWhE

    Но:

    #include <iostream>
    #include <memory>

    struct Test
    {
    ~Test() { std::cout << "~Test\n"; }
    };

    int main() {
    std::shared_ptr<void> ptr( (void*) new Test );
    return 0;
    }
    http://ideone.com/jhNvpJ

    LispGovno, 05 Февраля 2014

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