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

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

    +164

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    function boolConvert($value){
            if(strtolower($value)=='true')
                return 1;
            if(strtolower($value)=='false')
                return 0;
            return $value;
        }

    GoodTalkBot, 05 Марта 2014

    Комментарии (27)
  3. C# / Говнокод #14582

    +136

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    public static string CreateBrowserCacheExtension(object key)
    {
        //...Остальной код опущен для ясности
        return "Cache=" + Math.Abs(key.GetHashCode());
    }

    Legacy code из проекта над которым я работаю.
    Косяк в том, что GetHashCode() иногда возвращает значение, равное System.Int32.MinValue.
    А это в свою очередь приводит к OverflowException, в случае с Math.Abs(...);

    pikowatt, 14 Февраля 2014

    Комментарии (27)
  4. PHP / Говнокод #14556

    +138

    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
    private function toDBFields($fields){
    			$db_fields = array();
    			if ($fields){
    				foreach($fields as $field){
    					switch ($field){
    
                            case 'p_bits':
                                if (!array_search('b1',$db_fields))	$db_fields[] = 'b1';
                                break;
                            case 'n2':
                                if (!array_search('b1',$db_fields))	$db_fields[] = 'b1';
                                break;
                            case 'n1':
                                if (!array_search('b1',$db_fields))	$db_fields[] = 'b1';
                                break;
    						case 'cntdf':
    							if (!array_search('cntdf',$db_fields))	$db_fields[] = 'cntdf';
    							break;
    						case 'first_pckg':
    						
    							break;
    						case 'az':
    							if (!array_search('az',$db_fields))		$db_fields[] = 'az';
    							break;
    						case 'akb_v':
    							//if (!array_search('b2',$db_fields))		$db_fields[] = 'b2';
    							if (!array_search('b3',$db_fields))		$db_fields[] = 'b3';
    							break;
    						case 'p1code':
                            case 'ignition_ap1':
    							if (!array_search('b4',$db_fields))		$db_fields[] = 'b4';
    							if (!array_search('b6',$db_fields))		$db_fields[] = 'b6';
    							break;
    						case 'fuel_p2':
                            case 'fuel_p2_debug':
                            case 'p2code':
    							if (!array_search('b5',$db_fields))		$db_fields[] = 'b5';
    							if (!array_search('b6',$db_fields))		$db_fields[] = 'b6';
    							break;
                            case 'fuel_p3':
                            case 'fuel_p3_debug':
    						case 'p3code':
                            case 'p3code_debug':
    							if (!array_search('b7',$db_fields))		$db_fields[] = 'b7';
    							if (!array_search('b6',$db_fields))		$db_fields[] = 'b6';
    							break;
                            case 'fuel_p3p4_2drt':
                                if (!array_search('b8',$db_fields))		$db_fields[] = 'b8';
    							if (!array_search('b7',$db_fields))		$db_fields[] = 'b7';
    							if (!array_search('b6',$db_fields))		$db_fields[] = 'b6';
    							break;
                            case 'fuel_p2p3':
                                if (!array_search('b5',$db_fields))		$db_fields[] = 'b5';
    							if (!array_search('b7',$db_fields))		$db_fields[] = 'b7';
    							if (!array_search('b6',$db_fields))		$db_fields[] = 'b6';
    							break;
                            case 'fuel_p4':
                            case 'p4code_debug':
    						case 'p4code':
    							if (!array_search('b8',$db_fields))		$db_fields[] = 'b8';
    							if (!array_search('b6',$db_fields))		$db_fields[] = 'b6';
    							break;
    					}
    				}
    			}
    
    			return $db_fields;

    Вот такой кейс который используется в системе для перевода типа переменной к названию поле в таблице ....

    Deimm, 13 Февраля 2014

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

    +130

    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
    public 	function actionSelect_model(){
    		
    	 
    		
    	$data = Yii::app()->db->createCommand()
    			->select('*')
    			->from('moto')
    			->where(' wmotoid = '.$_GET[id] )
    			->queryAll();
    			
    		foreach($data as $F=>$L){
    			echo '<option value="'.$L[moto_id].'">'.$L[moto_text].'</option>';
    			}
    		
    		
    	}

    Инет магазин на Yii. Контроллер.

    flashbag, 21 Октября 2013

    Комментарии (27)
  6. Си / Говнокод #13768

    +129

    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
    #include <stdio.h>
    #include <dlfcn.h>
    #include <getopt.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define int64 long long
    
    const char* help =
    "       -h,--help  это сообщение\
            -f,--func  имя функции\
            -n,--num   количество аргументов функции\
            -a,--arg1  первый аргумент\
            -b,--arg2  второй аргумент";
    
    struct option longopts[] ={
            {"func",1,NULL,'f'},
            {"help",1,NULL,'h'},
            {"num", 1,NULL,'n'},
            {"arg1",1,NULL,'a'},
            {"arg2",1,NULL,'b'},
            {0,0,0,0}
    };
    
    void strtoa(void** arg, char* optarg){
        int tmp;
        char* e;
        tmp = strtol(optarg,&e,10);
        if (e != optarg + strlen(optarg)){
            *arg = malloc(strlen(optarg));
            strcpy((char*)*arg,optarg);
        } else *arg = (void*)tmp;
    
    }
            
    int64 main(int argc, char** argv){
      
      int opt, num = 0;
      void *a,*b;
      char* e;
    
      void* (*func2)(void*,void*), 
            (*func1)(void*), 
            (*func0)(), *func = 0;
      
      while((opt = getopt_long(argc,argv,"f:h:n:a:b:",longopts,NULL)) != -1){
          switch (opt){
              case 'h':
                  fprintf(stderr,"%s",help);
                  exit(0);
              case 'f':
                  func = dlsym(NULL,optarg);
                  break;
              case 'n':
                  num = atoi(optarg);
                  if (num > 2) exit(1);
                  break;
              case 'a':
                  strtoa(&a,optarg);
                  break;
              case 'b':
                  strtoa(&b,optarg);
                  break;
          }
      }
      
      if(func != 0){
          switch (num){
              case 0: 
                func0 = func;
                (*func0)();
                break;
              case 1:
                func1 = func;
                (*func1)(a);
                break;
              case 2:
                func2 = func;
                (*func2)(a,b);
          }
      }
      
      return 0;
    }

    Запускает любую функцию линукс, содержащую до 2-х аргументов. В хелпе все написано. Собирать так c флагом -dl.
    Использовать так:
    a.out -f sleep -n 1 -a 1 - sleep на 1 секунду
    a.out -f printf -n 1 -a OK - выводит без конца строки
    вдохновлен этим:
    http://govnokod.ru/13763

    Я, crastinus97, говорю вам: этого способа нет в man. Я его сам открыл.

    crastinus, 11 Сентября 2013

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

    +135

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    private void MultiSelectBox1_CheckedChanged(object sender, EventArgs e) {
               if (MultiSelectBox1.Checked == true) {
                    openFileDialog1.Multiselect = true;
                } else {
                    openFileDialog1.Multiselect = false;
                }
            }

    Lokich, 30 Августа 2013

    Комментарии (27)
  8. Java / Говнокод #13252

    +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
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    public boolean isModified() {
    	return
    			!pluginEnabled.isSelected() == getSettings().pluginEnabled
    					|| !pathToContainerTextField.getText().equals(getSettings().pathToProjectContainer)
    					|| !pathToUrlGeneratorTextField.getText().equals(getSettings().pathToUrlGenerator)
    					|| !symfonyContainerTypeProvider.isSelected() == getSettings().symfonyContainerTypeProvider
    					|| !objectRepositoryTypeProvider.isSelected() == getSettings().objectRepositoryTypeProvider
    					|| !objectRepositoryResultTypeProvider.isSelected() == getSettings().objectRepositoryResultTypeProvider
    
    					|| !twigAnnotateRoute.isSelected() == getSettings().twigAnnotateRoute
    					|| !twigAnnotateTemplate.isSelected() == getSettings().twigAnnotateTemplate
    					|| !twigAnnotateAsset.isSelected() == getSettings().twigAnnotateAsset
    					|| !twigAnnotateAssetTags.isSelected() == getSettings().twigAnnotateAssetTags
    
    					|| !phpAnnotateTemplate.isSelected() == getSettings().phpAnnotateTemplate
    					|| !phpAnnotateService.isSelected() == getSettings().phpAnnotateService
    					|| !phpAnnotateRoute.isSelected() == getSettings().phpAnnotateRoute
    					|| !phpAnnotateTemplateAnnotation.isSelected() == getSettings().phpAnnotateTemplateAnnotation
    
    					|| !yamlAnnotateServiceConfig.isSelected() == getSettings().yamlAnnotateServiceConfig
    			;
    }

    Плагин для Intellij Idea...

    kostoprav, 28 Июня 2013

    Комментарии (27)
  9. Pascal / Говнокод #12375

    +90

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    program Project1;
    Var
      i,j : Integer;
    begin
        i := 300001; j := 300002;
        asm
         MOV EAX, I;
         XCHG EAX,j
         MOV I, EAX;
        end;
        Write(i,' ',j); Readln;
    end.

    Ещё один кулхацкерный метод перестановки значений двух чисел местами.

    Govnocoder#0xFF, 30 Декабря 2012

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

    −88

    1. 1
    2. 2
    3. 3
    % if ($ARGS{standalone_bhv}) {} else {
    %# Что-то делаем
    % }

    Mason. Автор явно знает толк..

    Elvenfighter, 24 Декабря 2012

    Комментарии (27)
  11. C++ / Говнокод #12280

    +13

    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
    #include <iostream>
    
    using namespace std;
    
    class Manager
    {
    private:
        int *ar;
        int _size;
    public:
        void CreateArray(int size);
        void ResizeArray(int size);
    
        void MakeIntArray(int iter, int number);
        int GetIntArray(int iter);
    
        ~Manager()
        {
            delete [] ar;
        }
    };
    
    void Manager::CreateArray(int size)
    {
        _size = size;
    
        ar = new int[size];
    }
    
    void Manager::ResizeArray(int size)
    {
        int *nar = new int[size];
    
        for(int i = 0; i < _size; i++)
        {
            *(nar+i) = *(ar+i);
        }
    
        _size = size;
    
        delete [] ar;
    
        *ar = *nar;
    }
    
    int Manager::GetIntArray(int iter)
    {
        return iter < _size ? *(ar+iter) : NULL;
    }
    
    void Manager::MakeIntArray(int iter, int number)
    {
        if(iter < _size)
            *(ar+iter) = number;
    }
    
    int main()
    {
        Manager Man;
        Man.CreateArray(10);
    
        for(int i = 0; i < 10; i++)
            Man.MakeIntArray(i, i);
    
        for(int i = 0; i < 10; i++)
            cout << Man.GetIntArray(i) << " ";
    
        Man.ResizeArray(20);
        cout << endl;
    
        for(int i = 10; i < 20; i++)
            Man.MakeIntArray(i, i);
    
        for(int i = 0; i < 20; i++)
            cout << Man.GetIntArray(i) << " ";
    
        return 0;
    }

    > Собственно написал менеджер для массива, хочется узнать правильно ли все сделано, и нет ли утечек памяти?
    Очередной шедевр с самизнаетеоткуда.

    LispGovno, 12 Декабря 2012

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