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

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

    +75

    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
    private enum UpdateToken {
        W, A, F, D, H;
    
        private static final Pattern PATTERN = compilePattern(UpdateToken.class);
    }
    
    private static Pattern compilePattern(Class<? extends Enum> clazz) {
        StringBuilder builder = new StringBuilder("(");
        for (Enum enumConstant : clazz.getEnumConstants()) {
            if (enumConstant.ordinal() > 0) {
                builder.append("|");
            }
            builder.append(enumConstant.name());
        }
        builder.append(")");
        return Pattern.compile(builder.toString());
    }
    
    //и это добро вот так используется:
    
        if (!UpdateToken.PATTERN.matcher(token).matches()) {
            continue;
        }
        UpdateToken setupToken = UpdateToken.valueOf(token);

    https://github.com/aichallenge/aichallenge/blob/epsilon/ants/dist/starter_bots/java/AbstractSystemInputParser.java

    rat4, 01 Ноября 2011

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

    +75

    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
    int i128 = 0, i256 =0, i384=0, i512=0, i640=0, i786=0, i896=0, i1024=0, i1152=0, i1280=0, i1408=0, i1536=0;
            for (int index: data) {
                if (index>max) max = index;
                if (min>index) min = index;
                if (0<=index & index<128) i128++;
                else if (128<=index & index<256) i256++;
                else if (256<=index & index<384) i384++;
                else if (384<=index & index<512) i512++;
                else if (512<=index & index<640) i640++;
                else if (640<=index & index<786) i786++;
                else if (786<=index & index<896) i896++;
                else if (896<=index & index<1024) i1024++;
                else if (1024<=index & index<1152) i1152++;
                else if (1152<=index & index<1280) i1280++;
                else if (1280<=index & index<1408) i1408++;
                else if (1408<=index & index<1536) i1536++;

    Считаем количество попаданий index в различные диапазоны значений.

    betastat, 01 Ноября 2011

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

    +75

    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
    /**
     * Workaround ObjectInputStream for maintaining backward compatibility with serialization.
     * 
     * In the future, please, please, PLEASE assign each serializable class an explicit serialVersionUID.
     * 
     */
    public final class DecompressibleInputStream extends ObjectInputStream {
    	private static final Logger logger = Logger.getLogger(DecompressibleInputStream.class);
    
        public DecompressibleInputStream(InputStream in) throws IOException {
            super(in);
        }
    
        protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
            ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
            Class<?> localClass;
            
            try {
                localClass = Class.forName(resultClassDescriptor.getName()); 
            } catch (ClassNotFoundException e) {
                logger.error("No local class for " + resultClassDescriptor.getName(), e);
                return resultClassDescriptor;
            }
            
            ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
            
            if (localClassDescriptor != null) { // only if class implements serializable
                final long localSUID = localClassDescriptor.getSerialVersionUID();
                final long streamSUID = resultClassDescriptor.getSerialVersionUID();
                
                if (streamSUID != localSUID &&
                		(localClass == ByteArraySerial.class || localClass == Vector2D.class)) {
                	// Workaround: check for serialVersionUID mismatch with two specific classes
                    logger.error(String.format("Overriding serialized class version mismatch for %s: " +
                    		"local serialVersionUID = %s, stream serialVersionUID = %s",
                    		localClass.getName(), localSUID, streamSUID));
                    resultClassDescriptor = localClassDescriptor; // Use local class descriptor for deserialization
                }
            }
            
            return resultClassDescriptor;
        }
    }

    Продукт использует в качестве бинарного формата сохранённых файлов встроенную сериализацию. При этом ранние версии полагались на встроенный serialVersionUID.

    Вот теперь приходится расхлёбывать. Наши воркэраунды - самые воркэраундные воркэраунды в мире.

    lucidfox, 24 Октября 2011

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

    +75

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    /**
     * NOTE: This source is automatically generated please do not modify this file.  
     * Either subclass or remove the record in src/generated
     */
    public int getSize()
    {
            return 4 +  + 1 + 1 + 2 + 1 + 1 + 1 + 1 + 2 + 2 + 2 + 2 + 2 + 2 + 4 + 4 + 4 + 2 + 4 + 4 + 4 + 2 + 4 + 2 + 2 + 4 + 4 + 4 + 2 + 4 + 4 + 4 + 2 + 4 + 2 + 310 + 10 + 2 + 2 + 12 + 4 + 4 + 4 + 4 + 30 + 4 + 4 + 4 + 4 + 4 + 2 + 2 + 2 + 2;
    }

    xaoc, 05 Октября 2011

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

    +75

    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
    public void CheckText(EditText et1, EditText et2)
        {
        	String s;
        	ArrayList arr = new ArrayList();
        	s = "" + et1.getText();
        	for(int i=0; i<s.length();i++)
        	{
        		arr.add(s.charAt(i));
        	}
        	s = "";
        	for(int i=0;i<arr.size()-1;i++)
        	{
        		s += arr.get(i);
        	}
        	
        	et2.setText(s);
        	et2.setSelection(et2.length());
        }

    Android

    jnixable, 24 Сентября 2011

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

    +75

    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
    Entity entity1 = null;
            Entity entity2 = null;
            //..............        
            Entity entityN = null;
            
            try{
                entity1 = provider.getEntity("key1").getValue();
                entity2 = provider.getEntity("key2").getValue();
                //..................
                entityN = provider.getEntity("key3").getValue();
            }catch (NullPointerException e){
                log.error("error", e);
            }
            
            //later
            smth.call(entity1.getSMTH())

    Главное - сообщить в лог!
    Почти такое же г-но ушло на продакшн систему по причине "не успел поревьюить".

    klissan, 22 Сентября 2011

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

    +75

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    try {
        Method m = this.getClass().getMethod("setLayerType", int.class, Paint.class);
        if (m != null) {
            m.invoke(this, View.LAYER_TYPE_SOFTWARE, (Object)null);
        }
    } catch (NoSuchMethodException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
    }

    Моему коллеге пришлось писать _такое_ только потому, что заказчик не захотел форкнуть проект на две отдельных ветки, для Android 2.3 и для 3.2.

    wildscliss, 07 Сентября 2011

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

    +75

    1. 1
    String s = (new StringBuilder()).append("c.").append(Integer.toString(i, 36)).append(".").append(Integer.toString(j, 36)).append(".dat").toString();

    Опять из сорцов минесрафта.
    У нотча видимо StringBuilder головного мозга.

    RaZeR, 19 Августа 2011

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

    +75

    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
    public static boolean checkInt(String value, SocketMessage message,
    			int min, int max, boolean isNullCheck, Settings settings) {
    		if (value == null) {
    			if (!isNullCheck)
    				return true;
    			if (message != null && settings != null)
    				message.addSubMessage(ErrorSubElement.createErrorSubMessage(
    						ErrorSubElement.INVALID_ARGUMENT, "Invalid Int value",
    						settings));
    			return false;
    		}
    
    		if (value.length() > 9) {
    			if (message != null && settings != null)
    				message.addSubMessage(ErrorSubElement.createErrorSubMessage(
    						ErrorSubElement.INVALID_ARGUMENT, "Invalid Int value: "
    								+ value, settings));
    			return false;
    		}
    		try {
    			int val = Integer.parseInt(value);
    			MyLog.d(LOG_TAG, "Check Val:" + val);
    			if (val < min) {
    				if (message != null && settings != null)
    					message.addSubMessage(ErrorSubElement
    							.createErrorSubMessage(
    									ErrorSubElement.INVALID_ARGUMENT,
    									"Invalid Int value: " + value
    											+ " lower than " + min, settings));
    				return false;
    			}
    
    			if (val > max) {
    				if (message != null && settings != null)
    					message.addSubMessage(ErrorSubElement
    							.createErrorSubMessage(
    									ErrorSubElement.INVALID_ARGUMENT,
    									"Invalid Int value: " + value
    											+ " bigger than " + max, settings));
    				return false;
    			}
    			return true;
    		} catch (NumberFormatException e) {
    			if (message != null && settings != null)
    				message.addSubMessage(ErrorSubElement.createErrorSubMessage(
    						ErrorSubElement.INVALID_ARGUMENT, "Invalid Int value: "
    								+ value, settings));
    			return false;
    		}
    	}

    xitx, 10 Августа 2011

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

    +75

    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
    public static String ellipsizeText(String text, Context cnt) {
    		
    		int COUNT_OF_CHARACTERS_LDPI = 10;
    		int COUNT_OF_CHARACTERS_MDPI = 20;
    		int COUNT_OF_CHARACTERS_HDPI = 30;
    		
    		String ellipsizeT = "...";
    		
    		String newText = text;
    		
    		switch (cnt.getResources().getDisplayMetrics().densityDpi) {
    		case DisplayMetrics.DENSITY_LOW:
    		    if (text.length() > COUNT_OF_CHARACTERS_LDPI) {
    				newText = text.substring(0, COUNT_OF_CHARACTERS_LDPI) + ellipsizeT;
    			}
    		    break;
    		case DisplayMetrics.DENSITY_MEDIUM:
    		    if (text.length() > COUNT_OF_CHARACTERS_MDPI) {
    				newText = text.substring(0, COUNT_OF_CHARACTERS_MDPI) + ellipsizeT;
    			}
    		    break;
    		case DisplayMetrics.DENSITY_HIGH:
    		    if (text.length() > COUNT_OF_CHARACTERS_HDPI) {
    				newText = text.substring(0, COUNT_OF_CHARACTERS_HDPI) + ellipsizeT;
    			}
    		    break;
    		}
    		
    		return newText;		
    	}

    Android

    Таким нехитрым способом заменяется реализация стандартной процедуры TextView.setEllipsize(TextUtils.Truncate At.END);

    Saasha, 15 Июля 2011

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