1. C# / Говнокод #6950

    +127

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    private string CalculateDecadeOrYear(string year)
    		{
    			string decade = year.Remove(3) + "0";
    			string currentYear = DateTime.Now.Year.ToString();
    			string currentDecade = currentYear.Remove(3) + "0";
    			string prevDecade = ((Convert.ToInt32(currentYear.Remove(3)) - 1)).ToString() + "0";
    			if (!(decade == currentDecade) && !(decade == prevDecade))
    			{
    				return decade;
    			}
    			return year;
    		}

    stringly typed вычисление декады. нашел в исходниках индусов

    overberk, 14 Июня 2011

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

    −121

    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
    create or replace procedure rout_cancel (
      p_sql varchar2
    ) is
      v_curs sys_refcursor;
    begin
      if v_curs%isopen then
        close v_curs;
      end if;
      begin
        open v_curs for p_sql;
      exception
        when others then
          raise_application_error(-20000, 'Unable to open cursor');
      end;
      rout_cancel_details(v_curs);
      close v_curs;
    end;

    6-8: Кто ж тебя открыть-то успел?!

    dwinner, 14 Июня 2011

    Комментарии (3)
  3. SQL / Говнокод #6948

    −121

    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
    declare
        v_curs sys_refcursor;
        v_on number;
    begin
        open v_curs for 'select rout_num from rout_orders_promotion';
        fetch v_curs into v_on;
        loop
            exit when v_curs%notfound;
            if v_curs%rowcount = 1 then
                null;
            end if;
            fetch v_curs into v_on;
        end loop;
        close v_curs;
    end;

    Если запись одна, тогда уж точно нечего в цикле делать.

    dwinner, 14 Июня 2011

    Комментарии (4)
  4. bash / Говнокод #6947

    −127

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    am=`cat $file | wc -l`
    for ((i=1;i<=$am;i++))
    do
        read z
        imsi=`echo $z | sed -e "s/^[0-9]*[ ]*//" `
    done < $file

    berezhinskiy, 14 Июня 2011

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

    +85

    1. 1
    String methodName = (new Exception()).getStackTrace()[1].getMethodName();

    Вот как надо получать имя метода. Это вам не __func__ ...

    roman-kashitsyn, 14 Июня 2011

    Комментарии (47)
  6. Java / Говнокод #6945

    +83

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    try
    		{
    			nIndex = Integer.parseInt(sRowIndex.trim());
    		}
    		catch(NumberFormatException e)
    		{
    			//异常处理
    			nIndex = -2;
    		}
    		if(nIndex < 0) return;

    Китайцы продолжают отжигать.

    lucidfox, 14 Июня 2011

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

    +171

    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
    // Файл settings.cpp
    #define SETTINGS_READ
    #include "Settings.h" 
    
    #undef SETTINGS_READ
    #include "Settings.h" 
    #endif
    
    // ----------------------------------
    // Файл settings.h
    #ifdef SETTINGS_READ
            #define _XML_OPTION_VALUE(name,value) if (XML.FindElem(_CRT_WIDE(#name))) {/*XML.IntoElem();*/ XML.GetData(value);  /*ShowVar(value);ShowVar(_CRT_WIDE(#name));*/  /* XML.OutOfElem();*/}
            #define _XML_OPTION_ATTR(name,value) XML.GetAttrib(_CRT_WIDE(#name),value) 
            #define XML_NODE_START(name,value) if (XML.FindElem(_CRT_WIDE(#name))) {XML.IntoElem();
            #define XML_NODE_START_WITHATTRIB(name,value) if (XML.FindElem(_CRT_WIDE(#name))) {
            #define XML_NODE_STARTS(name,value) if (XML.FindElem(name)) {
            #define XML_NODE_START_VALUE(name,value) if (XML.FindElem(_CRT_WIDE(#name))) { XML.GetData(value);XML.IntoElem();
            #define XML_NODE_START_VALUE_WITHATTRIB(name,value) if (XML.FindElem(_CRT_WIDE(#name))) { XML.GetData(value);     
            #define XML_NODE_END()  \
                    XML.OutOfElem(); \
            }
            #define XML_OPTION_VALUE(name) _XML_OPTION_VALUE(name,name)
            #define XML_OPTION_MEMBER_VALUE(prefix, name) _XML_OPTION_VALUE(name, prefix##.name)
            #define XML_OPTION_ATTR(name) _XML_OPTION_ATTR(name,name)
            #define XML_OPTION_MEMBER_ATTR(prefix, name) _XML_OPTION_ATTR(name, prefix##.name)
            #define COLOR_TO_PINT(c) (*(int*)&##c)
    #else
            // SAVING MACROSES
            #define _XML_OPTION_VALUE(name,value) XML.AddElem(_CRT_WIDE(#name),value) ; /*XML.OutOfElem();*/
            #define _XML_OPTION_ATTR(name,value) XML.SetAttrib(_CRT_WIDE(#name),value) 
            #define XML_NODE_START(name,value) XML.AddElem(_CRT_WIDE(#name)); {XML.IntoElem();
            // ( еще десяток макросов)
    #endif
    
    #ifdef SETTINGS_READ
    bool CSettings::MacroLoadSettings(CMyXml &XML)
    {
    #else
    bool CSettings::MacroSaveSettings(CMyXml &XML)
    {
    #endif
            TCHAR szFont[1024];
    	CString Font;
            XML_NODE_START(Settings);   
            XML_NODE_START(General);
                    XML_OPTION_MEMBER_VALUE(Settings,Language); 
                    XML_OPTION_VALUE(ExplorerContextMenu);
    
                    #ifndef  SETTINGS_READ
                             HotkeysStr= Settings.Hotkeys.toString();
                             FontToString(&LogoSettings.Font,Font);                        
                    #endif
                            
                    XML_OPTION_VALUE(HotkeysStr);
                            #ifdef  SETTINGS_READ
                                    Settings.Hotkeys.DeSerialize(HotkeysStr);
                                    StringToFont(Font, &LogoSettings.Font);
                            #endif
                    #endif
            XML_NODE_END();
           // ..............................................
            XML_NODE_END(); // end of settings
            XML_NODE_END(); // 
            return true;
    }

    Фееричный пример повторного использования кода с помощью директив препроцессора.

    Создано с целью совместить в одном месте чтение/запись настроек программы (с риском что-нибудь забыть) из XML файла.

    Kernel32, 13 Июня 2011

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

    +74

    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
    public void removeFromBase()
                throws SQLException{
            for(ServiceInstance serv: getServiceInstances())
                serv.removeFromBase();
            for(Pay pay: pays)
                pay.removeFromBase();
            String query;
            PreparedStatement ps = null;
            Connection connection = null;
            try{
                connection = TIDatasource.getInstance().getConnection();
                query = "DELETE FROM orders WHERE id=?";
                ps = connection.prepareStatement(query);
                ps.setLong(1, getId());
                int res = ps.executeUpdate();
                if(res != 1)
                    throw new SQLException("Unexpected number of orders have deleted: " + res);
            }catch(SQLException sqlex){
                throw new SQLException("Can't remove order from base because of error: " + sqlex);
            }finally{
                ps.close();
                connection.close();
            }
        }

    PRIMARY KEY(`id`)

    volatile, 13 Июня 2011

    Комментарии (15)
  9. JavaScript / Говнокод #6942

    +164

    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
    <script type="text/javascript">
    /* vedomosti mercedes fullscreen second pages */
    var ref=document.referrer;
    var cookie=Get_Cookie('ved_fullscreen2_1');
    if (!cookie && ref!='' && ref!='none' && ref!='blocked' && 'http://'==ref.substring(0,7)) {
    	ref=ref.substring(7);
    	var pos=ref.indexOf('/');
    	var invalid='.vedomosti.ru';
    	if (pos>0) ref=ref.substring(0,pos);
    
    	if (ref.substring( ref.length-invalid.length ) != invalid) {
    		Set_Cookie('ved_cookie_test',1,1);
    		if (Get_Cookie('ved_cookie_test')) {
    			Set_Cookie('ved_fullscreen2_1', 1, 1 * 24 * 3600, '/');
    			document.location.href = 'http://www.vedomosti.ru/var/bansrc/2011/9a6776.html#!'+(document.location.pathname);
    		}
    	}
    }
    </script>

    журнализды из одной пафосной бульварной газетки копят на мерседес

    bugmenot, 13 Июня 2011

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

    +148

    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
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    <?php
    
    class CodeCounter {
        const MULTILINE_COMMENT = 0x01;
    
        private $dir = null;
        private $ext = null;
    
        public function __construct($dir = '.', $ext = '*') {
            $this->dir = $dir;
            if($ext == '*') {
                $this->ext = "/.*/si";
            } else {
                $e = explode('|', $ext);
                foreach($e as &$ext) {
                    $ext = trim($ext);
                    if($ext == '')
                        unset($ext);
                }
                $r = implode('|', $e);
                $this->ext = "/.*\.({$r})$/si";
            }
    
        }
    
        public function calculate() {
            $lines = 0;
            $args = func_get_args();
            if(count($args) == 0)
                $dir = $this->dir;
            else
                $dir = $args[0];
            if(file_exists($dir) && is_dir($dir)) {
                $list = scandir($dir);
                foreach($list as $item) {
                    if($item == '.' || $item == '..')
                        continue;
                    $fullItem = realpath($dir . DIRECTORY_SEPARATOR . $item);
                    if(is_dir($fullItem)) {
                        $lines += $this->calculate($fullItem);
                    } else {
                        if(preg_match($this->ext, $item)) {
                            echo "Calculating lines in {$fullItem}:  ";
                            $_lines = self::count($fullItem);
                            echo "{$_lines}\n";
                            $lines += $_lines;
                        }
                    }
                }
            }
            return $lines;
        }
    
        private static function count($file) {
            $lines = 0;
            $d = null;
            if(file_exists($file) && ($file = file($file))) {
                foreach($file as $line) {
                    $line = trim($line);
                    if($line == '')
                        continue;
                    if( substr($line, 0, 2) == '//' || //single line comment
                        substr($line, 0, 1) == '#'  || //single line comment
                        substr($line, 0, 2) == '<?' || //php open tag
                        substr($line, 0, 2) == '?>'    //php close tags
                    )
                        continue;
                    if(($pos = strpos('/*', $line)) !== false) {
                        if($pos == 0) {
                            if(strpos('*/', $line, $pos) === false) {
                                $d = self::MULTILINE_COMMENT;
                            }
                        } else {
                            $lines++;
                        }
                        continue;
                    }
                    if($d == self::MULTILINE_COMMENT) {
                        if(strpos('*/', $line) !== false) {
                            $d = null;
                        }
                        continue;
                    }
                    $lines++;
                }
            }
            return $lines;
        }
    }
    
    $counter = new CodeCounter('./amapys', 'php|js');
    $lines = $counter->calculate();
    echo "\nTotal: {$lines} lines\n";

    Автор: POPSuL
    Пхп-шники такие пхп-шники.
    ООП во все поля. Им неведом sed и awk.

    cutwater, 13 Июня 2011

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