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

    +2

    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
    public static int booleanToInt(Boolean value) {
    
            int res;
    
            if (value == null) {
    
                res = 0;
    
            } else {
                if (value) {
                    res = 1;
                } else {
                    res = 0;
                }
            }
    
            return res;
        }

    Добро пожаловать к нам в индийскую школу программирования!

    nekkiy, 28 Июня 2021

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

    +1

    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
    // https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qglobal.h?h=v5.13.1#n1017
    
    #if __cplusplus >= 201703L
    // Use C++17 if statement with initializer. User's code ends up in a else so
    // scoping of different ifs is not broken
    #define Q_FOREACH(variable, container)                                   \
    for (auto _container_ = QtPrivate::qMakeForeachContainer(container);     \
         _container_.i != _container_.e;  ++_container_.i)                   \
        if (variable = *_container_.i; false) {} else
    #else
    // Explanation of the control word:
    //  - it's initialized to 1
    //  - that means both the inner and outer loops start
    //  - if there were no breaks, at the end of the inner loop, it's set to 0, which
    //    causes it to exit (the inner loop is run exactly once)
    //  - at the end of the outer loop, it's inverted, so it becomes 1 again, allowing
    //    the outer loop to continue executing
    //  - if there was a break inside the inner loop, it will exit with control still
    //    set to 1; in that case, the outer loop will invert it to 0 and will exit too
    #define Q_FOREACH(variable, container)                                \
    for (auto _container_ = QtPrivate::qMakeForeachContainer(container); \
         _container_.control && _container_.i != _container_.e;         \
         ++_container_.i, _container_.control ^= 1)                     \
        for (variable = *_container_.i; _container_.control; _container_.control = 0)
    #endif

    А можно ли свой foreach сделать через какую-нибудь шаблонопарашу? Или тут, как обычно, нужна гомоиконность?

    j123123, 28 Июня 2021

    Комментарии (74)
  3. Куча / Говнокод #27485

    0

    1. 1
    Пиздец-оффтоп #22

    #1: https://govnokod.ru/26503 https://govnokod.xyz/_26503
    #2: https://govnokod.ru/26541 https://govnokod.xyz/_26541
    #3: https://govnokod.ru/26583 https://govnokod.xyz/_26583
    #4: https://govnokod.ru/26689 https://govnokod.xyz/_26689
    #5: https://govnokod.ru/26784 https://govnokod.xyz/_26784
    #5: https://govnokod.ru/26839 https://govnokod.xyz/_26839
    #6: https://govnokod.ru/26986 https://govnokod.xyz/_26986
    #7: https://govnokod.ru/27007 https://govnokod.xyz/_27007
    #8: https://govnokod.ru/27023 https://govnokod.xyz/_27023
    #9: https://govnokod.ru/27098 https://govnokod.xyz/_27098
    #10: https://govnokod.ru/27125 https://govnokod.xyz/_27125
    #11: https://govnokod.ru/27129 https://govnokod.xyz/_27129
    #12: https://govnokod.ru/27184 https://govnokod.xyz/_27184
    #13: https://govnokod.ru/27286 https://govnokod.xyz/_27286
    #14: https://govnokod.ru/27298 https://govnokod.xyz/_27298
    #15: https://govnokod.ru/27322 https://govnokod.xyz/_27322
    #16: https://govnokod.ru/27328 https://govnokod.xyz/_27328
    #17: https://govnokod.ru/27346 https://govnokod.xyz/_27346
    #18: https://govnokod.ru/27374 https://govnokod.xyz/_27374
    #19: https://govnokod.ru/27468 https://govnokod.xyz/_27468
    #20: https://govnokod.ru/27469 https://govnokod.xyz/_27469
    #21: https://govnokod.ru/27479 https://govnokod.xyz/_27479

    nepeKamHblu_nemyx, 26 Июня 2021

    Комментарии (2186)
  4. Куча / Говнокод #27484

    +1

    1. 1
    IT Оффтоп #97

    #67: https://govnokod.ru/27049 https://govnokod.xyz/_27049
    #68: https://govnokod.ru/27061 https://govnokod.xyz/_27061
    #69: https://govnokod.ru/27071 https://govnokod.xyz/_27071
    #70: https://govnokod.ru/27097 https://govnokod.xyz/_27097
    #71: https://govnokod.ru/27115 https://govnokod.xyz/_27115
    #72: https://govnokod.ru/27120 https://govnokod.xyz/_27120
    #73: https://govnokod.ru/27136 https://govnokod.xyz/_27136
    #74: https://govnokod.ru/27160 https://govnokod.xyz/_27160
    #75: https://govnokod.ru/27166 https://govnokod.xyz/_27166
    #76: https://govnokod.ru/27168 https://govnokod.xyz/_27168
    #77: https://govnokod.ru/27186 https://govnokod.xyz/_27186
    #78: https://govnokod.ru/27219 https://govnokod.xyz/_27219
    #79: https://govnokod.ru/27254 https://govnokod.xyz/_27254
    #80: https://govnokod.ru/27270 https://govnokod.xyz/_27270
    #81: https://govnokod.ru/27280 https://govnokod.xyz/_27280
    #82: https://govnokod.ru/27284 https://govnokod.xyz/_27284
    #83: https://govnokod.ru/27296 https://govnokod.xyz/_27296
    #84: https://govnokod.ru/27336 https://govnokod.xyz/_27336
    #85: https://govnokod.ru/27381 https://govnokod.xyz/_27381
    #86: https://govnokod.ru/27405 https://govnokod.xyz/_27405
    #87: https://govnokod.ru/27429 https://govnokod.xyz/_27429
    #88: https://govnokod.ru/27432 https://govnokod.xyz/_27432
    #89: https://govnokod.ru/27435 https://govnokod.xyz/_27435
    #90: https://govnokod.ru/27439 https://govnokod.xyz/_27439
    #91: https://govnokod.ru/27449 https://govnokod.xyz/_27449
    #92: https://govnokod.ru/27460 https://govnokod.xyz/_27460
    #93: https://govnokod.ru/27463 https://govnokod.xyz/_27463
    #94: https://govnokod.ru/27466 https://govnokod.xyz/_27466
    #95: https://govnokod.ru/27473 https://govnokod.xyz/_27473
    #96: https://govnokod.ru/27478 https://govnokod.xyz/_27478

    nepeKamHblu_nemyx, 26 Июня 2021

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

    +5

    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
    #include <unistd.h>
    #include <stdio.h>
    #include <limits.h>
    
    template<size_t Size> struct static_string {char data[Size];};
    template<size_t ... Indexes>struct index_sequence {};
    template<size_t Size, size_t ... Indexes>
    constexpr static_string<sizeof ... (Indexes) + 1> make_static_string(const static_string<Size>& str,index_sequence<Indexes ...>) {return {str.data[Indexes] ..., '\0'};}
    constexpr static_string<1> make_static_string() {return {'\0'};}
    template<size_t Size, size_t ... Indexes>
    struct make_index_sequence : make_index_sequence<Size - 1, Size - 1, Indexes ...> {};
    template<size_t Size>
    constexpr static_string<Size> make_static_string(const char (& str)[Size]) {return make_static_string(str, make_index_sequence<Size - 1>{});}
    template<size_t ... Indexes>
    struct make_index_sequence<0, Indexes ...> : index_sequence<Indexes ...> {};
    template<size_t Size, size_t ... Indexes>
    constexpr static_string<sizeof ... (Indexes) + 1> make_static_string(const char (& str)[Size],index_sequence<Indexes ...>) {return {str[Indexes] ..., '\0'};}
    template<size_t Size>
    constexpr size_t static_string_find(const static_string<Size>& str, char ch, size_t from, size_t nth) {return Size < 2 || from >= Size - 1 ? UINT_MAX :str.data[from] != ch ? static_string_find(str, ch, from + 1, nth) :nth > 0 ? static_string_find(str, ch, from + 1, nth - 1) : from;}
    template<size_t Size>
    constexpr size_t static_string_find_0(const static_string<Size>& str, char ch, size_t from, size_t nth) {return Size < 2 || from >= Size - 1 ? 0 : str.data[from] != ch ? static_string_find_0(str, ch, from + 1, nth) :nth > 0 ? static_string_find(str, ch, from + 1, nth - 1) : from;}
    template<size_t Size1, size_t ... Indexes1, size_t Size2, size_t ... Indexes2>
    constexpr static_string<Size1 + Size2 - 1> static_string_concat_2(const static_string<Size1>& str1, index_sequence<Indexes1 ...>,const static_string<Size2>& str2, index_sequence<Indexes2 ...>) {return {str1.data[Indexes1] ..., str2.data[Indexes2] ..., '\0'};}
    template<size_t Size1, size_t Size2>
    constexpr static_string<Size1 + Size2 - 1> static_string_concat_2(const static_string<Size1>& str1, const static_string<Size2>& str2) {return static_string_concat_2(str1, make_index_sequence<Size1 - 1>{},str2, make_index_sequence<Size2 - 1>{});}
    template<size_t Begin, size_t End, size_t ... Indexes>
    struct make_index_subsequence : make_index_subsequence<Begin, End - 1, End - 1, Indexes ...> {};
    template<size_t Pos, size_t ... Indexes>
    struct make_index_subsequence<Pos, Pos, Indexes ...> : index_sequence<Indexes ...> {};
    template<size_t Begin, size_t End, size_t Size>
    constexpr static_string<End - Begin + 1> static_string_substring(const static_string<Size>& str) {return make_static_string(str, make_index_subsequence<Begin, End>{});}
    template<size_t Begin, size_t Size>
    constexpr static_string<Size - Begin> static_string_suffix(const static_string<Size>& str) {return static_string_substring<Begin, Size - 1>(str);}
    #define remove_underscore(arg) ([] () __attribute__((always_inline)) {constexpr auto a = static_string_find(make_static_string(arg),'_',0,0) == UINT_MAX? make_static_string(arg):static_string_concat_2(static_string_concat_2(static_string_substring<0,static_string_find_0(make_static_string(arg),'_',0,0)>(make_static_string(arg)),static_string_suffix<static_string_find_0(make_static_string(arg),'_',0,0)+1>(make_static_string(arg))),make_static_string("\0"));return a;}().data)
    
    int main()
    {
        puts(remove_underscore("_testtest"));
        puts(remove_underscore("test_test"));
        puts(remove_underscore("testtest_"));
    }

    Убогий constexpr в c++11

    mittorn, 25 Июня 2021

    Комментарии (46)
  6. Куча / Говнокод #27481

    −1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    На этом ресурсе неоднократно появляются "говнокоды" с кривым форматированием. 
    Не всё что криво отформатированно, является говнокодом с точки зрения самого кодирования.
    Отчасти такие коды - ето копипаст из лаб студентов,
    которых заставили скопипастнуть свой код в какой-нибудь отчет, а возиться с вордовской версткой мало кому охота ага).
    О том, что формат doc(x) для сорцов мало подходит, мало каких преподов волнует. Им отчет же нужно показать комиссии.
    Столкнулась с этим после неоднократных переделок лаб по предметам, связанным с программированием.

    JaneBurt, 24 Июня 2021

    Комментарии (1358)
  7. Си / Говнокод #27477

    +3

    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
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DEREF(x) ({*(x);})
    #define PTR(x) ({&(x);})
    #define PTR_T(x) typeof(typeof(x) *)
    #define DEREF_T(x) typeof( ({typeof(x) DEREF_T; *DEREF_T;}) )
    
    
    void add5(PTR_T(int) a)
    {
      DEREF(a)+= 5;
    }
    
    int main(void)
    {
      int a = 0;
      printf("%d\n", a);
      add5(PTR(a));
      printf("%d\n", a);
      
      int b = 5;
      PTR_T(b) b_p = PTR(b); // int *
      printf("%d\n", DEREF(b_p) );
      
      DEREF_T(b_p) c = 666; // int
      printf("%d", c);
      
      return EXIT_SUCCESS;
    }

    https://govnokod.ru/27476#comment637183

    ASD_77:
    потому что я люблю Си и С++ но ненажижу * и & ... поэтому я решил исправить этот недочет в мире

    Исправил, проверь. Компилится только в GCC, всё-таки Clang не 100% совместим c гнутыми расширениями.

    j123123, 21 Июня 2021

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

    +1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    function main() {
    
        let a = 10;
    
        function f() {
            print(a);
        }
    
        f();
    
        print("done.");
    }

    С виду этот простой кодик - это то что не может С и я так долго трахался что бы смочь его проимлементировать.... но я бля сделал

    ASD_77, 21 Июня 2021

    Комментарии (113)
  9. Куча / Говнокод #27475

    +1

    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
    dirty_boostrap_test() ->
        SourceTab = ets:new(source, [public, named_table]),
        ReplicaTab = ets:new(replica, [public, named_table]),
        %% Insert some initial data:
        ets:insert(source, {1, 1}),
        ets:insert(source, {2, 2}),
        ets:insert(source, {3, 3}),
        try
            register(testcase, self()),
            Replica = spawn_link(fun replica/0),
            register(replica, Replica),
            %% "importer" process emulates mnesia_tm:
            spawn_link(fun importer/0),
            %% "bootstrapper" process emulates bootstrapper server:
            spawn_link(fun bootstrapper/0),
            receive
                done ->
                    SrcData = lists:sort(ets:tab2list(source)),
                    RcvData = lists:sort(ets:tab2list(replica)),
                    ?assertEqual(SrcData, RcvData)
            end
        after
            ets:delete(SourceTab),
            ets:delete(ReplicaTab)
        end.
    
    importer() ->
        Ops = [ {write, 3, 3}
              , {write, 4, 4}
              , {write, 4, 5}
              , {delete, 2}
              ],
        lists:map(fun(OP) ->
                          import_op(source, OP),
                          %% Imitate mnesia event (note: here we send it
                          %% directly to the replica process bypassing
                          %% the agent):
                          replica ! {tlog, OP}
                  end,
                  Ops),
        replica ! last_trans.
    
    replica() ->
        receive
            {bootstrap, K, V} ->
                ets:insert(replica, {K, V}),
                replica();
            bootstrap_done ->
                replay()
        end.
    
    replay() ->
        receive
            {tlog, Op} ->
                import_op(replica, Op),
                replay();
            last_trans ->
                testcase ! done
        end.
    
    import_op(Tab, {write, K, V}) ->
        ets:insert(Tab, {K, V});
    import_op(Tab, {delete, K}) ->
        ets:delete(Tab, K).
    
    bootstrapper() ->
        {Keys, _} = lists:unzip(ets:tab2list(source)),
        [replica ! {bootstrap, K, V} || K <- Keys, {_, V} <- ets:lookup(source, K)],
        replica ! bootstrap_done.

    Follow-up к треду про то, как делать снепшоты.

    CHayT, 20 Июня 2021

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

    0

    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
    Game::Game()
    {
        run = true;//флаг признак нажатия кнопки выхода F5
        Matrix = new int* [8];//Поле 64 ячейки - значения 0 - для пустой ячейки, для игрока каждая пешка-шашка от 1 до 9, для компьютера значения в матрице от 10 до 18
        for (int i = 0; i < 8; i++)
            Matrix[i] = new int[8];
        //Квадраты координат нужны чтобы программа знала какие ячейки над указателем мыши, 64 квадрата
        QuadCoorXleft = new int* [8];//каждой ячейки матрицы Matrix соответстует квадрат координат для мыши xleft означает левую координату x
        QuadCoorXright = new int* [8];//xright - правая x
        QuadCoorYdown = new int* [8];//верхняя y координата
        QuadCoorYup = new int* [8];//нижняя y координата
        for (int i = 0; i < 8; i++)
        {
            QuadCoorXleft[i] = new int[8];
            QuadCoorXright[i] = new int[8];
            QuadCoorYdown[i] = new int[8];
            QuadCoorYup[i] = new int[8];
        }
        //Координаты пешек для отрисовки
        ChessX = new double[18];//X
        ChessY = new double[18];//Y
        //Выделяемая пешка ее координаты и значения
        ActiveX = -1;//X
        ActiveY = -1;//Y
        Active = -1;//Value
        firstplayer = true;//флаг того что можете игрок 1й ходить
        secondplayer = false;//флаг того что можете игрок 2й ходить
        ai = new bool[18];//ячейки флаги того что пешка на финишной позиции
        chessai tmp;
        for (int i = 0; i < 18; i++)
        {
            ai[i] = false;
            if (i > 8)
            {
                tmp.ai = ai[i];
                tmp.value = i+1;
                Ai.push_back(tmp);//Вектор с флагами финиша каждой пешки для искуственного интеллекта
            }
        }
        aicountfirstrow = 0;//счетчик кол-ва пешек ИИ(искуственного интеллекта) на верхней строчке(0-я)
        aicountsecondrow = 0;//счетчик кол-ва пешек ИИ на предверхней строчке(1-я)
        aicountthirdrow = 0;//счетчик кол-ва пешек ИИ на предпредверхней строчке(2-я)
    }

    https://github.com/Beginerok/DominiGames/blob/master/Domini/Chess/Chess/Game.cpp

    https://habr.com/ru/post/563398/
    Странные шахматы как тестовое задание

    PolinaAksenova, 18 Июня 2021

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