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

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

    −92

    1. 1
    a < 5 and a or 5

    Вы все еще думаете, что сочетание and'а и or'а это тернарник? Тогда мы идем к вам :)

    http://ideone.com/qC0TyS

    bormand, 28 Августа 2013

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

    +125

    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
    //сервер сайд (из colors.php)
    
    $colors = [
        'banana'   => 'yellow',
        'orange'   => 'orange',
        'cucumber' => 'green'
    ];
    
    echo $colors[$_GET['fruit']];
    
    
    //клиент сайд (js)
    var fruits = ['banana', 'orange', 'cucumber']
    for (var i in fruits) {
        var fruit = fruits[i]
        $.get('colors.php?fruit='+fruit, function(color){
            document.write(fruit +' is '+ color +'<br/>')
        })
    }

    Этот код показывают и задают вопрос почти на любом собеседовании на должность web-программита:
    Что здесь неверно и как это исправить?

    LispGovno, 02 Марта 2013

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

    +95

    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
    // same as Callable but without exception
    public interface  Executable<T>
    {
     	public T call();
    }
    /*В другом классе: методы для конверсии туда-сюда */
    	public Callable<T> toCallable(final Executable<T> ex){
     		return new Callable<T>(){
    			public T call() throws Exception{
    				return ex.call();
    			}
    		};
    	}
    	public Executable<T> toExecutable(final Callable<T> c)
     	{
     		return new Executable<T>(){
    			public T call(){
    		 		try{
    					return c.call();
    				}catch (Exception e){
    					throw new RuntimeException(e);
    				}
    			}
    		};
    	}

    3.14159265, 11 Июня 2012

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

    +76

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    @Override
        public ResponseBag[] send(SmsBag[] smsBag) {
            ArrayList<ResponseBag> responseList = new ArrayList<ResponseBag>();
            for(SmsBag bag : smsBag) {
                responseList.add(super.send(bag));
            }
            ResponseBag[] responseBag = new ResponseBag[responseList.size()];
            return responseList.toArray(responseBag);
        }

    manyrus, 18 Августа 2011

    Комментарии (82)
  6. PHP / Говнокод #6752

    +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
    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
    $month = explode(".",$arResult['voting']['DATE_CREATE']);
      $day = $month['0'];
      $yarh = $month['2'];
      $month = $month['1'];
      switch ($month) {
        case 01:
            $monthtext = "января";
            break;
        case 02:
            $monthtext = "февраля";
            break;
        case 03:
            $monthtext = "марта";
            break;
        case 04:
            $monthtext = "апреля";
            break;
        case 05:
            $monthtext = "мая";
            break;
        case 06:
            $monthtext = "июня";
            break;
        case 07:
            $monthtext = "июля";
            break;
        case 08:
            $monthtext = "августа";
            break;
        case 09:
            $monthtext = "сентября";
            break;
        case 10:
            $monthtext = "октября";
            break;
        case 11:
            $monthtext = "ноября";
            break;
        case 12:
            $monthtext = "декабря";
            break;
      }

    Как думаете что произойдет в августе(и последующих месяцах) с таким кодом? :-)

    P.S. $arResult['voting']['DATE_CREATE'] в формате %d.%m.%Y

    develx0, 25 Мая 2011

    Комментарии (82)
  7. Java / Говнокод #26808

    +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
    import java.util.concurrent.TimeUnit;
    import java.util.regex.Pattern;
    import static java.lang.System.*;
    import static java.util.concurrent.TimeUnit.NANOSECONDS;
    
    public class Rep implements CharSequence
    {
        String str = null;
        int    len;
        char  base;
    
        public Rep(char x, int count)
        {
            this.len  = count;
            this.base = x;
        }
    
        @Override
        public int length()
        {
            return len;
        }
    
        @Override
        public char charAt(int index)
        {
            return base;
        }
    
        @Override
        public CharSequence subSequence(int beginIndex, int endIndex)
        {
            if (beginIndex < 0) {
                throw new StringIndexOutOfBoundsException(beginIndex);
            }
            if (endIndex > this.len) {
                throw new StringIndexOutOfBoundsException(endIndex);
            }
            int subLen = endIndex - beginIndex;
            if (subLen < 0) {
                throw new StringIndexOutOfBoundsException(subLen);
            }
            return ((beginIndex == 0) && (endIndex == this.len)) ? this
                    : new Rep(this.base, subLen);
        }
    
        @Override
        public String toString()
        {
            return null!=str ?  str : (this.str  = new String(new char[]{base}).repeat(len));
        }
    
        public static void main(String... args)
        {
            long ns = 0L;
            Pattern lazy   = Pattern.compile("^(11+?)\\1+$");
            Pattern greedy = Pattern.compile("^(11+?)\\1+$");
            ns=nanoTime();  lazy  .matcher(new Rep('1',100160079)).matches(); out.println( NANOSECONDS.toMillis(nanoTime()-ns));
            ns=nanoTime();  greedy.matcher(new Rep('1',100160079)).matches();out.println( NANOSECONDS.toMillis(nanoTime()-ns));
            ns=nanoTime();  greedy.matcher(new Rep('1',1165139)).matches();out.println( NANOSECONDS.toMillis(nanoTime()-ns));
    
            ns=nanoTime(); "1".repeat( 100160079 ).matches("^(11+?)\\1+$") ; out.println("Lazy String:"+ NANOSECONDS.toMillis(nanoTime()-ns));
            ns=nanoTime(); "1".repeat( 100160079 ).matches("^(11+)\\1+$") ; out.println("Greedy String:"+ NANOSECONDS.toMillis(nanoTime()-ns)); 
        }
    
    }

    Так как в «Йажа» регулярки работают не на строках, а на интерфейсе CharSequence https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/CharSequence.html

    Решил что можно сделать тупую реализацию CharSequence для строк из повторяющегося символа.

    https://ideone.com/8eYFU7

    3.14159265, 16 Июля 2020

    Комментарии (81)
  8. Си / Говнокод #26801

    +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
    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
    /* meta */
    typedef struct r_anal_meta_item_t {
    	ut64 from;
    	ut64 to;
    	ut64 size;
    	int type;
    	int subtype;
    	char *str;
    	int space;
    } RAnalMetaItem;
    
    typedef struct {
    	struct r_anal_t *anal;
    	int type;
    	int rad;
    	SdbForeachCallback cb;
    	void *user;
    	int count;
    	struct r_anal_type_function_t *fcn;
    } RAnalMetaUserItem;
    
    typedef struct r_anal_range_t {
    	ut64 from;
    	ut64 to;
    	int bits;
    } RAnalRange;
    
    #define R_ANAL_UNMASK_TYPE(x) (x&R_ANAL_VAR_TYPE_SIZE_MASK)
    #define R_ANAL_UNMASK_SIGN(x) (((x& R_ANAL_VAR_TYPE_SIGN_MASK)>> R_ANAL_VAR_TYPE_SIGN_SHIFT)==R_ANAL_VAR_TYPE_UNSIGNED)?0:1
    
    #define R_ANAL_GET_OFFSET(x,y,z) \
    	(x && x->binb.bin && x->binb.get_offset)? \
    		x->binb.get_offset (x->binb.bin, y, z): -1
    enum {
    	R_ANAL_DATA_TYPE_NULL = 0,
    	R_ANAL_DATA_TYPE_UNKNOWN = 1,
    	R_ANAL_DATA_TYPE_STRING = 2,
    	R_ANAL_DATA_TYPE_WIDE_STRING = 3,
    	R_ANAL_DATA_TYPE_POINTER = 4,
    	R_ANAL_DATA_TYPE_NUMBER = 5,
    	R_ANAL_DATA_TYPE_INVALID = 6,
    	R_ANAL_DATA_TYPE_HEADER = 7,
    	R_ANAL_DATA_TYPE_SEQUENCE = 8,
    	R_ANAL_DATA_TYPE_PATTERN = 9,
    };
    
    // used from core/anal.c
    #define R_ANAL_ADDR_TYPE_EXEC      1
    #define R_ANAL_ADDR_TYPE_READ      1 << 1
    #define R_ANAL_ADDR_TYPE_WRITE     1 << 2
    #define R_ANAL_ADDR_TYPE_FLAG      1 << 3
    #define R_ANAL_ADDR_TYPE_FUNC      1 << 4
    #define R_ANAL_ADDR_TYPE_HEAP      1 << 5
    #define R_ANAL_ADDR_TYPE_STACK     1 << 6
    #define R_ANAL_ADDR_TYPE_REG       1 << 7
    #define R_ANAL_ADDR_TYPE_PROGRAM   1 << 8
    #define R_ANAL_ADDR_TYPE_LIBRARY   1 << 9
    #define R_ANAL_ADDR_TYPE_ASCII     1 << 10
    #define R_ANAL_ADDR_TYPE_SEQUENCE  1 << 11
    
    #define R_ANAL_ARCHINFO_MIN_OP_SIZE 0
    #define R_ANAL_ARCHINFO_MAX_OP_SIZE 1
    #define R_ANAL_ARCHINFO_ALIGN 2
    #define R_ANAL_ARCHINFO_DATA_ALIGN 4

    Как нужно называть идентификаторы.

    Источник:
    https://github.com/radareorg/radare2/

    TEH3OPHblu_nemyx, 11 Июля 2020

    Комментарии (81)
  9. PHP / Говнокод #26754

    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
    /**
         * @param int $filterType
         * @return int
         */
        public static function performanceFarmerStatFilterTranslator(int $filterType): int
        {
            switch (true) {
                case 1 === $filterType:
                    return Task::TYPE_PREPARE_ACCOUNTS_BY_REQUEST;
                case 2 === $filterType:
                    return Task::TYPE_PREPARE_GOOGLE_ACCOUNTS_BY_REQUEST;
                case 3 === $filterType:
                    return Task::TYPE_PREPARE_TWITTER_ACCOUNTS_BY_REQUEST;
                case 4 === $filterType:
                    return Task::TYPE_PASSWORD_MATCHING;
                case 6 === $filterType:
                    return Task::TYPE_ACCOUNT_MARKUP;
                case 7 === $filterType:
                    return Task::TYPE_REPLACE_ACCOUNTS;
            }
    
            return 0;
        }

    FireDemonru, 15 Июня 2020

    Комментарии (81)
  10. Си / Говнокод #26384

    +1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    // https://github.com/microsoft/PQCrypto-SIDH/blob/ebd1c80a8ac35e9ca2ef9680291a8a43b95a3bfa/src/random/random.c#L22
    
    static __inline void delay(unsigned int count)
    {
        while (count--) {}
    }

    ... guess what?

    j123123, 25 Января 2020

    Комментарии (81)
  11. Java / Говнокод #25348

    +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
    when {
                    (defaultCurrency != null) -> {
                        when {
                            (currenciesList == null) -> {
                                currenciesList = mutableListOf(defaultCurrency)
                            }
                            (currenciesList?.isEmpty() == true) -> {
                                currenciesList?.add(defaultCurrency)
                            }
                            else -> {
                                if (currenciesList?.contains(defaultCurrency) == false) {
                                    defaultCurrency = currenciesList?.first()
                                }
                            }
                        }
                    }
                    else -> {
                        when {
                            ((currenciesList == null) || (currenciesList?.isEmpty() == true)) -> {
                                throw IllegalArgumentException("Default currency and list of currencies from terminal configuration are empty")
                            }
                            else -> {
                                defaultCurrency = currenciesList?.first()
                            }
                        }
                    }
                }

    Интерн сражается со скобочками.

    peanutwolf, 01 Февраля 2019

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