Re: =?UTF-8?B?SmF2Ye+/vXMgQnJva2VuIEJvb2xlYW5z?=
Ian Shef wrote:
In any case, I have drifted from the original thread. I suggest: If you
like ?: , go ahead and use it. Just be judicious.
I'm in complete accord with your suggestion.
?: never was intended for complex or lengthy expressions, not even in C. It's
a convenience for situations such as the one you posited upthread, a simple
conditional expression that would otherwise require a whole other method.
Obviously the choice balances the cost (however you perceive it) of the one
idiom against the other. When, as in the situations you described, the cost
of the ?: exceeds the cost of the method, or the method's benefits really
start to pay off, you eschew the operator and go with the method.
In your example, the expression was so simple that the cost of relocating a
reader's attention to a separate method lacked the advantage over the tight
grouping of a simple decision operator expression. Or perhaps in real life
the factory method's advantages are strong even here.
Joshua Bloch's /Effective Java/, Item 1, is, "Consider static factory methods
instead of constructors". It is not, "Use static factory methods instead of
constructors".
http://java.sun.com/docs/books/effective/
One (among several) motivation for factory methods is opacity - to hide
implementation (example: Collections.unmodifiableMap()). Another is
self-documentation - the name reveals what you're getting (same example).
Other times you have less need for what factories provide, or even want to
reveal some things about implementation (HashMap). Then you use a constructor
- it hurts nothing and it's simpler than dealing with tons and tons of factory
methods. Sometimes you feel like a nut, sometimes you don't.
http://en.wikipedia.org/wiki/Mounds_(candy)
So as Ian said, be judicious.
--
Lew
Honi soit qui mal y pense.