1. PHP / Говнокод #17067

    +156

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    // где то: 
    $_SESSION['type_school'] = $db->query("SELECT * FROM ou_type ",array(),'kassoc');
    
    // а потом:
    $smarty->assign('type_school',$_SESSION['type_school']);
    
    //и в шаблоне:
    <td>{$type_school[$v.base_type].type_name}</td>

    $v.base_type - тоже результат запроса. Left/Right/Inner Join - не, не слышал...

    А потом: чёта проект тормозит...
    Я так подозреваю кто то таким методом оптимизацию и кеширование сделал, ну чтобы бд лишний раз не грузить.. Не, ну, а чё, зачем каждый раз базу данных мучить при открытии одной! странички со списком *****, давайте одну таблицу-справочник из бд откопируем каждому пользователю в его сессию отдельно и будем читать его при каждом session_start даже если на текущей странице нам этот справочник не нужен.

    vGhost, 05 Ноября 2014

    Комментарии (12)
  2. Java / Говнокод #17066

    +79

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    Object o1 = true ? Integer.valueOf(1) : Double.valueOf(2.0);
    Object o2;
    
    if (true) {
    	o2 = Integer.valueOf(1);
    } else {
    	o2 = Double.valueOf(2.0);
    }
    
    System.out.println(o1);
    System.out.println(o2);

    Такие вот в Java интересные типы-обёртки.

    Проверить себя: http://ideone.com/BrhREq

    Источник: http://java.dzone.com/articles/10-things-you-didnt-know-about

    someone, 05 Ноября 2014

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

    +137

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    //Перевести строку в JSON
    #region ConvertToJson
    public static string ConvertToJson(string str)
    {
        return str.Replace(@"/", string.Empty).Replace(@"~", string.Empty).Replace(@":", string.Empty).Replace(@"{", string.Empty).Replace(@"}", string.Empty).Replace("\"", string.Empty).Replace(",", string.Empty).Replace("\n", string.Empty);
    }
    #endregion

    json сериализатор из строки в строку, написанный в одну строчку

    Lokich, 05 Ноября 2014

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

    +53

    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
    const char *GetExternalFilesDir ()
      {
        assert (started);
        static std::string s="";
        if (s.length()==0)
        {
          LOGI("Try get external files dir");
          jclass c; jmethodID m; jobject o;
    
          c = env->FindClass        ("android/app/NativeActivity");
          m = env->GetMethodID      (c, "getExternalFilesDir", "(Ljava/lang/String;)Ljava/io/File;");
          o = env->CallObjectMethod  (appState->activity->clazz, m, NULL);
    
          c = env->GetObjectClass    (o);
          m = env->GetMethodID      (c, "getAbsolutePath", "()Ljava/lang/String;");
          o = env->CallObjectMethod  (o, m);
          jstring jo = (jstring)o;
    
          const char *path = env->GetStringUTFChars(jo, NULL);
          s=path;
          env->ReleaseStringUTFChars(jo, path);
          s+='/';
          LOGI("Path for program's data files is %s", s.c_str());
        }
        return s.c_str();
      }

    Этот код был написан одним очень известным в очень узких кругах человеком. Будем надеяться, что он придет в обсуждение и прокомментирует его.

    DlangGovno, 04 Ноября 2014

    Комментарии (83)
  5. Куча / Говнокод #17063

    +126

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    If v1 = 1 Then
      t1 = " "
    ElseIf v1 = 2 Then
      t1 = "q"
    ElseIf v1 = 3 Then
      t1 = "w"
    ElseIf v1 = 4 Then
      t1 = "e"
    ElseIf v1 = 5 Then
      t1 = "r"
    ElseIf v1 = 6 Then
      t1 = "t"
    ElseIf v1 = 7 Then
      t1 = "y"
    ElseIf v1 = 8 Then
      t1 = "u"
    ElseIf v1 = 8 Then
      t1 = "u"
    ElseIf v1 = 9 Then
      t1 = "i"
    ElseIf v1 = 10 Then
      t1 = "o"
    ElseIf v1 = 11 Then
      t1 = "p"
    ElseIf v1 = 12 Then
      t1 = "a"
    ElseIf v1 = 13 Then
      t1 = "s"
    ElseIf v1 = 14 Then
      t1 = "d"
    ElseIf v1 = 15 Then
      t1 = "f"
    ElseIf v1 = 16 Then
      t1 = "g"
    ElseIf v1 = 17 Then
      t1 = "h"
    ElseIf v1 = 18 Then
      t1 = "j"
    ElseIf v1 = 19 Then
      t1 = "k"
    ElseIf v1 = 20 Then
      t1 = "l"
    ElseIf v1 = 21 Then
      t1 = "z"
    ElseIf v1 = 22 Then
      t1 = "x"
    ElseIf v1 = 23 Then
      t1 = "c"
    ElseIf v1 = 24 Then
      t1 = "v"
    ElseIf v1 = 25 Then
      t1 = "b"
    ElseIf v1 = 26 Then
      t1 = "n"
    ElseIf v1 = 27 Then
      t1 = "m"
    ElseIf v1 = 28 Then
      t1 = "!"
    ElseIf v1 = 29 Then
      t1 = "?"
    EndIf
    If v2 = 1 Then
      t2 = " "
    ElseIf v2 = 2 Then
      t2 = "q"
    ElseIf v2 = 3 Then
      t2 = "w"
    ElseIf v2 = 4 Then
      t2 = "e"
    ElseIf v2 = 5 Then
      t2 = "r"
    ElseIf v2 = 6 Then
      t2 = "t"
    ElseIf v2 = 7 Then
      t2 = "y"
    ElseIf v2 = 8 Then
      t2 = "u"
    ElseIf v2 = 8 Then
      t2 = "u"
    ElseIf v2 = 9 Then
      t2 = "i"
    ElseIf v2 = 10 Then
      t2 = "o"
    ElseIf v2 = 11 Then
      t2 = "p"
    ElseIf v2 = 12 Then
      t2 = "a"
    ElseIf v2 = 13 Then
      t2 = "s"
    ElseIf v2 = 14 Then
      t2 = "d"
    ElseIf v2 = 15 Then
      t2 = "f"
    ElseIf v2 = 16 Then
      t2 = "g"
    ElseIf v2 = 17 Then
      t2 = "h"
    ElseIf v2 = 18 Then
      t2 = "j"
    ElseIf v2 = 19 Then

    Благодаря этому примеру, мы ещё раз убеждаемся, что SmallBasic- не язык. В оригинале программы есть ещё 350 строк отборного говнокода такого плана.

    pure_is_koda, 04 Ноября 2014

    Комментарии (14)
  6. ActionScript / Говнокод #17062

    −91

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    [Embed(source = "/assets/video_preview/VideoSlideThumb.png")]
    public static const VIDEO_PREVIEW: Class;
    
    public static function get videoPreviewBD(): BitmapData
    {
    	var image : Bitmap = new VIDEO_PREVIEW ();
    	return image.bitmapData.clone();		
    }

    Когда-то давно бытовала такая пословица: What Intel giveth Microsoft taketh away. Но похоже что переходное красное знамя подхватили и в других организациях по-меньше.

    wvxvw, 04 Ноября 2014

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

    +135

    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
    void *list_find_data(struct node *llist, void *data, int (*cmp_func)(void *, void *))
    {
    	struct node* it_node = NULL;
    
    	if (NULL != cmp_func)
    	{
    		for(it_node = llist; (NULL != it_node); it_node = it_node->next)
    	    {
                if (NULL != it_node->data)
                {
                    if (0 == cmp_func(data, it_node->data))
                    {
                        break;
                    }
                }
            }
    	}
    	return ((NULL != it_node) && (NULL != it_node->data)) ? it_node->data : NULL;
    }

    Велосипедисты жгут. О glibопараше не наслышаны.

    codemonkey, 04 Ноября 2014

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

    −863

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    SELECT *
    	FROM session s
    	WHERE s.platform IS NOT NULL
    	GROUP BY s.user_id
    	HAVING MAX(s.open_utc)

    Но хоть можно понять что сказать пытались, и на том спасибо.

    Lowezar, 03 Ноября 2014

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

    +55

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    void input (char inpt [64])
    {
    	for (;;)
    	{
    		int i(0);
    		cin >> inpt;
    		i ++;
    		if (inpt[i] == '\n')
    			inpt[i] = '\0';break;
    	}
    }

    normbo, 03 Ноября 2014

    Комментарии (14)
  10. PHP / Говнокод #17058

    +156

    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
    <?php
        class view {
            protected $dir; //templates directory
            protected $lang; //language
            protected $authorized;
            protected $user;
    
            protected function getCache($template) {
                //return false; //uncomment for developing
                if (!isset($_SESSION['cache_' . $template])) return false;
                return $_SESSION['cache_' . $template];
            }
    
            protected function addCache($template, $content) {
                $_SESSION['cache_' . $template] = $content;
            }
    
            public function __construct($dir, localization $lang, user $user) {
                $this->dir = $dir; 
                $this->authorized = (bool) $user->authorized;
                $this->user = $user;
                $this->lang = $lang;
            }
    
            public function invoke($template, $params = [], $return = false, $quests = []) { //can be called w/o params
                $filename = ROOT . '/' . $this->dir . '/tpl/' . $template . '.tpl';
                $lang = $this->lang->getData();
                $content = $this->getCache($template);
                if (!$content) {
                    $f = fopen($filename, 'a+');
                    $content = fread($f, (filesize($filename) > 0 ? filesize($filename) : 1));
                    $this->addCache($template, $content);
                }
                foreach ($params as $key => $value) {
                    $content = str_ireplace('{{' . $key . '}}', $value, $content);
                } 
                preg_match_all("@{{:([a-z0-9_]+?)}}@sui", $content, $localization);
                $localization = $localization[1];
                foreach ($localization as $value) {
                    $content = str_ireplace('{{:' . $value . '}}', $lang[$value], $content);
                } //applying lang
    
                foreach ($quests as $key => $value) {
                    preg_match_all("@{\?$key=$value\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $matches);
                    while (!empty($matches[0])) {
                        $content = str_replace($matches[0][0], $matches[1][0], $content);
                        preg_match_all("@{\?$key=$value\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $matches);
                    }
                    preg_match_all("@{\?$key=((?!$value).+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $matches);
                    while (!empty($matches[0])) {
                        $content = str_replace($matches[0][0], "", $content);
                        preg_match_all("@{\?$key=((?!$value).+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $matches);
                    }
                }
    
                preg_match_all("@{\?access=([a-z0-9]+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $perms);
                while (!empty($perms[0])) {
                    foreach ($perms[1] as $value) {
                        if ($this->user->canAccess($value))
                            $content = preg_replace("@{\?access=$value\?}(((?!{\?.+\?}).)*?){\?\?}@sui", "$1", $content);
                        else $content = preg_replace("@{\?access=$value\?}(((?!{\?.+\?}).)*?){\?\?}@sui", "", $content);
                    }
                    preg_match_all("@{\?access=([a-z0-9]+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $perms);
                }
    
                $content = preg_replace("@{\?authorized=((?!" . (int) $this->authorized . ").+?)\?}(.+?){\?\?}@sui", "", $content);
                $content = preg_replace("@{\?authorized=" . (int) $this->authorized . "\?}(.+?){\?\?}@sui", "$1", $content);
                $content = preg_replace("@{\?(.+?)\?}(.+?){\?\?}@sui", "", $content);
    
                $content = str_ireplace('{{DIR}}', '/' . $this->dir, $content); //replacing DIR param
                $content = str_ireplace('{{URI}}', urlencode(other::filter($_SERVER['REQUEST_URI'])), $content); //replacing URI param
                $content = str_ireplace('{{HTTP_HOST}}', $_SERVER['HTTP_HOST'], $content); //replacing HTTP_HOST param
                $content = preg_replace("@{\?((.+?)|(.+?){0})\?}@sui", "", $content);
                if (!$return) echo $content;
                return $content;
            }
        }
    ?>

    Мой шаблонизатор. Детям и беременным женщинам не смотреть.

    Efog, 03 Ноября 2014

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