4 ms·
Good job! One question though. In cases like this when there is generally no state, isn't it better to make the methods static?
by thwaw 10y ago
Good job! One question though. In cases like this when there is generally no state, isn't it better to make the methods static?
- scarmig 10y agoDepends, but objects aren't just about state. There's dynamic dispatch too. You can have different stateless strategies, and at runtime you might want to be able to swap one out for another. If that was a requirement, static methods don't work.
- bfung 10y agoLooks like parent post is on the money for the reason this library was done w/o static methods. Note that there are interfaces in the library. https://github.com/tdebatty/java-string-similarity/blob/master/src/main/java/info/debatty/java/stringsimilarity/Levenshtein.java#L12 https://github.com/tdebatty/java-string-similarity/blob/mast... The example code in README.md can probably do this instead: MetricStringDistance msd = new Levenshtein(); System.out.println(msd.distance("My string", "My $tring")); MetricStringDistance msd2 = new MetricLCS(); System.out.println(msd2.distance("ABDEF", "ABDIF"));
- scarmig 10y agoFancy seeing you here! Hope you're enjoying your vacation :)
- nugator 10y agoStatic method calls in Java code is a nightmare when testing.
- wfunction 10y agoAre they any different in other languages? What's special about Java?
- TeMPOraL 10y agoWhy? Especially if we're talking stateless methods?
- hellrich 10y agoAm I missing something? I regularly write static methods and test them with JUnit.
- gopalv 10y ago> I regularly write static methods and test them with JUnit Implementing mock objects which return "unexpected" results is impossible with static methods. The standard 1 interface + 1 impl pattern in Java is just so that the Proxy.newProxyInstance can create a decorated or mocked object for testing. So you can test the methods directly, but you can't write failure-inducing methods (like a connect exception throwing one) which test the methods which use it.