The problem with getting rid of the "undesirables" is that sooner or later someone will decide that YOU are an undesirable.
Stephan van Hulst wrote:Static methods are never inherited.
Junilu Lacar wrote:Treat any use of default interface methods that are not motivated by this as highly suspicious and potentially dangerous. I may be wrong but I get a sense that you may be leaning towards a misuse of the default interface method feature just by the way you posed your question. Whatever it is you're thinking of doing, you probably shouldn't do it.
Henry Wong wrote:Additionally, the idea of using default methods isn't common (in fact, I would argue it is near zero chance). After all, how many of your colleagues only use default methods, on the chance that you might want to derive from more than one of them? Or do you only code in default methods so that your colleagues can use your code in a "multiple inheritance" way?
Stephan van Hulst wrote:I agree that any language feature shouldn't be used to try and bend the rules, but Java interfaces have become like traits in other OO languages since the addition of default methods. Avoiding the use of default methods for anything else but adding new methods to existing interfaces is shooting yourself in the foot, because you're throwing out the many advantages of using traits.
Junilu Lacar wrote:However, if someone comes to me and says, "I think I want to use a default method in this new interface we're publishing...just because I already have an idea for how I would implement it in this particular scenario," then my Spidey senses would be tingling like crazy and I would say, "Stop it right there, buddy! Let go of your mouse and step away from the keyboard!"
Junilu Lacar wrote:Another key to making default interface methods safe(r), if you really were to use them, is simplicity and singularity in purpose. Default behavior should be simple and easy to understand. Simplicity and singularity in purpose make the intent and behavior of the default method clear(er). Anything less will make a default implementation suspect to me. I would even dare to say that due diligence would require a set of unit tests to be provided for a default interface method to document the designer's original intent and to help others ensure that their implementations of that interface don't violate the Liskov Substitution Principle with respect to the default method.
Stephan van Hulst wrote:I took a look at your other post, and it didn't clarify for me what you mean by "inheritance relationships" or why you need to pay careful attention to them. Are you referring to 'self-use'?
Stephan van Hulst wrote:Is this what you're referring to? Otherwise I see no drawbacks in using default methods.
Stephan van Hulst wrote:
I absolutely agree. But once again I don't think this is a special case for default methods. An API designer can provide tests for any interface method, whether they come with a default implementation or not.Junilu Lacar wrote:Another key to making default interface methods safe(r), if you really were to use them, is simplicity and singularity in purpose....
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:I don't think it's very fair to blame default methods for that, because ever since we got generics the API exploded with methods that had to do things specially for primitives.
If Java had just allowed us to treat primitives as objects, then a lot of the API bloat would never have come to pass.
Your opinion is that the single method interfaces we had, should have remained that way, and that utilities should have been added to dedicated utility classes.
So if we don't consider the static methods and the overloads for primitives, we're left with one reversed() method and the three thenComparing() methods. They *greatly* improve fluent interface, and I think the benefits you get from the readable and succinct code you can write with them vastly outweighs the cleaner API you get by moving them to a utility class.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:But they are consistent? thenComparing() has a an overload for each overload of comparing(), and only has one extra overload that chains an existing comparator. I can write the following, which is way more fluent than the nested mess I would get with utility methods:
).
- willing to concede that a "then" method may be a good idea; but it should have been called thenCompare() and defined solely on the basis of a "key extractor" function. And that's how I intend to do it with my Order class.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:1. My view always has been (and still is) that Comparators are an alternative to Comparable, and so should NOT allow nulls. They are (or were) also meant to provide a single ordering to a type, functional or otherwise.
2. Both interfaces should have had a "reversing" method since their inception; and while it's probably too late for Comparable, reversed() is arguably 6 releases too late for Comparator (and definitely 2 too late for me
).
3. If you agree with me about point 1, then "null" ordering is an extension of a Comparator, which can be easily fulfilled with an abstract class that allows it as an option. Mine is called Order, and in my case it also provides a reverse() method. I haven't yet decided whether to take it out or not.
4. NaturalOrder is a concrete (and final) implementation of Order.
5. Combining of Comparators is a whole new behaviour which just happens to provide an order (small 'O'); so I've implemented it as a concrete class called Index that implements Comparator.
Now you may see this as overkill, but to me it keeps to the "single responsibility principle" that we keep banging on to beginners about. The reason that I hate the "new improved" Comparator is that it's lost its focus, and has now become pretty much "anything that provides an order for a type".
I am however - you may be glad to know
- willing to concede that a "then" method may be a good idea;
but it should have been called thenCompare() and defined solely on the basis of a "key extractor" function.
Stephan van Hulst wrote:I can write the following, which is way more fluent than the nested mess I would get with utility methods...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:So let's say I want to add nulls to a TreeSet, how would I do that without a Comparator that accepts null?
String implements Comparable. What could I expect to happen if I called helloWorld.reversed()?
I don't understand what you mean by extension. Regardless, if I had an instance of Comparator, how would I get a new one that orders nulls first?
So to get one, I would have to call new NaturalOrder<>()?
I don't understand why it's called Index or how I would use it. Do you have some examples of your orders in action?
Well, that's pretty much what I've always seen it as. Comparators simply ARE "anything that provides an order for a type", that's what they were before Java 8 as well. Some languages call it an Order, and some even have support for a PartialOrder. I think Comparator is a bad name, because it describes what it does and not what it is.
Single responsibility is to improve readability of code.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:I think it's a very good convention to name methods that return 'operations' with a present participle. A method named thenCompare() would imply that the comparison was actually the thing being carried out when you call it. It doesn't, it returns an operation that performs the comparison at a later stage.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Stephan van Hulst wrote:Hey Winston, thanks for your lengthy posts. I didn't get back to you because I've been mulling your ideas over and I can't come up with a reply that would advance the discussion.
For now, let's agree to disagree, and I'm sure we'll lock horns the next time the subject comes up![]()
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
