1. Си / Говнокод #12604

    +125

    1. 1
    2. 2
    3. 3
    4. 4
    void get_me(const char **retval)
    {
        *retval = "Hello, Word!";
    }

    Надейся на компилятор...

    Novi4oK, 16 Февраля 2013

    Комментарии (39)
  2. Куча / Говнокод #12603

    +141

    1. 1
    http://rosettacode.org/wiki/Category:Programming_Tasks

    Сегодня это реально сразу куча.

    LispGovno, 16 Февраля 2013

    Комментарии (1)
  3. Pascal / Говнокод #12602

    +93

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    s := '  .  ';
    IF fGamma >= 1000 THEN s[1] := CHR (ORD ('0') + fGamma DIV 1000);
    s[2] := CHR (ORD ('0') + fGamma DIV 100 MOD 10);
    s[4] := CHR (ORD ('0') + fGamma DIV 10 MOD 10);
    s[5] := CHR (ORD ('0') + fGamma MOD 10);

    Перевод числа в строку.
    Из исходников Photoshop 1.0.1
    http://bit.ly/W11p5e
    Там на первый взгляд хватает копипасты. Но особо не углублялся.

    absolut, 16 Февраля 2013

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

    +77

    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
    byte[] buffer = new byte[BUFFER_SIZE];
    ReadState readState = ReadState.BOUNDARY;
    
    InputStream input = request.getInputStream();
    int read = input.read(buffer);
    int pos = 0;
    
    // This is a fail-safe to prevent infinite loops from occurring in some environments
    int loopCounter = 20;
    
    while (read > 0 && loopCounter > 0) {
        for (int i = 0; i < read; i++) {
            switch (readState) {
                // Pos is calculated...
                case BOUNDARY: 
                case HEADERS: 
                case DATA: 
            }
        }
    
        if (pos < read) {
            // move the bytes that weren't read to the start of the buffer
            int bytesNotRead = read - pos;
            System.arraycopy(buffer, pos, buffer, 0, bytesNotRead);
            read = input.read(buffer, bytesNotRead, buffer.length - bytesNotRead);
    
            // Decrement loopCounter if no data was readable
            if (read == 0) {
                loopCounter--;
            }
    
            read += bytesNotRead;
        } else {
            read = input.read(buffer);
        }
    }

    Кусок исходников из недров JBoss Seam(наткнулся профайлером).
    Пацаны пофиксили багу с бесконечным циклом и 100 утилизацией CPU.
    Весь класс:
    https://www.java2s.com/Open-Source/Java/JBoss/jboss-seam-2.2.0/org/jboss/seam/web/MultipartRequestImpl.java.htm
    Версия с бесконечным циклом:
    http://www.docjar.com/html/api/org/jboss/seam/web/MultipartRequestImpl.java.html

    psvm, 16 Февраля 2013

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

    +141

    1. 1
    http://www.youloveit.ru/uploads/gallery/main/439/youloveit_ru_pony-arts07.png

    dos_, 15 Февраля 2013

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

    +74

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    private String stateChangedReason;
    ...
    
    if (stateChangedReason != null && stateChangedReason instanceof String) {
                dealerManageInfo.setChangeStateReason(stateChangedReason.toString());
    }

    amarfey, 15 Февраля 2013

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

    +141

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    var declination= function(n, form1, form2, form5){
            n = n % 100;
            var n1 = n % 10;
            if (n > 10 && n < 20) return form5;
            if (n1 > 1 && n1 < 5) return form2;
            if (n1 == 1) return form1;
            return form5;
        }

    хуита, 15 Февраля 2013

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

    +121

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    public class DefaultQueryEngine implements QueryEngine {
    
        private static volatile QueryEngine DEFAULT;
        
        public static QueryEngine getDefault() {
            if (DEFAULT == null) {
                DEFAULT = new DefaultQueryEngine(new DefaultEvaluatorFactory(CollQueryTemplates.DEFAULT));
            }
            return DEFAULT;
        }

    https://github.com/mysema/querydsl/blob/master/querydsl-collections/src/main/java/com/mysema/query/collections/DefaultQueryEngine.java

    Ехал дефолт через дефолт...

    someone, 15 Февраля 2013

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

    +151

    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
    <?php
    class MainController extends ModuleAdminController {
    	public $defaultAction = 'admin';
    	
    	public function actionCreate() {
    		$this->redirect('admin');
    	}
    	
    	public function actionDelete($id) {
    		$this->redirect('admin');
    	}
    	
    	public function actionView($id) {
    		$this->redirect('admin');
    	}
    }

    Модули в Yii такие коварные :(

    Diwms, 15 Февраля 2013

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

    +133

    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
    private function init_categories()
    	{
    		// Дерево категорий
    		$tree = new stdClass();
    		$tree->subcategories = array();
    		
    		// Указатели на узлы дерева
    		$pointers = array();
    		$pointers[0] = &$tree;
    		$pointers[0]->path = array();
    		
    		// Выбираем все категории
    		$query = $this->db->placehold("SELECT c.id, c.parent_id, c.name, c.description, c.url, c.meta_title, c.meta_keywords, c.meta_description, c.image, c.visible, c.position
    										FROM __categories c ORDER BY c.parent_id, c.position");
    											
    		// Выбор категорий с подсчетом количества товаров для каждой. Может тормозить при большом количестве товаров.
    		// $query = $this->db->placehold("SELECT c.id, c.parent_id, c.name, c.description, c.url, c.meta_title, c.meta_keywords, c.meta_description, c.image, c.visible, c.position, COUNT(p.id) as products_count
    		//                               FROM __categories c LEFT JOIN __products_categories pc ON pc.category_id=c.id LEFT JOIN __products p ON p.id=pc.product_id AND p.visible GROUP BY c.id ORDER BY c.parent_id, c.position");
    		
    		
    		$this->db->query($query);
    		$categories = $this->db->results();
    				
    		$finish = false;
    		// Не кончаем, пока не кончатся категории, или пока ниодну из оставшихся некуда приткнуть
    		while(!empty($categories)  && !$finish)
    		{
    			$flag = false;
    			// Проходим все выбранные категории
    			foreach($categories as $k=>$category)
    			{
    				if(isset($pointers[$category->parent_id]))
    				{
    					// В дерево категорий (через указатель) добавляем текущую категорию
    					$pointers[$category->id] = $pointers[$category->parent_id]->subcategories[] = $category;
    					
    					// Путь к текущей категории
    					$curr = $pointers[$category->id];
    					$pointers[$category->id]->path = array_merge((array)$pointers[$category->parent_id]->path, array($curr));
    					
    					// Убираем использованную категорию из массива категорий
    					unset($categories[$k]);
    					$flag = true;
    				}
    			}
    			if(!$flag) $finish = true;
    		}
    		
    		// Для каждой категории id всех ее деток узнаем
    		$ids = array_reverse(array_keys($pointers));
    		foreach($ids as $id)
    		{
    			if($id>0)
    			{
    				$pointers[$id]->children[] = $id;
    
    				if(isset($pointers[$pointers[$id]->parent_id]->children))
    					$pointers[$pointers[$id]->parent_id]->children = array_merge($pointers[$id]->children, $pointers[$pointers[$id]->parent_id]->children);
    				else
    					$pointers[$pointers[$id]->parent_id]->children = $pointers[$id]->children;
    					
    				// Добавляем количество товаров к родительской категории, если текущая видима
    				// if(isset($pointers[$pointers[$id]->parent_id]) && $pointers[$id]->visible)
    				//		$pointers[$pointers[$id]->parent_id]->products_count += $pointers[$id]->products_count;
    			}
    		}
    		unset($pointers[0]);
    		unset($ids);
    
    		$this->categories_tree = $tree->subcategories;
    		$this->all_categories = $pointers;	
    	}

    построение дерева категорий в платной cms simpla

    alpex, 15 Февраля 2013

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