Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe11.iad.POSTED!83aa503d!not-for-mail From: Daniel Pitts User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Swing is dead! Long live Swing. References: <4f3d9152$0$291$14726298@news.sunsite.dk> <4f3d96c1$0$293$14726298@news.sunsite.dk> <6f72r.16359$L12.15612@newsfe23.iad> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Lines: 57 Message-ID: X-Complaints-To: abuse@newsrazor.net NNTP-Posting-Date: Wed, 29 Feb 2012 17:11:07 UTC Date: Wed, 29 Feb 2012 09:10:52 -0800 X-Received-Bytes: 2950 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12535 On 2/29/12 12:00 AM, Lew wrote: > Wanja Gayk wrote: >> markspace wrote: >>> ... For situations where I might be tempted to just use strings, >>> I try to substitute enums. For example, instead of >>> >>> bind( someComponent, "event-name" ); >>> >>> I'd use this: >>> >>> bind( someComponent, Events.NAME ); >>> >>> It provides automatic syntax checking, and is much easier to refactor if >>> names need to be changed or moved around later. >>> >>> Any thoughts on this idea? >> >> I think the same way. >> I'm even going further and strongly propose preferring Enums to boolean >> parameters and this is why: >> http://brixomatic.wordpress.com/2010/02/24/boolean-harmful/ > > +1 > > This might irritate those who already find Java verbose, since 'String's > are more compact to declare, but type safety and refactorability [sic] > is a payoff in many situations. > > I'm even worse, because I pump a "friendly" string representation into > the enum. > > «An enum type should override this method when a more > "programmer-friendly" string form exists.» I'm even worse than that. My designs often include replacing a boolean with an Enum that has *logic* in it. Flyweight strategy pattern. public enum FooStrategy { ONE_WAY { public void handle(Foo foo) { foo.doItOneWay(); } }, ANOTHER { public void handle(Foo foo) { foo.doItAnother(); } } ; public abstract void handle(Foo foo); } This is more verbose but ends up being more flexible and easier to maintain. Enums can even implement interfaces, so you could add additional layer of abstraction which allows custom or statefull implementations alongside the the enum implementations.