Junilu Lacar wrote:Besides asking for specifics, I would probably choose to throw an IllegalArgumentException instead with a message that the argument should have at least two elements -- it's a step further back from violating the Principle of Least Surprise than returning a value that's not actually a part of the list.
@Campbell - JavaDocs are a poor substitute
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
I stand corrected and bow to your logic.Winston Gutkowski wrote:Actually, they are
Winston Gutkowski wrote:Have to disagree there. Documentation, in this case, is essential. In the absence of documentation, I would agree that POLS is probably the way to go.
Junilu Lacar wrote:Aren't you one of those who rage against java.util.Date?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
William Koch wrote:The assignment assumed that there were at least 2 Integer elements in the list.
Junilu Lacar wrote:
Winston Gutkowski wrote:Have to disagree there. Documentation, in this case, is essential. In the absence of documentation, I would agree that POLS is probably the way to go.
In this case, documentation would be useful, not necessarily essential. POLS wins over documentation any day in my book -- you can document it all day if you want, if it's still surprising to me as a user of the method/class, documentation isn't worth a darn keystroke.
Because saying that there is a 2nd value in a collection that doesn't have one make no sense to me -- there is no sensible "default" value. It's like asking me, "Who is your second child?" and I either had only one child or no children; it makes no sense for me to say "Well, I don't have one but let's just say his name is Bob," does it?Winston Gutkowski wrote:but we're used to defaults; why not here?
Jeff Verdegan wrote:Undefined behavior for invalid input is perfectly valid in some cases.
Junilu Lacar wrote:
Jeff Verdegan wrote:Undefined behavior for invalid input is perfectly valid in some cases.
That's moot
The OP already clarified the requirement that the method expects a List that has at least 2 elements in it.
Therefore, an IllegalArgumentException rather than some "default" value or leaving the behavior as undefined is a better design choice, IMO.
Junilu Lacar wrote:
Because saying that there is a 2nd value in a collection that doesn't have one make no sense to me -- there is no sensible "default" value.Winston Gutkowski wrote:but we're used to defaults; why not here?
Junilu Lacar wrote:Because saying that there is a 2nd value in a collection that doesn't have one make no sense to me -- there is no sensible "default" value. It's like asking me, "Who is your second child?" and I either had only one child or no children; it makes no sense for me to say "Well, I don't have one but let's just say his name is Bob," does it?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jeff Verdegan wrote:In some cases, the identity element (which would be Integer.MIN_VALUE here) is an appropriate output for max(empty set)
Aren't you actually validating my analogy here? Isn't this just like shrugging when asked a non-sensical question? Also, a default value is a valid value that you use in the absence of any other valid value. What you're talking about is an ERROR FLAG value, not a default.Winston Gutkowski wrote:The analogy is inaccurate... b)return a value that indicates that the question was nonsensical.
Junilu Lacar wrote:In this case, documentation would be useful, not necessarily essential. POLS wins over documentation any day in my book -- you can document it all day if you want, if it's still surprising to me as a user of the method/class, documentation isn't worth a darn keystroke.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Paul Clapham wrote:Here's the logic, then. ...
So finally, what's the maximum of an empty array of numbers? By now we know we're looking for the identity element of the "max" operator. And since Integer.MIN_VALUE max x = x for all values of x (in the range of Java ints, that is) it follows that Integer.MIN_VALUE is the answer. (In real life where you're working with arbitrary integers, the answer is minus infinity.)
Junilu Lacar wrote:Aren't you actually validating my analogy here? Isn't this just like shrugging when asked a non-sensical question? Also, a default value is a valid value that you use in the absence of any other valid value. What you're talking about is an ERROR FLAG value, not a default.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:I contend that max({}) is UNDEFINED
Jeff Verdegan wrote:I used to think that too. Paul was kind enough to set me straight, though the idea still felt squicky for a while.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Junilu Lacar wrote:In this case, documentation would be useful, not necessarily essential. POLS wins over documentation any day in my book -- you can document it all day if you want, if it's still surprising to me as a user of the method/class, documentation isn't worth a darn keystroke.
So how does your understanding of POLS work in the case of an indexOf() method, Junilu? There you have the case of a method which is highly likely to return a value that indicates "not found". In such a case you absolutely should NOT throw an Exception since, as we both know, Exceptions are used for "exceptional" behaviour; and yet you cannot return a valid index either. Furthermore, you don't have a single identity value (as you do with max()).
I've actually heard it argued that the best value to return is Integer.MIN_VALUE, since it's the least likely to occur (at least not quickly ), and allows for the fact that in some situations an "index" could be negative; but in general I stick with -1, because that's what the rest of the world does.
Winston
Junilu Lacar wrote:
So how would secondBiggest({Integer.MIN_VALUE, Integer.MIN_VALUE}) being equal to secondBiggest({}) make any sense to you? The logic there simply does not compute.
Junilu Lacar wrote:That seems like quite a leap. Following your previous logic: max(A, B) ==> max(A union B) ==> max({} union B) ==> ?what is here to get to the next step? ==> max({Integer.MIN_VALUE} union B)
Where did Integer.MIN_VALUE come from all of a sudden? There is no equivalence between max({}) and max({Integer.MIN_VALUE}) -- I contend that max({}) is UNDEFINED, not Integer.MIN_VALUE. If you want to say for purposes of calculation, you can consider max({}) as Integer.MIN_VALUE, then that's fine but logically speaking, if max({}) were to return a set, it would return a null set, not a set with one element (Integer.MIN_VALUE).
Junilu Lacar wrote:Haven't stumped me yet
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Junilu Lacar wrote:In the case of secondBiggest(), Integer.MIN_VALUE is NOT outside the range of valid values because you can have a list that is {Integer.MIN_VALUE, Integer.MIN_VALUE}
So how would secondBiggest({Integer.MIN_VALUE, Integer.MIN_VALUE}) being equal to secondBiggest({}) make any sense to you? The logic there simply does not compute.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Imagine that this method needed to be run on a billion such arrays. Are you happy to have your program plough through 999 million of them and then throw an Exception? Not I.
Junilu Lacar wrote:...but I would do what I tell my guys to do to avoid RTEs because they violated a contract:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Just for comparison purposes, my solution to the method would probably be:Not optimal, but fairly generic.
Campbell Ritchie wrote:I would suggest if (list == null) throw new NullPointerException();
You probably do not have a contract that you throw no Exceptions. You have a precondition that nulls are not permissible, so enforce it by throwing that Exception. And put a @throws tag in your /** comment */.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Now that really would start a fightWinston Gutkowski wrote:… should you throw NPE or IllegalArgumentException? …
Junilu Lacar wrote:Are we all good with this? I am.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:I was taught a long time ago (and in a different language) that errors should be thrown early and LOUD; and simply "let[ting] NullPointerException's get thrown as they will" violates that principle in spades, resulting in it becoming an effect rather than a cause.
Junilu Lacar wrote:I think that even the creators of Java had some idea of this (or maybe I'm making a stretch here a little bit too) because there is, in fact, a constant defined in java.lang.Double for NEGATIVE_INFINITY. The best alignment with the mathematics would be to say that "Double.NEGATIVE_INFINITY is the identity element of max()" but that's only good for collections of Double.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:we could have returned a constant (perhaps equal() to Integer.MIN_VALUE) that was unambiguous in its meaning.
Junilu Lacar wrote:A constant that is equal() to Integer.MIN_VALUE doesn't fit the bill though because it's still ambiguous; equals() is, after all, transitive...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:You're missing the point. Sure, it might be equal() to some valid value, but it can't be '==' to it.
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|