Do you consider the use of switch to be in bad form? I rarely have use for switch, but in this particular case I'd like the fall through. However, given that CENTER and RIGHT must both modify x the need to check for x == 0 makes me think it's not a good approach.
Tony Morris
Java Q&A (FAQ, Trivia)
But here you've got a fallthrough and a case that checks whether it was reached by a fallthough, and that's really pretty gross.
I'd just hoist "y" out of the switch, make all the cases explicit, and write:
switch(n) where n is of type T
prefer:
interface Strategy{Association get(T t);}
with an implementation that matches your otherwise switch.
It certainly seems like a better alternative to switch, though isn't this case a bit trivial for it?
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
I don't believe in a case that is too trivial. Trial and error has brought about this perception. There is never a good reason to use switch/case, always prefer a strategy, even if only because it is a more appropriate abstraction (if it isn't, a switch/case certainly isn't either).
There is no emoticon for what I am feeling!
There is no emoticon for what I am feeling!
Originally posted by Jeff Albrechtsen:
Ken,
Kudos on using the new type-safe enums. But reading the code in your first and second posts makes me think you are misinterpreting how a switch works with this enum type: switch(loc){...} will throw a NullPointerException if loc is null. You may be assuming that it will skip the rest of the statement instead, but noooo....
Anyhowdy, it may be more fun to nix switch and pix strategy. Instead of introducing an interface and implementing it separately, consider "constant-specific" methods, as demonstrated by the Operation enumeration in Sun's "New Features and Enhancements" document:
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
Tony Morris
Java Q&A (FAQ, Trivia)
Kudos on using the new type-safe enums. But reading the code in your first and second posts makes me think you are misinterpreting how a switch works with this enum type: switch(loc){...} will throw a NullPointerException if loc is null. You may be assuming that it will skip the rest of the statement instead, but noooo....
Anyhowdy, it may be more fun to nix switch and pix strategy. Instead of introducing an interface and implementing it separately, consider "constant-specific" methods, as demonstrated by the Operation enumeration in Sun's "New Features and Enhancements" document:
Originally posted by Tony Morris:
Binding an enum to an implementation detail (which is completely unrelated to an enum) is horrible best case scenario.[/QB]
There is no emoticon for what I am feeling!
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Jeff Albrechtsen:
Binding addition to PLUS, subtraction to MINUS, multiplication to TIMES and division to DIVIDES is binding a completely unrelated implementation detail?
Originally posted by Ken Blair:
With all this talk about switch/case being bad I'm starting to wonder if there's ever a case where it's appropriate?
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Ken Blair:
Isn't an enumerated type more or less an identifier? As I understand it an enumerated type is responsible for what it identifies, now how to perform whatever operation it identifies. TIMES would indicate multiplication, I'm not sure it should be responsible for performing that multiplication too.
There is no emoticon for what I am feeling!
I'm not comfortable making ex cathedra pronouncements (did I sound like a certain poster whose initials are TM, for an instant, there?
)
So I can't say, out of context, whether's Sun's Operation example is bad, but where's the harm?
It's certainly reasonable to have a type for operations. Then why not enumerate those operations? This assumes operations aren't open-ended, but few calcuators have docks for plugging in keypad extensions
![]()
Tony Morris
Java Q&A (FAQ, Trivia)
Under a given axiom, which I claim *you* assume, and for now I will call it "valid software" (it does have a formal, but verbose expression), a switch/case is bad practice.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by u johansson:
Well, you know, you shouldn't use switches in code because if you do you hardcode and that's not good. OO is about not doing that.
Originally posted by Ilja Preuss:
Frankly, I don't know what Tony means by "valid software".
Why *I* program, there are basically two things I care about:
- the software does what the customer needs it to do. It seems obvious to me that this can be done using as many switch statements as you like.
- the software is easy to maintain and extend. This is where type cases (switch- or if-else-statements based on a type/enum etc.) get in the way, because every time the number of types changes, all the type cases need to be examined (and it's actually easy to miss one and thereby introduce a nasty bug).
Originally posted by Ilja Preuss:
Having said that, putting polymorphic methods on a typesafe enum is a rather elegant way to implement the Strategy or State pattern with a small, fixed (at compile time) number of instances, in my opinion.
Originally posted by Ilja Preuss:
Of course there are cases where I don't want to do this, where I want more flexibility. I often use the Visitor pattern in those cases. Not sure how the DoubleOperation interface is going to help, as Tony didn't show how it would be implemented (not a switch statement inside, I assume).
Originally posted by Ilja Preuss:
And always keep in mind that a design isn't written in stone once it's implemented - you always can (and should) refactor when circumstances change.
Originally posted by u johansson:
Well, Ilja Preuss , why don't you tell us how enums makes us implement the Strategy and State patterns please?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ken Blair:
I believe when writing software it's not just important that it be easy to extend and maintain, but that it be easy to reuse. This implicitly means to me that objects should take on as little responsibility as possible, ideally doing one thing only. If it has one responsibilty, if it does one thing only, then it can be used anywhere that is required. If it has two responsibilities, then it can not necessarily be used anywhere one is required and not the other.
My experience thus far has been that objects too often take on too much responsibility under the notion of being centralized and easy to use. Sure, they end up easy to use under the very narrow context it was intended for, but they also then end up nearly impossible to reuse.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
"I'm not back." - Bill Harding, Twister
Originally posted by Jim Yingst:
[QB]
Well, the case values of a switch statement are required to be compile-time constants, or enums (which the compiler converts to compile-time constants, which is much the same thing except the term "compile-time constant" was defined before enums were introduced. Aaanyway...)
Since when does the compiler convert enums to compile-time constants?
[Tony]: A switch forces the use of "compiled strategies" with the potential to also force a O(n) lookup (as opposed to always O(1) with a strategy).
I suppose in theory that potential is there. If one assumes the compier writers are sufficiently foolish. However when compiled to bytecode, the switch statement also has the potential (and in actual practice, the extreme likelihood) to take advantage of jumpswitch and tableswitch statements, which are a considerably faster than hashtable lookups. Of course this is the sort of optimization that's quite irrelevant to the majority of programs, and can be counterproductive to flexibility. However the fact remains that there are some programs that need to parse large amounts of data quickly, and in such situations it may be perfectly reasonable to sacrifice some flexibility and OO purity for the sake of performance. Thus, I can't agree to never use a switch statement, though I can certainly agree with avoiding it in most cases.
Can you back that up? I'm assuming you're going to argue that a O(1) lookup is faster on a tableswitch than when using some kind of associative type (if not, can you beat O(1)?). Assuming this is the case, may I steal one of your arguments? The one about compilers that should do optimisations and all that? I'll bet not. What if I produced a case for you? What then? I'll tell you what, since you seem to have such validity in your words, I'll propose that you write me a switch statement, any switch statement you like, you can even compile it yourself, I won't touch it, and I'll beat it. Surely you can't pass that one up!?
---
And a couple of truly irrelevant digressions...
according to you, right? of course
---
[Tony]: Independent of my existence (and yours) there exists a reality - this is an assumption that I hope we all agree on.
Not really. Since you've been fond of inserting irrelevant physics comments amidst discussions of software engineering, I must point out that while Einstein was a staunch defender of the idea of objective reality, it's been pretty much done away with by the EPR paradox and the subsequent work of John Bell, Alain Aspect, and others. Physics, fundamentally, doesn't seem to allow for a reality independent of the observer. No matter how much that might satify our intellectual ideas of how we think the universe should work.
Well sir almighty, this part deserves a chuckle. First, I don't see the irrelevance in stating a basis of reasoning when we are... er... reasoning. Next, wtf does physics have to do with anything? You might mean perhaps philosophy or, if you permit your mind to ponder into that wild territory, astrophysics, but in any case, I'm merely attempting to portray a method of science - a more plausible basis than the egotistical ideas that are typically thrown around at least. Finally, I strongly urge you to take a second, and perhaps more, look at the work of those who you cite. I'm most willing to discuss it in great detail, much much more than some silly software engineering exercise, but of course, we'll take it off somewhere else - I look forward to it. Please exercise objectivity for the sake of constructive progressions.
---
Sometimes it's funny and I amuse myself, sometimes it's funny and I put myself up for criticism. At the end of the day, it's still funny. I expect nothing less than the wrath!
Tony Morris
Java Q&A (FAQ, Trivia)
"I'm not back." - Bill Harding, Twister