r/badcode • u/monkeysupersonic • Jul 09 '20
java Good practice: Always call java.lang.Object's constructor
51
u/harryFF Jul 09 '20
Been like 2 years since i touched java, what's wrong with this one again?
2
u/gvcallen Jul 09 '20
What are you coding in now out of curiosity?
4
u/harryFF Jul 09 '20
In a C#/SQL role primarily, but i also manage some Python and one Angular project :)
0
Jul 09 '20
[deleted]
20
Jul 09 '20
Oh yeah, super is for base classes from which the class inherits from, right? Like
base(params)in C#?18
Jul 09 '20
Yes. In case of no params, there's no need to call it. And certainly no need to call in a base class (that only inherits from the top level Java class).
99
40
14
51
u/LinuxGeek747 Jul 09 '20
The only thing wrong I see are the package-private fields (which I doubt is what the author intended). It's not really a bad practice to call Object's constructor. Although it gets called implicitly, calling it explicitly does not have any effect. It's like multiplying with 1 in maths. Math savvy people will say "Why" but programmers may say "Chill dude it's just preprocessed code; the 1 was a macro". In this case, the author may had came from Python backgrounds (Remember PEP 8: Explicit is better than implicit).
21
Jul 09 '20
Never have I ever seen someone calling
super()for an type in python that inherits from object. That statement by itself doesn't even do anything, it just refers to the base class.If you mean what I think you do;
super().__init__()still serves no functionality. In fact, it does nothing special: source.7
u/Jonno_FTW shameless Jul 09 '20
You no longer need to inherit from
objectin python.1
Jul 09 '20
You do not need to inherit from it, no, but it is implicitly inherited from object - therefore it is inherited from it.
0
Jul 09 '20
[deleted]
3
u/Jonno_FTW shameless Jul 09 '20
It was need in python2, since it gave some of the nice features of new-style objects. https://stackoverflow.com/a/45062077/150851
2
u/b1ackcat Jul 09 '20
Explicit is better than implicit
It always makes me giggle that this is one of the main tenants of PEP8 when so much of python is designed to act like magic.
Don't get me wrong, I love the language. I just think it's funny.
8
5
u/qqwy Jul 09 '20
What about 'type' being a string rather than using an enum, a sum type (do newer Javas have these?) or subtyping.
4
u/halfanothersdozen Jul 09 '20
This isn't "bad" code. It's just newb code. Possible autogenerated code, honestly.
3
u/qqwy Jul 09 '20
'Bad' is always subjective. Personally I don't feel like autogenerated code belongs on r/badcode because it was never meant for human consumption in the first place. (now if the code generation itself is convoluted, thát is juicy content 😜)
3
u/Akangka Jul 09 '20
Of course, this protect the code from the cases that Java devs want to add a funky code to the Object's constructor. /s
1
u/-Dueck- Jul 09 '20
My year 1 programming module at university said you should always add the super(), regardless of whether it was actually useful. Oh well. There are much worse things to do.
1
u/MachineGunPablo Jul 09 '20
You laugh at this but in C++ the call to super is not implicit if you define a constructor yourself.
1
u/MysticTheMeeM Jul 13 '20
Late to the party but it is.Any constructor will, by default, call the default constructor for any base classes. Should the base class not have a default constructor, a compile-time error will be thrown.
So the code:
class foo : public bar{public:foo() {} //Calls bar() before foo()foo(args...) {} //Calls bar() before foo(args...)foo() : bar(args...) {} //Calls bar(args...) but not bar()};is valid (apart from multiple default constructors) despite implicitly calling bar's constructor.
1
u/wizzwizz4 Jul 09 '20
You actually had to do this at some point in Python 2 for some reason (not sure why; memories are hazy).
1
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Sep 08 '20
Image Transcription: Code
public class Message {
String address;
String timestamp;
String type;
public Message() {
super();
}
public String getAddress() {
return this.address;
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
-4
0
Jul 09 '20
[deleted]
4
u/WheresTheSauce Jul 09 '20
Someone is using Eclipse, and didn't uncheck the default call to super() while generating a constructor.
Why on earth would that be the default?
0
Jul 09 '20
[deleted]
1
u/d360jr Jul 09 '20
When is it needed? Haven’t worked in Java for a bit :/
3
u/itzNukeey Jul 09 '20
When the class inherits it usually needs to call parent constructor to initialize properly
1
u/WheresTheSauce Jul 10 '20
I work in Java and C# daily and I use base constructors maybe 1% of the time.
383
u/GlowingOrb Jul 09 '20
The only bad thing is the getADress method which returns an address, but not a dress