在不扩展任何内容的类中调用super()是不好的做法吗? [英] Is it bad practice to call super() in a class that doesn't extend anything?

查看:100
本文介绍了在不扩展任何内容的类中调用super()是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到人们在没有明确扩展任何内容的类中显式调用 super()

Quite often I see people explicitly calling super() in a class that doesn't explicitly extend anything.

public class Foo
{
  public Foo()
  {
    super();

    //do other constructor stuff
  }
}

现在我知道这是完全合法的,如果省略,编译器会添加调用,但我仍然认为这是不好的做法。每当我看到这个,我想知道程序员是否对继承有误解,并且所有类都隐含地扩展 Object

Now I know this is perfectly legal and if omitted the call is added by the compiler but I still think its bad practice. Whenever I see this I wonder if the programmer has some misunderstanding of inheritance and the fact that all classes implicitly extend Object.

我应该将其添加到我们的编码标准/最佳实践中吗?当我看到他们这样做时,我是否应该拉开团队中的其他开发人员?它是我个人的错误熊,但我不知道我是否只是挑剔。

Should I add this to our coding standards/best practice and should I pull up the other devs in my team when I see them do it? Its a personal bug-bear of mine but I don't know if I'm just being picky or not.

推荐答案

我是一个一致的人,我也反对微观管理每一点风格。拥有大量编码约定,特别是当其中一些似乎是任意的时,是阻止人们关注它们的一部分。我认为编码指南应该简化为最有价值的实践,以提高能力。通过强制执行这种做法,可读性,可维护性,性能等提高了多少?

While I'm one whose all for consistency, I'm also against micromanaging every bit of style. Having a huge list of coding conventions, particularly when some of them seem arbitrary, is part of what discourages people from following them. I think coding guidelines should be streamlined to the most valuable practices that improve the -ilities. How much is readability, maintainability, performance, etc improved by mandating this practice?

虽然我个人的做法是致电 super()在这种情况下,它不够严重,我会将它包含在编码指南中,或称其为审查其他程序员代码时的缺陷。然而,我仍会提及它并在代码评论中讨论它(不是缺陷,只是作为一种风格问题),试图游说更多的工程师 NOT 使用这个电话。

While my personal practice is to not to call super() in such a case, its not serious enough that I would include it in the coding guidelines or call it a defect in reviewing other programmer's code. I would however still however mention it and discuss it in code reviews (not as a defect, just as a question of style) in an attempt to lobby more engineers to NOT use the call.

这篇关于在不扩展任何内容的类中调用super()是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆