在您的经验中发现的最糟糕的 Java 实践是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/238173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:33:57  来源:igfitidea点击:

Worst Java practice found in your experience?

java

提问by Zizzencs

Similar to this question...

类似于这个问题...

What are the worst practices you actually found in Java code?

您在 Java 代码中实际发现的最糟糕的做法是什么?

Mine are:

我的是:

  • using instance variables in servlets (it's not just bad practice but bug, actually)
  • using Collection implementations like HashMap, and not using the appropriate interfaces
  • using seemingly cryptic class names like SmsMaker (SmsFactory) or CommEnvironment (CommunicationContext)
  • 在 servlet 中使用实例变量(实际上,这不仅是不好的做法,而且是错误)
  • 使用像 HashMap 这样的 Collection 实现,而不是使用适当的接口
  • 使用看似神秘的类名,如 SmsMaker (SmsFactory) 或 CommEnvironment (CommunicationContext)

回答by asalamon74

I had to maintain java code, where most of the Exception handling was like:

我不得不维护 java 代码,其中大部分异常处理是这样的:

catch( Exception e ) {}

回答by Peter ?tibrany

Once I encountered 'singleton' exception:

一旦我遇到“单身”异常:

class Singletons {
    public static final MyException myException = new MyException();
}

class Test {
    public void doSomething() throws MyException {
        throw Singletons.myException;
    }
}

Same instance of exceptionwas thrown each time ... with exact same stacktrace, which had nothing to do with real code flow :-(

每次都抛出相同的异常实例......具有完全相同的堆栈跟踪,这与实际代码流无关:-(

回答by Peter Lawrey

Six really bad examples;

六个非常糟糕的例子;

  • Instead of error reporting, just System.exitwithout warning. e.g.
    if(properties.size()>10000) System.exit(0);buried deep in a library.
  • Using string constants as locks. e.g. synchronized("one") { }.
  • Locking on a mutable field. e.g. synchronized(object) { object = ...; }.
  • Initializing static fields in the constructor.
  • Triggering an exception just to get a stack trace. e.g. try { Integer i = null; i.intValue(); } catch(NullPointerException e) { e.printStackTrace(); }.
  • Pointless object creation e.g. new Integer(text).intValue() or worse new Integer(0).getClass()
  • 而不是错误报告,只是 System.exit没有警告。例如,
    if(properties.size()>10000) System.exit(0);深埋在图书馆里。
  • 使用字符串常量作为锁。例如synchronized("one") { }
  • 锁定可变字段。例如synchronized(object) { object = ...; }
  • 在构造函数中初始化静态字段。
  • 触发异常只是为了获取堆栈跟踪。例如try { Integer i = null; i.intValue(); } catch(NullPointerException e) { e.printStackTrace(); }
  • 无意义的对象创建,例如 new Integer(text).intValue() or worse new Integer(0).getClass()

回答by Steve B.

if{
 if{
  if{
   if{
    if{
     if{
      if{
       if{
         ....

回答by John Topley

I hate it when people create interfaces just for hanging a set of constants on:

我讨厌人们创建接口只是为了将一组常量挂在:

public interface InterfaceAntiPattern {
  boolean BAD_IDEA = true;
  int THIS_SUCKS = 1;
}

—Interfaces are for specifying behavioural contracts, not a convenience mechanism for including constants.

—接口用于指定行为契约,而不是包含常量的便利机制。

回答by Claudiu

Not related strictly to Java, but calling an expensive function over and over instead of storing the result, when you know it won't change. Example:

与 Java 没有严格关系,但是当您知道结果不会改变时,会一遍又一遍地调用昂贵的函数而不是存储结果。例子:

if (expensiveFunction() > aVar)
    aVar = expensiveFunction();
for (int i=0; i < expensiveFunction(); ++i)
    System.out.println(expensiveFunction());

回答by Apocalisp

The worst Java practice that encompasses almost all others: Global mutable state.

最糟糕的 Java 实践几乎涵盖了所有其他实践:全局可变状态。

回答by madlep

Ridiculous OO mania with class hierachies 10+ levels deep.

可笑的 OO 狂热,具有 10 多个级别的等级等级。

This is where names like DefaultConcreteMutableAbstractWhizzBangImplcome from. Just try debugging that kind of code - you'll be whizzing up and down the class tree for hours.

这就是名字的由来DefaultConcreteMutableAbstractWhizzBangImpl。只需尝试调试那种代码 - 您将在类树上上下颠簸几个小时。

回答by volley

Subclassing when you're not supposed to, e.g. instead of using composition, aggregation, etc.

当您不应该进行子类化时,例如而不是使用组合、聚合等。

Edit: This is a special case of the hammer.

编辑:这是锤子的特例。

回答by CodeClimber

saw something like this:

看到这样的事情:

public static boolean isNull(int value) {
    Integer integer = new Integer(value);

    if(integer == null) {
        return true;
    } else {
        return false;
    }
}

They had a similar method for longs.

他们对多头有类似的方法。

I presume they had originally done something like

我想他们最初做过类似的事情

if(value == null) {

and got a compile error and still didn't realise that primitive values couldn't be null.

并得到一个编译错误,但仍然没有意识到原始值不能为空。