Winston Gutkowski wrote:
Ulf Lindqvist wrote:You're wrong because the "probability of choises" criterion I mention in my previous reply is implementation independent.
I'm not "wrong" unless you can:
(a) Point me to the relevant paragraph in the JLS or JVM spec that specifies HOW a switch statement is executed.
(b) Convince me that the choice of if over switch - even when probabilities are not uniform - is so overwhelming that it overrides the many other concerns already mentioned.
Your statement also only makes sense if you take into account the length of time it takes to evaluate each test expression and order by probability products - and do you really want to do that? I suspect also that for some varieties of test and a good optimizing compiler, switch might only ever need to evaluate the test condition once, whereas an if stack is far more likely to need to evaluate each case in turn.
Ulf Lindqvist wrote:My advice is in the best practice category. It's not to be found in the JLS. Actually very little advice of this kind is.
The grammar and sematics of a language is one thing, it's effective usage based on practice and experience and knowledge of computer science in general is another.
For example considering the JLS alone you would have no idea the OO paradigm is specifically supported by Java because it's not even mentioned once.
Finally my advice would be different if the JLS guaranteed the switch statement made selections in constant time, but it doesn't.
I stand by my advice: Prefer a switch but consider replacing it with an if-else chain when the selection probabilities are known and (strongly) non-uniform, and then arrange the chain so the more likely a selection is to be true the earlier it's evaluated.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jayesh A Lalwani wrote:
Actually, no you are wrong. I ran this unit test.
Ulf Lindqvist wrote:My advice is in the best practice category. It's not to be found in the JLS.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ulf Lindqvist wrote:
Jayesh A Lalwani wrote:
Actually, no you are wrong. I ran this unit test.
Sure you did but what are you measuring really? It seems the test code is spending most of the time doing other things than comparing the alternatives. And do you really print during execution? If you do it's bad and if you don't the code does nothing and is subject to optimization and may even be removed so that's bad too. In short you don't know what you're measuring here.
Jayesh A Lalwani wrote:
No, I know what I'm testing here, because the only differrence between the test methods is the if-else chain vs switch statement.
Winston Gutkowski wrote:
"The Java programming language is a general-purpose concurrent class-based object-oriented programming language"
doing in the Preface to the first Edition, written by Gosling himself (2nd paragraph).
Ulf Lindqvist wrote:
Winston Gutkowski wrote:
"The Java programming language is a general-purpose concurrent class-based object-oriented programming language"
doing in the Preface to the first Edition, written by Gosling himself (2nd paragraph).
Okay, in the preface to the first edition of the JLS you managed to find a reference to OO. But then it's gone because the JLS shouldn't care as you should know.
Ulf Lindqvist wrote:Okay, in the preface to the first edition of the JLS you managed to find a reference to OO. But then it's gone because the JLS shouldn't care as you should know.
The JLS supports my advice.
It does that by not giving any performance guarantees on the switch statement...My advice is fully supported by the JLS.
Your opposition is based on your personal view only. You have no support in the JLS whatsoever.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:So I gather this topic was extracted from this previous topic. The resulting thread here is missing quite a bit of context, particularly the performance test code that people are going on about.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Consider Paul's rocket mass heater. |