1. JavaScript / Говнокод #14076

    +154

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    function FindProxyForURL(url, host) {
      if (shExpMatch(host, "*.govnokod.ru")) {
          return "PROXY 178.63.104.146:80";
      }
      
      return "DIRECT";
    }

    Навеяно прочтением статьи http://en.wikipedia.org/wiki/Proxy_auto-config

    inkanus-gray, 30 Октября 2013

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

    +10

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    int ScriptArrow;
    ...
    // проверим а есть ли в натуре скрипт по этому указателю
    try
    {
        RunScript = (Script*)ScriptArrow;
        RunScript->Script.ArrVar.count();
    }
    catch (...)
    {
        RunScript = NULL;
    }

    Проверка, в натуре. Авторский комментарий сохранен :)

    bormand, 30 Октября 2013

    Комментарии (42)
  3. Python / Говнокод #14074

    −98

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    rows = db.query_all("select id, command from всякие-заджойненные-таблички")
    foreach row in rows:
        if row[1] == command:
            return row[0]
    return None

    Высрал сегодня на работе. Пока пых не трогал - такого говна не писал ;)

    bormand, 28 Октября 2013

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

    +22

    1. 1
    printf("Куда все подевались? Ни одного коммента за два дня...\n");

    bormand, 24 Октября 2013

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

    +161

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    /*
         * Calculates date for $count_days before today
         * 
         * @param int $count_days - how much days before today
         * @return date - date for $count_days before today
         */
        function DaysBeforeToday($count_days) {
            $date = date('Y-m-d');
            $pos = strrpos($date, '-');
            $date = substr($date, 0, $pos + 1) . (substr($date, $pos + 1, 2) - $count_days);
            return $date;
        }

    ня! ^^

    ragnar, 22 Октября 2013

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

    +72

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    String isFeatureEnabled = object.get("featureEnabledParam");
    boolean featureEnabled = app.isFeatureEnabled();
    if (Boolean.parseBoolean(isFeatureEnabled) && !featureEnabled) {
        return false;
    }
    if (!Boolean.parseBoolean(isFeatureEnabled) && featureEnabled)
    {
        return false;
    }

    myzone, 22 Октября 2013

    Комментарии (14)
  7. JavaScript / Говнокод #14047

    +168

    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
    // ==UserScript==
    // @name        no horses
    // @match       *://govnokod.ru/*
    // @grant       none
    // @run-at      document-start
    // ==/UserScript==
    var CONFIG = {
        horses: [ 
            "Horse2", 
            "PragramistOtBoga", 
            "anonimb84a2f6fd141",
        ],
        autoDownVote: true,
    };
    var observer = new MutationObserver(observeCallback);
    var config = {
        childList: true,
        subtree: true,
    };
    observer.observe(document, config);
    function observeCallback(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes) {
                Array.prototype.forEach.call(mutation.addedNodes, function(node) {
                    try {
                        if (node.nodeType === 1 && /^comments_\d+$/.test(node.id)) {
                            handleComments(node);
                        }
                    } catch (e) {
                        console && console.warn && console.warn(e);
                    }
                });
            }
        });
    }
    function downVote(node, type) {
        var sel;
        switch (type) {
        case "post": sel = ".vote-against"; break;
        case "comment": sel = ".comment-vote-against"; break;
        default: throw 42; break;
        }
        var el = node.querySelector(sel);
        if (el) {
            var evt = document.createEvent("MouseEvents");
            evt.initMouseEvent("click", true, true, unsafeWindow, 
                0, 0, 0, 0, 0, false, false, false, false, 0, null); 
            el.dispatchEvent(evt);
        }
    }
    function handleComments(node) {
        var comments = node.querySelectorAll(".entry-comment-wrapper");
        Array.prototype.forEach.call(comments, function(comment) {
            try {
                handleComment(comment);
            } catch (e) {
                console && console.warn && console.warn(e);
            }
        });
    }
    function handleComment(node) {
        var author = node.querySelector(".entry-author").textContent.trim();
        if (CONFIG.horses.indexOf(author) != -1) {
            node.style.opacity = 0.3;
            node.style.maxHeight = "4em";
            node.style.overflow = "scroll";
    
            if (CONFIG.autoDownVote) {
                downVote(node, "comment");
            }
        }
    }
    function handlePosts(node) {
        var posts = node.querySelectorAll(".hentry");
        var i;
        for (i = 0; i < posts.length; i++) {
            try {
                handlePost(posts[i]);
            } catch (e) {
                console && console.warn && console.warn(e);
            }
        }
    }
    function handlePost(node) {
        var author = node.querySelector(".author a:nth-child(2)").textContent.trim();
        if (CONFIG.horses.indexOf(author) != -1) {
            if (!/^\/\d+$/.test(document.location.pathname)) {
                node.style.opacity = 0.3;
                node.style.maxHeight = "4em";
                node.style.overflow = "scroll";
            }
            if (CONFIG.autoDownVote) {
                downVote(node, "post");
            }
        }
    }
    document.addEventListener("DOMContentLoaded", function() {
        handleComments(document.body);
        handlePosts(document.body);
    });

    Я так и не смог заставить MutationObserver срабатывать на новые элементы, появляющиеся во время загрузки страницы. Отсюда и костыль в последних строчках.

    WGH, 22 Октября 2013

    Комментарии (121)
  8. JavaScript / Говнокод #14045

    +155

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    function printNumbersTimeout20_100() {
      var i = 1;
      var timerId = setTimeout(function go() {
        console.log(i);
        if (i < 20) setTimeout(go, 100);
        i++;
      }, 100);
    }
    
    // вызов
    printNumbersTimeout20_100();

    Вывод чисел каждые 100мс, через setTimeout

    Сделайте то же самое, что в задаче "Вывод чисел каждые 100мс", но с использованием setTimeout вместо setInterval.
    http://learn.javascript.ru/task/vyvod-chisel-kazhdye-100ms-cherez-settimeout

    Tairesh, 22 Октября 2013

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

    +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
    function date_to_text($date){
    	
    
    	
    	$now=time();
    	if ($s=strtotime($date)) //the function returns false is $date is not a string (as already in numeric unix date format)
    		$date = $s;
    	$d=round(($now-$date)); //s
    
    	//echo $d;
    	if($d>60){
    		$d=$d/60;//m	
    		if($d>60){
    			$d=$d/60;//h
    			if($d>24){
    				$d=$d/24;//d
    				if($d>7){
    					$d=$d/7;//w						
    					if($d>(52/12)){
    						$d=$d/52*12;//m
    						if($d>12){
    							$d=$d/12;//y
    							$d=floor($d);			
    							$str=$d.' year'.(($d>1)?'s':'');								
    						}else{
    							$d=floor($d);			
    							$str=$d.' month'.(($d>1)?'s':'');		
    						}
    					}else{
    						$d=floor($d);			
    						$str=$d.' wk'.($d>1?'s':'');									
    					}
    				}else{					
    					$d=floor($d);
    					$str=$d.' day'.($d>1?'s':'');											
    				}
    			}else{			
    				$d=floor($d);			
    				$str=$d.' hr'.(($d>1)?'s':'');		
    			}
    		}else{
    			$d=floor($d);			
    			$str=$d.' min'.(($d>1)?'s':'');			
    		}
    	}elseif ($d>0) {
    		$d=floor($d);			
    		$str=$d.' sec'.(($d>1)?'s':'');		
    	}
    	if ($d>0){
    		$str.=' ago';
    	} else {
    		$str = '';
    	}
    	return $str;
    }

    неопiсуемо

    rombik, 22 Октября 2013

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

    +100

    1. 1
    ТРАЛИ ПАБЕДЕЛИ ВАМ НЕАТКУДАВА ЖДАТЬ ПОМАЩИ ЛАЛКИ ИБАНЫЕЙЕ АЗАЗААЗХАЗХЗАХВАХВЩАХВЩАВА

    PragramistOtBoga, 22 Октября 2013

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