1. Java / Говнокод #11682

    +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
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    try {
    	repository.saveAll(entities);
    } catch (final ConstraintViolationException e) {
    	throw new SettingsServiceException(ErrorFormatter.formatValidationErrors(
    			"Errors have been detected when saving", e));
    } catch (final PersistenceException e) {
    	for (val t: Throwables.getCausalChain(e)) {
    		if (t instanceof EntityExistsException) {
    			val failedObject = (IEntity) ((ExceptionInfo) t).getFailedObject();
    			
    			// failedObject is returned with the state in which it is in the database.
    			// Let's find the version that we tried to save, instead.
    			for (val entity: entities) {
    				if (entity.getId() != null && entity.getId().equals(failedObject.getId())) {
    					throw new SettingsServiceException("Attempted to save duplicate value: " + entity);
    				}
    			}
    		}
    	}
    	
    	throw e;
    }

    Попытка вернуть пользователю человеко-читаемое сообщение о том, где, собственно, дубликат.

    Костыли вы мои, костыли...

    someone, 31 Августа 2012

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

    +54

    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
    <?php
    
    class SimpleController extends Zend_Controller_Action
    {
        public function init()
        {
            $action = $this->_request->getActionName();
            $is_action_available = in_array($action, explode(',', 'terms,privacy,about,copyright,support,legal,features,help,refund'));
            $is_incorrect_action = CR_Settings::siteIsFree() && $action == 'refund';
            if (!$is_action_available || $is_incorrect_action)
                $this->_helper->redirector->setGotoRoute(array('action' => 'index'), 'default', true);
        }
        
        public function termsAction(){
        }
        
        public function privacyAction(){
        }
        
        public function aboutAction(){
        }
        
        public function copyrightAction(){
        }
        
        public function supportAction(){
        }
        
        public function legalAction(){
        }
        
        public function featuresAction(){
        }
        
        public function helpAction(){
        }
        
        public function refundAction(){
        }
    }

    Zend Framework проверка на существование action :)

    arkasha, 31 Августа 2012

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

    +26

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    result.push_back(TVector<2>
    				(
    					(A-√(D))/C,
    					(E-Line.K()*√(D))/C
    				));
    				result.push_back(TVector<2>
    				(
    					(A+√(D))/C,
    					(E+Line.K()*√(D))/C
    				));

    LispGovno, 31 Августа 2012

    Комментарии (55)
  4. Куча / Говнокод #11679

    +131

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    if (http_status_code == 200 || http_status_code == 500) {
      .... //success!!
    } else {
      .... // error!!
    }

    Было замечено в одном андроид приложении.

    mr.The, 30 Августа 2012

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

    +141

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    var number = new Random();
    if (number.Next(10)%2  == 0)
    {...}
    else
    {...}

    Нашёл такой шедевр в индокоде, которые мне дали модернизировать.

    badstarosta, 30 Августа 2012

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

    +57

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    function SetImage($row=array())
    	{
         foreach($row as $property=>$value)
         	{
         	eval("\$this->".$property." = '".$value."';");
         	}
    	}

    minramilka, 30 Августа 2012

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

    +107

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    if (_appliesTo.Contains("CH"))
    {
        depElig &= true;
    }
    else
    {
        depElig &= false;
    }

    Вроде такого boolshit тут ещё не было.

    roman-kashitsyn, 30 Августа 2012

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

    +153

    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
    var num_normalize = function (num) {
            if (typeof num == 'string') {
                if (num.test('.')) {
                    return parseFloat(num).toFixed(2);
                }
                else {
                    return parseInt(num);
                }
            }
            else if (parseInt(num) == parseFloat(num)) {
                return parseInt(num);
            }
            else return parseFloat(num).toFixed(2);
        };

    Привод стринги в numeric, проблема была в том что приходила значение в string, но могло быть с точками. естественно parseInt не годится и parseFloat тоже, ибо число не красивое визуально

    Stud, 30 Августа 2012

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

    +157

    1. 1
    2. 2
    3. 3
    4. 4
    var temp = new Date(),
        diff = parseInt(($('#calrlimit1').data('datebox').theDate - temp) / ( 1000 * 60 * 60 * 24 ));
        diffstrt = (diff * -1)-1; // If you want a minimum of 1 day between, make this -2 instead of -1
        diffend = diff + 11; // Why 11 instead of 10?  No idea...

    Документация к плагину jQuery Mobile DateBox2

    UnnamedUser, 30 Августа 2012

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

    +49

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    /**
      * Test for method importData()
      */
    public function testImportData()
    {
         $this->_model->expects($this->once())
             ->method('_importData');
         $this->_model->importData();
     }

    Ценное юнит-тестирование правильности работы метода

    InnerJoin, 30 Августа 2012

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