15

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
  }
}

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.

Qwerky
  • 18,217
  • 6
  • 44
  • 80

6 Answers6

22

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?

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.

Bert F
  • 85,407
  • 12
  • 106
  • 123
  • I like this answer, code reviews seem the best place to address the issue as a point of style, and clear up any potential misunderstanding the programmer has. – Qwerky Jan 13 '11 at 11:58
9

Adding code that isn't needed is always a bad practice. It will make the code less readable as you would need to stop and start wondering why this line of code is added and if it is actually there for a reason.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
6

it will implicitly get called so no need to do this

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 3
    I'm not asking whether I need to call it, I'm asking whether it's bad practice. – Qwerky Jan 13 '11 at 11:08
  • 1
    +1 - Dont know who downvoted you, but I think your answer is correct. – Sachin Shanbhag Jan 13 '11 at 11:08
  • 2
    @Qwerky `if(true == true ){ //yes it is true}` will you do it ? – jmj Jan 13 '11 at 11:10
  • 2
    I'm not the downvoter, but I feel that the OP already pointed this information in his question. The question does not ask whether its necessary (the OP already stated its implied: `if omitted the call is added by the compiler`). The question was whether its its important enough to take action on it: add to coding guidelines and tell other devs is a defect. – Bert F Jan 13 '11 at 11:30
3

It needs to be pointed out, that:

  • Calling super() is completely pointless, no matter if class inherits anything or not; it is meaningful only if you do not call implicit constructor of base class
  • Question should be "Is doing something completely pointless bad practice?". Answer is obvious - Yes!
NSNoob
  • 5,548
  • 6
  • 41
  • 54
peenut
  • 3,366
  • 23
  • 24
1

Explicit super constructor call often added by automatic code generator by default, so it's not always about programmers bad practice. Or maybe bad practice of leaving doubtlessly silly generated code as is.

Eclipse especially generates this super call (generate constructor from superclass), however, I don't have much experience on other IDEs. (Write your experiencs in comments.) My another 'favourite' default generated code is interfaces' method declarations: 'public abstract' which is completely redundant.

pcjuzer
  • 2,724
  • 4
  • 25
  • 34
0

super() call gets implicitly added by JDeveloper. While writing a class of your own, there is no need and usefulness of doing this.