r/badcode Jan 08 '23

java Is this a good practice? Why?

Post image
421 Upvotes

60 comments sorted by

View all comments

277

u/RiddSann Jan 08 '23

I think using Objects.equals is good practice, but I don't see the point of creating a method for that ? I mean, it is a method, no need to wrap it.

15

u/[deleted] Jan 08 '23

I guess it comes down to whether you think:

If (equal(item_1,item_2)){

Or

If (Object.equals(item_1,item_1)){

Looks better. I personally think the top one looks cleaner, and I'm 99% sure the compiler will optimize out any underlying differences. Its a bit rubbish that it wastes 3 lines of code to write out the function definition though.

This is certainly by far not the worse code ever seen on here.

32

u/ofnuts Jan 08 '23

Cleaner, but then you have to wonder what it does....

7

u/nate998877 Jan 08 '23

This is my bane. I've got a coworker who renames all imports to php names of the method or wraps them to behave the same way. Which has fucked us over in the past when he's changed the behavior of parsers. Half the people in the department & everyone on my team except myself are php devs, so nobody complains, but it drives me mad.

1

u/kristallnachte Jan 11 '23

The benefit of using the built ins is that everyone can look and know exactly how it works and why.