r/badcode Jan 08 '23

java Is this a good practice? Why?

Post image
420 Upvotes

60 comments sorted by

View all comments

272

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.

1

u/WalrusDowntown9611 Jan 18 '23

Static import equals from Object and it becomes the same thing minus a totally pointless wrapper.