Do you put a super() call a the beginning of your constructors?

风格不统一 提交于 2019-12-08 17:38:05

问题


This is a question about coding style and recommended practices:

As explained in the answers to the question unnecessary to put super() in constructor?, if you write a constructor for a class that is supposed to use the default (no-arg) constructor from the superclass, you may call super() at the beginning of your constructor:

public MyClass(int parm){
  super(); // leaving this out makes no difference
  // do stuff...
}

but you can also omit the call; the compiler will in both cases act as if the super() call were there.

So then, do you put the call into your constructors or not?

On the one hand, one might argue that including the super() makes things more explicit. OTOH, I always dislike writing redundant code, so personally I tend to leave it out; I do however regularly see it in code from others.

What are your experiences? Did you have problems with one or the other approach? Do you have coding guidelines which prescribe one approach?

BTW: A related question (just for reference):

Is there a reason to explicitly code a default constructor when there are no other constructors?


回答1:


I don't write the super() call for the same reason I don't write unnecessary casts:

It adds noise to the code without providing any additional information (neither to the compiler nor to the developer reading my code).




回答2:


The default settings of some versions of some IDEs generate it automatically, so that's one reason you might be seeing it.

Whether to write it or not depends on who will be reading it. I prefer not to add it, but if you are not certain that team members understand what the compiler does, and how object construction proceeds, you may add it "just in case".




回答3:


I don't, but the argument given to me for doing this while at university was that it makes things more explicit, so you don't 'forget' that a superclass constructor is being called.

I just don't see the need tho.




回答4:


I use the super() only when the constructor super() needs parameters.




回答5:


What are your experiences? Did you have problems with one or the other approach?

I never called the default constructor (explicit) and never faced any problems




回答6:


I never add it and I was deeply shocked when a colleague didn't understand that there was an implicit call made to the super class so perhaps I should start doing it.




回答7:


Advantage: Anyone reading your code knows that you really want to call the default constructor, you did not leave the call out by mistake.

Disadvantage: You clutter your code with unimportant information.

Usually, if a language lets you omit some instruction, it is not bad style doing so. I think it is a question of weighing the pros and cons. For instance, explicitly writing super() when your constructor has parameters and the base class also has parametered constructors available may be a good idea.




回答8:


I actually put the super() call in as I find it to be a helpful memory aide. When I have written a piece of code a few months ago and return to it, the super() reminds me when I am looking at the constructor that the class I wrote inherits from something.




回答9:


I try to avoid writing code where calling super() is necessary, because I prefer composition over inheritance.

If I have to derive from another class, I try to put both behind an own interface and pass one of them to the other as a dependency.

If I have a lib class, however, which I cannot give an extra dependency, the first thing I try is to use a decorator.

If this doesn't work, and I have to derive from an implementation, I think it doesn't matter whether to write super() or not, since it happens so rarely ;)

So, all in all, I think this question is pointless.



来源:https://stackoverflow.com/questions/2712971/do-you-put-a-super-call-a-the-beginning-of-your-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!