r/badcode • • Jul 09 '20

java Good practice: Always call java.lang.Object's constructor

Post image
704 Upvotes

52 comments sorted by

383

u/GlowingOrb Jul 09 '20

The only bad thing is the getADress method which returns an address, but not a dress

49

u/oktupol Jul 09 '20

I do this typo all the time when programming. Address is written with a single d in my native language, German. Fortunately IntelliJ marks typos like this

46

u/metahuman_ Jul 09 '20

Ah, we code in frenglish where I work. GetAdresse, GetDictionnaire, sendMessageToUtilisateur...

9

u/leftsaidtim Jul 09 '20

I’m really curious if this works well for you and your team. As a native English speaker I’ve had to work in mixed languages codebases that my company inherits sometimes and have always found it to be significantly worse than when the codebase has names that stick more to one language or the other.

But since I moved abroad a while ago, I’m finding that this seems to work well for some of my clients and I’m wondering if I was too narrow minded to actually see the benefits here.

13

u/LeiziBesterd Jul 09 '20

I find it awful. It's always best if you can do everything in English, because all the non proprietary will be English anyway.

Source: lives in non English speaking country and have worked with that type of mixed language codebase

2

u/metahuman_ Jul 10 '20

Well all the business related code is in french because it's all done here, in France. We deal with some foreign companies and use their terms when appropriate but thats all. It's way easier to speak to the business guys when you both use the same words

→ More replies (1)
→ More replies (1)

2

u/blahbah Jul 10 '20

Ouch... I got unpleasant flashbacks from these.

I'd try to push for more consistent coding standards, and also update my resume...

2

u/metahuman_ Jul 10 '20

We are actually pushing toward consistent coding standards ^^ and the french part is sadly kind of needed because the business part is in french and no one can translate/understand it all in english, and mixing with english allows us to keep short method names. Like we use "get" and not "recuperer", etc

→ More replies (1)
→ More replies (1)

2

u/moekakiryu Jul 10 '20

The worst thing is when your editor learns the typo and then a while later your program fails when you actually spell the word correctly

66

u/CorrenteAlternata Jul 09 '20

but not a dress

in which case it should be named getDress or at least getADress

38

u/BlueMarble007 Jul 09 '20

Or, if we abide by how it’s described, getOneDress

18

u/EnemysKiller Jul 09 '20

return dresses[0]

29

u/GlowingOrb Jul 09 '20

return new getDressFactoryBuilder().createDressFactory().createDress()

→ More replies (2)

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

u/[deleted] Jul 09 '20

[deleted]

20

u/[deleted] Jul 09 '20

Oh yeah, super is for base classes from which the class inherits from, right? Like base(params) in C#?

18

u/[deleted] 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

u/carfniex Jul 09 '20

the real bad code is String timestamp

40

u/lateToThePartyyy Jul 09 '20

I wanted a dress and all I got was a string.

3

u/rar_ita Jul 09 '20

That’s actually a good pun

14

u/Squigmeister2000 Jul 09 '20

"extends Object"

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

u/[deleted] 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 object in python.

1

u/[deleted] 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

u/[deleted] 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

u/PazuzuEU Jul 09 '20

pay respect to The Object

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

u/Alainx277 Jul 09 '20

super() calls the base class constructor, but Message does not inherit.

25

u/Chefzor Jul 09 '20

In Java everything inherits from the Object "superclass"

0

u/[deleted] 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

u/[deleted] 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.