1. C++ / Говнокод #17153

    +52

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    int QTabBarPrivate::indexAtPos(const QPoint &p) const
    {
        Q_Q(const QTabBar);
        if (q->tabRect(currentIndex).contains(p))
            return currentIndex;
        for (int i = 0; i < tabList.count(); ++i)
            if (tabList.at(i).enabled && q->tabRect(i).contains(p))
                return i;
        return -1;
    }

    currentIndex - видимо откат заплатил

    PS
    Cкоро всю Qt на говнокод заливать придется.
    Плохое настроение или хотите поржать - открывайте исходники Qt в случайном месте - поднимаете себе настроение идиотскими решениями - профит

    fsmoke, 23 Ноября 2014

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

    +57

    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
    typedef deque<uint8_t> bn;
    #define cat2(b,e) b##e
    #define cat(b,e) cat2(b,e)
    #define fsi(i,s,e) for(size_t i(s), cat(i,cat(_fsi_end_,__LINE__))(e); i<cat(i,cat(_fsi_end_,__LINE__)); ++(i))
    void ml10(bn& n){
      n.push_front(0);
    }
    uint8_t ni(const bn& n, size_t i){
      if(n.size()<=i)
        return 0;
      else
        return n[i];
    }
    size_t ms(const bn& n1, const bn& n2){
      return (max) (n1.size(), n2.size());
    }
    void pb(uint8_t b1, uint8_t b2, uint8_t lc, uint8_t& r, uint8_t& c){
      uint8_t tr = (b1+b2+lc);
      r = tr & 1;
      c = !!(tr & 2);
    }
    bn bi(bn n){
      reverse(n.begin(), n.end());
      return n;
    }
    bn pl(const bn& n1, const bn& n2){
      bn r;
      uint8_t c=0,br=0;
      fsi(i,0,ms(n1, n2)){
        pb(ni(n1,i),ni(n2,i),c,br,c);
        r.push_back(br);
      }
      if (c)
        r.push_back(c);
      return r;
    }
    bn ml(bn n1, const bn& n2){
      bn r;
      fsi(i,0,n2.size()){
        if(n2[i])
          r=pl(r,n1);
        ml10(n1);
      }
      return r;
    }

    У меня к вам вопрос парни.
    В нашем отделе взяли олимпиадника. За ним не следили. Он написал много кода и ушел в гугл.
    Как бы за ним теперь код поправить? Хотябы конструкции языка типа for вернуть вместо например fsi?
    Код не из проекта. но его. В проекте похожий и даже хуже.
    Нужно типа утилиты препроцессор крестов для части макросов (все разворачивать макросы или инклудить хедеры очевидно нельзя).

    laMer007, 23 Ноября 2014

    Комментарии (68)
  3. JavaScript / Говнокод #17151

    +161

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    # coffee script
    d = 1 - Math.pow(d,n) * Math.pow((1/d -1),n)
    
    // java script
    d = 1 - Math.pow(d, n) * Math.pow(1 / d(-1), n);

    почему не любят кофескрипт, выпуск -дцатьпервый. потому что нельзя просто скопипастить формулу и не поймать Uncaught TypeError: number is not a function

    makc3d, 22 Ноября 2014

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

    +139

    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
    SGELISTENTRY *sgeListAdd(SGELIST *l, const char *id, void *data) {
       SGELISTENTRY *ret;
    
       sgeNew(ret, SGELISTENTRY);
       l->numberOfEntries++;
       if (l!=NULL) {
          ret->prev=l->last;
       } else {
          ret->prev=NULL;
       }
       if (l!=NULL && l->last!=NULL) {
          l->last->next=ret;
       }
       ret->next=NULL;
       ret->id=strdup(id);
       ret->data=data;
    
       if (l==NULL) return ret;
    
       if (l->first==NULL) l->first=ret;
       l->last=ret;
    
       return ret;
    }

    Эх, проверяй, не проверяй, один хрен все грохнется при l==NULL

    Pythoner, 21 Ноября 2014

    Комментарии (54)
  5. PHP / Говнокод #17149

    +160

    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
    /**
    * @param array $input
    * @return array
    * @throws Exception
    */
    protected function makeParams($input)
    {
        if (!is_array($input))
        {
            throw new \Exception('argument should be array');
        }
    
        $params = array();
    
        foreach ($input as $k=>$v)
        {
            $params[] = $v;
        }
    
        return $params;
    }

    свой array_values, с блекджеком и экзепшенами

    heyzea1, 21 Ноября 2014

    Комментарии (62)
  6. JavaScript / Говнокод #17148

    +154

    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
    if ((new Date(2014, 0, 1)).getHours() != 0 || new Date(2015, 0, 7).getHours() != 0) {
      // Переопределяем конструктор
      Date = (function (NativeDate) {
        var NewDate = function (Y, M, D, h, m, s, ms) {
          var length = arguments.length;
          var date = length === 1 && String(Y) === Y ? // isString(Y)
              new NativeDate(NewDate.parse(Y)) :
                  length >= 7 ? new NativeDate(NativeDate.UTC(Y, M, D, h, m, s, ms)) :
                  length >= 6 ? new NativeDate(NativeDate.UTC(Y, M, D, h, m, s)) :
                  length >= 5 ? new NativeDate(NativeDate.UTC(Y, M, D, h, m)) :
                  length >= 4 ? new NativeDate(NativeDate.UTC(Y, M, D, h)) :
                  length >= 3 ? new NativeDate(NativeDate.UTC(Y, M, D)) :
                  length >= 2 ? new NativeDate(NativeDate.UTC(Y, M)) :
                  length >= 1 ? new NativeDate(Y) :
              new NativeDate();
          if (length == 0) {
            date = new NativeDate(+date - date.getTimezoneOffset() * 60000);
          }
          date.constructor = NewDate;
          return this instanceof NativeDate ? date : date.toString();
        };
    
        if (NativeDate.now) {
          NewDate.now = function () {
            return new NewDate().getTime();
          };
        }
        if (NativeDate.UTC) {
          NewDate._UTC = NativeDate.UTC;
          NewDate.UTC = function (Y, M, D, h, m, s, ms) {
            var _date = new NewDate(NewDate._UTC.apply(this, arguments));
            return +_date - _date.getTimezoneOffset() * 60000;
          };
        }
        NewDate.prototype = NativeDate.prototype;
        NewDate.prototype.constructor = NewDate;

    И так далее, "фикс" для кривых таймзон в JS на винде http://habrahabr.ru/post/243685/

    asavartsov, 21 Ноября 2014

    Комментарии (95)
  7. PHP / Говнокод #17147

    +159

    1. 1
    2. 2
    3. 3
    static function beforeIblockElementAddOrUpdateResetResponseAuthor($fields){
        ...
    }

    n1ce22rus, 21 Ноября 2014

    Комментарии (97)
  8. PHP / Говнокод #17145

    +160

    1. 1
    echo 'some string'."\n";

    )))

    GoodTalkBot, 20 Ноября 2014

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

    +131

    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
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    namespace CA1
    {
        class Program
        {
            static void Main()
            {
                int buffer;
                int Cout = 0;
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader("file.txt");
                while ((line = file.ReadLine()) != null)
                {
                    buffer = Convert.ToInt32(line);
                    if(buffer > 0)
                    {
                        if(buffer / 2 > 5 && buffer / 2 < 49.5)
                        {
                            Cout++;
                        }
                    }
    
                    if (buffer < 0)
                    {
                        if (buffer / 2 < - 5 && buffer / 2 > - 49.5)
                        {
                            Cout++;
                        }
                    }
                }
                Console.WriteLine(Cout);
                Console.ReadLine();
            }
        }
    }

    Вычисление количества цифр в числе

    LightningAtom, 20 Ноября 2014

    Комментарии (138)
  10. Perl / Говнокод #17143

    −366

    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
    sub rotate {
        my ($filename, $max_entries) = @_;
        $max_entries //= 10; # TODO: make infinity available
    
        return if (! -f $filename);
    
        my ($name, $dir, $suffix) = fileparse($filename, qr/\.[^.]*$/);
    
        my %old_files = ();
        my $shift_max = 0; # maximum number of file that is going to be rotated
        my $glob = File::Spec->catfile($dir, $name . '.*' . $suffix);
        for my $file (glob qq/"$glob"/) {
            my ($number) = $file =~ /\.(\d+)\Q$suffix\E$/;
    
            $old_files{$number} = $file if (defined $number);
            $shift_max++ while (exists $old_files{$shift_max + 1});
        }
    
        $shift_max = $max_entries - 1 if ($shift_max >= $max_entries);
        # how many files to remove from the end
        my $to_remove = scalar(keys %old_files) - $max_entries;
    
        # rename and unlink
        for my $number (sort { $b <=> $a } keys %old_files) {
            my $file = $old_files{$number};
    
            if ($to_remove-- > 0) {
                print "## DEBUG: unlink $file\n";
                unlink($file)
                    or croak('Cannot unlink ' . $file);
            } elsif ($number <= $shift_max) {
                my $new_name = File::Spec->catfile($dir, $name . '.' . ($number + 1) . $suffix);
                print "## DEBUG: rename $file -> $new_name\n";
                rename($file, $new_name)
                    or croak('Cannot rename ' . $file . ' to ' . $new_name);
            }
        }
    
        my $new_name = File::Spec->catfile($dir, $name . '.1' . $suffix);
        print "## DEBUG: rename $filename -> $new_name\n";
        rename($filename, $new_name)
            or croak('Cannot rename ' . $filename . ' to ' . $new_name);
    }

    Продолжаем парад велосипедов. Сделал ротацию бекап-архивов :-{

    Elvenfighter, 20 Ноября 2014

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