Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you avoid Guavas Ordering.usingToString()?

This question was prompted after reading Joshua Bloch's "Effective Java". Specifically in Item #10, he argues that it is bad practice to parse an object's string representation and use it for anything except a friendlier printout/debug. The reason is that such a use "is error-prone, results in fragile systems that break if you change the format". To me it looks like Guava's Ordering.usingToString() is a spot on example of this. So is it bad practice to use it?

like image 308
m2o Avatar asked Dec 15 '22 07:12

m2o


2 Answers

Well, if the sorting is only used for deciding in which order to display things to a user, I'd argue it's part of "friendlier printout/debug".

If, however, your codes correctness depends on the ordering, then I'd argue that it's indeed a bad idea to depend on toString.

like image 109
Joachim Sauer Avatar answered Dec 30 '22 04:12

Joachim Sauer


As the author of that method, I would agree: it's really just a crutch. For those "look, I just need an Ordering<Object>, dammit" cases. It should probably be removed, since you can get its behavior with Ordering.onResultOf(Functions.toStringFunction) anyway.

like image 25
Kevin Bourrillion Avatar answered Dec 30 '22 05:12

Kevin Bourrillion