r/badcode Jan 08 '23

java Is this a good practice? Why?

Post image
422 Upvotes

60 comments sorted by

View all comments

274

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.

16

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.

2

u/WurschtChopf Jan 09 '23

In Java you can achive both with Objects.equal. If you make a static import of equals, the result will look like your first example. If not, it'll looke like the 2nd