Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Swing is dead! Long live Swing. Date: Wed, 29 Feb 2012 00:00:23 -0800 Organization: albasani.net Lines: 96 Message-ID: References: <4f3d9152$0$291$14726298@news.sunsite.dk> <4f3d96c1$0$293$14726298@news.sunsite.dk> <6f72r.16359$L12.15612@newsfe23.iad> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.albasani.net CTSfKS6PDNCR0yzWTCfhxiFSUfYWfBIDc0NXndzQsR99hDdo21WAg+ICxHUa93dgCRoQnQm67tzzi5tPcKrOE/pNW/rs3t574e8fKj+5E684s48sW0gTzkOyL2cCjqMO NNTP-Posting-Date: Wed, 29 Feb 2012 08:00:24 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="oXl+x4Ygr7m2MxVpDGKkoGXgs7cP/v6TRUm38+T5ASTgmN0TMg5c0sb2+6LlRf88B7qKMbclIbS+C7rkWz46MsDkFFYZRtEwAghAQeBX+eFL4tML9BzMfha425mMCQVy"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: Cancel-Lock: sha1:sWJyuCB+Dcb9Zi+b071zbNieHVA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12518 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.» Necessitating a corresponding 'public static E fromString(String rep)'. 'fromString()' compares 'toString()' strings first; if those fail it delegates to 'valueOf()'. package eegee; /** * Essential {@code enum} implementation with "friendly" representation. */ public enum Essential { FOO("foo"), BAR("bar"), FANCY("fancy form"), ANOMALY("useful to hold log or error messages"), ; private static final long serialVersionUID = 1L; private final String representation; /** * Constructor setting the friendly representation. * @param rep String friendly representation of constant. */ private Essential(String rep) { this.representation = rep; assert this.representation != null; } @Override public final String toString() { return representation; } /** * Look up enum constant from String representation. * @param rep String representation of enum constant. * @return Essential constant matching the representation. */ public static Essential fromString(String rep) { if (rep == null) { return null; } for (Essential value : values()) { if (rep.equals(value.toString())) { return value; } } return valueOf(rep); } } -- Lew Honi soit qui mal y pense. http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg