默认构造函数是善还是恶? Checkstyle和PMD在这里是相反的

发布于 2024-09-28 18:31:26 字数 1431 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

挽你眉间 2024-10-05 18:31:26

我喜欢 PMD 的回答。代码越少越好。不要编写编译器将为您编写的构造函数。

我的印象是,编写构造函数的主要论点是,一些不了解构造函数在 Java 中如何工作的可怜的程序员可能会绊倒您的代码并感到困惑。我不喜欢编写不必要的晦涩代码,但我也不喜欢编写简单的代码。

但这是我的强迫症,而且可能不合理。世界上有很多应用程序程序员,他们的主要关注点是业务,而不是语言,而且他们也不是语言专家。很多人采用的生存技巧就是风格一致,是否有必要并不是重点。

I like PMD's answer. The less code the better. Don't write constructors the compiler will write for you.

My impression is that the main argument for writing the constructor is that some poor programmer who doesn't understand how constructors work in Java might stumble over your code and get confused. I don't like writing code that is needlessly obscure but I don't like writing dumbed-down code either.

But this is me being obsessive and probably unreasonable. There is a world of application programmers whose central focus is on the business, not on the language, and who are not language experts. A survival technique a lot of people use is to have a consistent style, whether it is absolutely necessary is not the point.

聆听风音 2024-10-05 18:31:26

与许多“有争议”的决定一样,事实是这并不那么重要。写构造函数或者不写。对代码质量和可维护性的影响可以忽略不计。如果您与其他人一起编码,则采用相同的风格以保持一致性,但除此之外 - 随心所欲。

As with many decisions that are 'controversial', the truth is that it really doesn't matter that much. Write the constructor or don't. The effect on the quality and maintainbility of your code will be negligible. If you are coding with others then adopt the same style for consistency, but otherwise - do whichever you feel like.

年华零落成诗 2024-10-05 18:31:26

当默认构造函数是唯一的构造函数时,以空体显式编写或省略它是 100% 等效的。但是,如果您有任何显式定义的构造函数(无论是否默认),编译器都不会生成默认构造函数。这意味着,如果您依赖编译器为您生成构造函数,然后添加替代构造函数,则默认构造函数将消失。就我个人而言,我倾向于让编译器无论如何都进行生成;如果正在使用该默认构造函数,它将生成编译警告,并且很容易在此时添加。否则,为什么还要保留它呢?

When the default constructor is the only constructor, it is 100% equivalent to write it explicitly with an empty body or to omit it. However, the compiler will not generate a default constructor if you have any explicitly defined constructors, default or not. This means that if you rely on the compiler to generate a constructor for you, and later add alternative constructors, then the default constructor goes away. Personally, I would tend toward letting the compiler do the generation anyway; if that default constructor was in use it will generate compile warnings and is easy to add at that point. Otherwise, why keep it around at all?

那请放手 2024-10-05 18:31:26

虽然我有点喜欢较少的代码,但使用默认构造函数可以更轻松地在需要调试时设置断点。

不管怎样,我可以提醒你,PMD的网站也说这个问题是有争议的:)

Although I'm somewhat like lesser code but having default constructor make it easier to set a breakpoint whenever it is needed to debug.

Anyway, may I remind you that PMD's website also said that the issue is controversial :)

长发绾君心 2024-10-05 18:31:26

我会选择 Checkstyle。由于没有显式的(无参数)构造函数,没有人会知道它是否是有意的。

考虑这个实用程序类:

public class Util {
    // no constructor given - implicit default constructor
    // intended? probably not, but who knows

    public static void foo() {
      // blabla
    }

    public static void bar() {
      // more blabla
    }

}

I'll go with Checkstyle. With no explicit (noarg) constructor, no one will know if it is intended or not.

Consider this utility class:

public class Util {
    // no constructor given - implicit default constructor
    // intended? probably not, but who knows

    public static void foo() {
      // blabla
    }

    public static void bar() {
      // more blabla
    }

}
万劫不复 2024-10-05 18:31:26

默认情况下,编译器会为您生成默认构造函数,因此如果您不想指定任何特殊操作(成员的初始化不是这里的重点),那么您不必指定构造函数。

另一件事是某些类应该具有一致的状态。例如,您有一个 Book 课程。创建一本没有标题的书是没有意义的,因此有必要指定一个带有字符串参数的构造函数:

public Book(String name) {
    this.name = name;
}

至于默认构造函数,如果您应该序列化您的类或在编组/解组中使用它,则它们可能是必要的(JAXB 需要一个空)默认构造函数)。

如果这不是重点,并且您的类没有所谓的一致状态,那么绝对没有必要声明空的默认构造函数。

您应该记住,默认构造函数默认是公共的,因此如果您想对此进行一些限制,请考虑使用显式构造函数。

另外,如果您的类相当长,您可以考虑声明一个空的默认构造函数以增加可读性。

By default the compiler generates the default constructor for you, so if you don't want to specify any special actions( initializing of members is not the point here ), then you have not to specify the constructor.

Other thing is that some classes should have a consistent state. For example you have a Book class. There is no sence to create a book with no title, so it is necessary to specify a constructor with string parameter:

public Book(String name) {
    this.name = name;
}

As for default constructors they may be necessary if you should serialize your class or use it in marshalling/unmarshalling(JAXB requires an empty default constructor).

If that's not the point and your class hasn't what is called a consistent state, so empty default constructor is absolutely unnecessary to declare.

You should remember that default constructor is public by default, so consider decalring an explicit one if you want some restrictions to this.

Also if your class is rather long you can consider declaring an empty default constructor to increase readability.

莫相离 2024-10-05 18:31:26

我要补充的一件事是,如果没有指定构造函数,则有一个代码可能依赖的隐式默认构造函数。

然后,当您添加特定的构造函数时,代码可能不再编译 - 或者更糟的是,代码将无法运行(可能是对无参数构造函数的反射依赖 - 大多数 ORM 工具需要为持久实体提供默认构造函数)。

这就是为什么您应该在需要时显式添加默认构造函数。

One thing I'll add is that if there is no constructor specified, there is an implicit default constructor which code may depend on.

Then when you add a specific constructor, the code may no longer compile - or even worse, the code will not run (may be a reflective dependency on the parameterless constructor - most ORM tools require a default constructor be present for persistent entities).

Which is why you should explicitly add the default constructor if it is required.

饮湿 2024-10-05 18:31:26

IMO 类应该定义一个构造函数,因为如果您依赖默认构造函数,则实例变量将具有默认值(例如整数为零,字符串为 null 等)。如果您想在创建该类的对象时将实例变量设置为某个默认值,那么您自己定义构造函数可能是一个不错的选择。 (假设您不想使用 getter 和 setter 方法)

class Cube {
   private int length;
   private int breadth;
   private int height;

  public Cube() {
      length = 10;
      breadth = 10;
      height = 10;
  }
     ......
     ......
}

现在,当您创建 Cube1 类的对象时,该对象的所有实例变量都将设置为 10。如果您在这里不使用构造函数,则您的实例变量(长度、宽度和高度)将具有默认值(在本例中为零)。


编辑:[添加一件事]

如果您依赖编译器为您生成默认构造函数,并且您想使用一些从用户那里获取参数的构造函数,那么您必须定义默认构造函数,否则编译器将生成错误。

记住:仅当该类不包含任何其他构造函数时,编译器才会为您的类创建默认构造函数。

IMO Class should define a constructor because if you rely on default constructors the instance variables will have default values (like zero for integers, null for a String etc). If you want to set instance variables to some default value whenever an object for that class is created defining a constructor by yourself can be a good choice in that case. (Provided you don't wanna use the getter & setter methods)

class Cube {
   private int length;
   private int breadth;
   private int height;

  public Cube() {
      length = 10;
      breadth = 10;
      height = 10;
  }
     ......
     ......
}

Now when you make an object of the class Cube1 all the instance varibales of that object will be set to 10. If you don't use an constructor here your instance variables (length,breadth and height) will have the default values (zero in this case).


Edited : [Added one more thing]

If you are relying on the compiler to generate a default constructor for you and you want to use some constructors that takes argument/s from the user then you must define the default constructor else the compiler will generate error.

Remember : Compiler makes the default constructor for your class only if that class doesn't contains any other constructors.

北座城市 2024-10-05 18:31:26

我倾向于仅在需要时才编写代码。如果您不需要任何其他构造函数,请让编译器为您生成它。否则,编写您需要的所有构造函数。

I'm inclined to go with writing code only when it's needed. If you don't need any other constructors, go with having the compiler generate it for you. Else, code all the constructors you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文