Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #2752
| From | Lew <noone@lewscanon.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Enums: Properties vs. Methods |
| Date | 2011-04-02 10:36 -0400 |
| Organization | albasani.net |
| Message-ID | <in7c9p$q21$1@news.albasani.net> (permalink) |
| References | <2f38bb8e-9a8d-4464-ad3d-b9ce0b557219@e21g2000yqe.googlegroups.com> <MPG.2801085a92724d6898969e@202.177.16.121> |
On 04/02/2011 05:23 AM, Wanja Gayk wrote:
> In article<2f38bb8e-9a8d-4464-ad3d-b9ce0b557219
> @e21g2000yqe.googlegroups.com>, shortcutter@googlemail.com says...
>>
>> All,
>>
>> I am just musing about the pros and cons of using boolean properties
>> in enum classes vs. custom methods.
>> So far I found
>>
>> pro Properties:
>> - less classes
>> - when adding enum values to an enum you cannot forget to define
>> properties
>>
>> pro Methods:
>> - smaller memory footprint per instance
>
> Quite frankly: Use what's easier to read and maintain.
> Memory and runtime should be your smallest concern, assuming you use
> some common sense when chosing your algorithm, only if there's clearly a
> problem for the user and the system you should optimize to something
> probably less readable. To find bottlenecks, don't guess, but use a
> profiling tool. "jvisualvm" (which you can find in your jdk/bin folder)
> is not bad for a start.
>
>> Example
>> /** We use boolean properties. */
>> public enum Prop {
>>
>> A(true, true), B(true, false), C(false, true);
>>
>> private final boolean a;
>>
>> private final boolean b;
>>
>> Prop(boolean a, boolean b) {
>> this.a = a;
>> this.b = b;
>> }
>>
>> public boolean isA() {
>> return a;
>> }
>>
>> public boolean isB() {
>> return b;
>> }
>> }
>
> Like proposed elsewhere you could also write (untested):
>
> public enum Prop {
> //@formatter:off
> A(){
> public boolean isA(){return true;}
> public boolean isB(){return true;}
>
> },
> B(){
> public boolean isA(){return true;}
> public boolean isB(){return false;}
> },
> C(){
> public boolean isA(){return false;}
> public boolean isB(){return true;}
> }
> ;
> //@formatter:on
>
> public abstract boolean isA();
> public abstract boolean isB();
> }
>
>
> Consider that the use of boolean parameters is often quite bad to read
> and maintain. Just have a look at your boolean initializer:
>
> A(true, true), B(true, false), C(false, true);
>
> You can't tell from looking at the code, whether the first argument
> represents the return value for "isA" or "isB". From this point of view,
> with a proper formatting, I'd prefer the abstract method approach.
I get your point about the parameter approach, but in an enum where everything
is together and nothing is public that is much less of a confusion. For that
I'd go with the first form for clarity and simplicity.
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-29 08:18 -0700
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 11:47 -0400
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-30 20:00 +0200
Re: Enums: Properties vs. Methods Lew <lew@lewscanon.com> - 2011-03-30 13:17 -0700
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 00:48 -0700
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:41 -0400
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 08:26 -0700
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 22:16 -0400
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-04-02 11:56 +0200
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-04-02 10:29 -0400
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-04-03 13:14 +0200
Re: Enums: Properties vs. Methods markspace <-@.> - 2011-03-29 09:26 -0700
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-30 20:06 +0200
Re: Enums: Properties vs. Methods markspace <-@.> - 2011-03-30 12:40 -0700
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 00:38 -0700
Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 19:03 +0200
Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 19:04 +0200
Re: Enums: Properties vs. Methods Roedy Green <see_website@mindprod.com.invalid> - 2011-03-29 12:49 -0700
Re: Enums: Properties vs. Methods Roedy Green <see_website@mindprod.com.invalid> - 2011-03-29 12:52 -0700
Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 22:45 +0200
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 17:08 -0400
Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-29 23:25 +0200
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 19:05 -0400
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-29 19:40 -0400
Re: Enums: Properties vs. Methods Robert Klemme <shortcutter@googlemail.com> - 2011-03-31 02:57 -0700
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:52 -0400
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:54 -0400
Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-30 18:40 +0200
Re: Enums: Properties vs. Methods Lew <lew@lewscanon.com> - 2011-03-30 10:33 -0700
Re: Enums: Properties vs. Methods Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2011-03-30 20:43 +0200
Re: Enums: Properties vs. Methods Lew <lew@lewscanon.com> - 2011-03-30 13:20 -0700
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-03-31 08:55 -0400
Re: Enums: Properties vs. Methods Wanja Gayk <brixomatic@yahoo.com> - 2011-04-02 11:23 +0200
Re: Enums: Properties vs. Methods Lew <noone@lewscanon.com> - 2011-04-02 10:36 -0400
Re: Enums: Properties vs. Methods Wanja Gayk <brixomatic@yahoo.com> - 2011-04-02 21:20 +0200
csiph-web