1. Лучший говнокод

    В номинации:
    За время:
  2. Java / Говнокод #16045

    +68

    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
    class CircuitBreaker
    {
        boolean broken = false;
        CircuitBreaker() {}
        private void breakCircuit()
        {
            this.broken = true;
        }
        private boolean isBroken()
        {
            return this.broken;
        }
    }
    
    private boolean writeAssetsToDisk()
    {
        CircuitBreaker breaker = new CircuitBreaker();
        writeBase64EncodedAssetToDisk(breaker, "...", getPath(...));
        writeBase64EncodedAssetToDisk(breaker, "...", getPath(...));
        writeBase64EncodedAssetToDisk(breaker, "...", getPath(...));
        writeBase64EncodedAssetToDisk(breaker, "...", getPath(...));
        return !breaker.isBroken();
    }
    
    private void writeBase64EncodedAssetToDisk(CircuitBreaker breaker, String base64String, String filename)
    {
        if (breaker.isBroken()) {
            return;
        }
        ...
        try
        {
            ...
        }
        catch (IOException e)
        {
            breaker.breakCircuit(); return;
        }
        ...
    }

    Используй исключения, Люк. Фрагмент из Amazon Mobile Ads SDK.

    chaoswithin, 23 Мая 2014

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

    +68

    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
    #include <iostream>
    
    using namespace std;
    
    unsigned ololo_div(unsigned what, unsigned by) {
        unsigned cnt=0;
        while( what>by ) {
            what-=by;
            cnt++;
        }
        if( what==0 ) cnt++;
        return cnt;
    }
    
    int main() {
        cout << ololo_div(10,0) << endl;
        return 0;
    }

    "че там делить то)))"

    http://codepad.org/nL06uX2s

    Elvenfighter, 23 Апреля 2014

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

    +68

    1. 1
    2. 2
    3. 3
    boolean isCyrillic(char c) {
        return Character.UnicodeBlock.CYRILLIC.equals(Character.UnicodeBlock.of(c));
    }

    Краткость - сестра таланта

    evg_ever, 21 Марта 2014

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

    +68

    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
    private Date value;
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
    private SimpleDateFormat sdfshort = new SimpleDateFormat("dd.MM.yyyy");
    
    void setValue(String value) {
    	try {
    		if (value.length() >= 18)
    			this.value = sdf.parse(value);
    		else
    			this.value = sdfshort.parse(value);
    	} catch (ParseException e) {
    		this.value = sdfshort.parse(value);
    	}
    }

    evg_ever, 17 Марта 2014

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

    +68

    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
    public boolean fastItemEquals(ItemStack st, ItemStack nd) {
            if(st.hashCode() == nd.hashCode()) return true;
            if(st.getType() != nd.getType()) return false;
            if(!st.getItemMeta().getDisplayName().equals(nd.getItemMeta().getDisplayName())) return false;
            if(st.getEnchantments().size() != nd.getEnchantments().size()) return false;
            if(st.getItemMeta().getLore().size() != nd.getItemMeta().getLore().size()) return false;
            List<String> 
                    lst = st.getItemMeta().getLore(),
                    lnd = nd.getItemMeta().getLore();
            for(int i = 0 ; i < st.getItemMeta().getLore().size() ; i++)
                if(!lst.get(i).equals(lst.get(i))) return false;
            
            //return st.isSimilar(nd);
            return true;
        }
        
        /* оригинал
        @Override
        public boolean isSimilar(ItemStack stack) {
            if (stack == null) {
                return false;
            }
            if (stack == this) {
                return true;
            }
            if (!(stack instanceof CraftItemStack)) {
                return stack.getClass() == ItemStack.class && stack.isSimilar(this);
            }
    
            CraftItemStack that = (CraftItemStack) stack;
            if (handle == that.handle) {
                return true;
            }
            if (handle == null || that.handle == null) {
                return false;
            }
            if (!(that.getTypeId() == getTypeId() && getDurability() == that.getDurability())) {
                return false;
            }
            return hasItemMeta() ? that.hasItemMeta() && handle.tag.equals(that.handle.tag) : !that.hasItemMeta();
        }
        */

    DiaLight, 15 Марта 2014

    Комментарии (0)
  7. Java / Говнокод #14510

    +68

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    public static String elvis(String value, String ifNull) {
            return value == null ? ifNull : value;
        }
    
        public static Boolean elvis(Boolean value, Boolean ifNull) {
            return value == null ? ifNull : value;
        }
    
        public static Object elvis(Object value, Object ifNull) {
            return value == null ? ifNull : value;
        }

    - Objects#firstNotNull()?
    - нет, не слышал

    myzone, 05 Февраля 2014

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

    +68

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    // setting simple fields that couldn't be null
            if (firstOperDay != null) {
                dto.setFirstClosedDay(firstOperDay);
            } else {
                dto.setFirstClosedDay(null);
            }
            if (lastOperDay != null) {
                dto.setLastClosedDay(lastOperDay);
            } else {
                dto.setLastClosedDay(null);
            }

    ingenuus, 15 Ноября 2013

    Комментарии (1)
  9. Java / Говнокод #14081

    +68

    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
    public static AstRoot parse (String code) {
            CompilerEnvirons env = new CompilerEnvirons();
            env.setRecoverFromErrors(true);
            env.setGenerateDebugInfo(true);
            env.setRecordingComments(true);
    
            // try to ignore errors - does not seem to work
            env.setIdeMode(true);
    
            IRFactory factory = new IRFactory(env);
            AstRoot root = factory.parse(code, null, 0);
    
            // work around rhino bug 800616 (not fixed in neither rhino nor closure)
    
            root.visit(new NodeVisitor() {
                @Override
                public boolean visit(AstNode node) {
                    if (node instanceof NumberLiteral) {
                        NumberLiteral num = (NumberLiteral)node;
                        int from = num.getAbsolutePosition();
                        int to = from + num.getLength() + 2;
                        if (to < code.length()) {
                            String hex = "0x" + num.toSource();
                            if (code.substring(from, to).equals(hex)) {
                                // reset node value and length
                                num.setValue(hex); num.setLength(hex.length());
                            }
                        }
                        return false;
                    }
                    return true;
                }
            });
    
            // work around rhino SwitchStatement.toSource() bug with empty switches
            // https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/ast/SwitchStatement.java#L96
            // https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/ast/SwitchStatement.java#L107-L109
            // https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/ast/SwitchStatement.java#L158
            root.visit(new NodeVisitor() {
                @Override
                public boolean visit(AstNode node) {
                    if (node instanceof SwitchStatement) {
                        SwitchStatement ss = (SwitchStatement)node;
                        if (ss.getCases().isEmpty()) {
                            // need to add at least one node to make ss.cases non-null
                            ArrayList<SwitchCase> cases = new ArrayList<>();
                            cases.add(new SwitchCase());
                            ss.setCases(cases);
                            // set this back to empty list
                            cases.clear();
                            ss.setCases(cases);
                        }
                        return false;
                    }
                    return true;
                }
            });
    
            return root;
        }

    И ещё немножко трудовыебуднев пользователей рино: правда клёво взять и распарсить джаваскрипт одним простым методом? Авотхуй.

    makc3d, 07 Ноября 2013

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

    +68

    1. 1
    2. 2
    3. 3
    if (!(taxOrgsFilter.getTaxOrgs() == null)) {
    ....
    }

    ingenuus, 19 Августа 2013

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

    +68

    1. 1
    2. 2
    3. 3
    StringBuffer serialNum = new StringBuffer ();
    for (int i = 0; i < 0; i++)
           serialNum = serialNum.append (Byte.toString (serial[ i ]));

    john812, 02 Августа 2013

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