Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #2608

Re: Enums: Properties vs. Methods

From Lew <lew@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: Enums: Properties vs. Methods
Date 2011-03-30 13:17 -0700
Organization http://groups.google.com
Message-ID <fba37755-efd7-4ff7-a6a7-ee6c48cf7590@hg8g2000vbb.googlegroups.com> (permalink)
References <2f38bb8e-9a8d-4464-ad3d-b9ce0b557219@e21g2000yqe.googlegroups.com> <imsutq$115$1@news.albasani.net> <8vh9efFg01U1@mid.individual.net>

Show all headers | View raw


Robert Klemme wrote:
> Lew wrote:
> > You actually don't know what the footprint will be once Hotspot takes
> > over. With your "toy" example, those booleans might all optimize away
> > and both cases take the same memory at runtime.
>
> Just so I understand it properly: are you saying that with hotspot the
> compiler might remove members of the instances?  I am not sure how that
>

More the other way around: if it sees an opportunity to enregister
members it will remove the instance from the members.

> would work since then hotspot would need to create several different
> versions of the property getter methods (one per instance).  I believe
> this is not what hotspot can do.
>

Yes.  So?  that's what HotSpot does, except it's not one per instance,
it's N >= 1 per instance, potentially.  HotSpot optimizes for
individual hot spots in the code, hence the name.

> >> Considering that there are always only so many instances it seems the
> >> properties approach wins. It seems, custom methods in enum instances

Yes, it wins, but that has nothing to do with memory or performance in
the JVM.

For the toy example given I would expect no difference, since the
final variables ocmpile to constants anyway.

>> Your "methods" example is confusing and the purpose behind the logic
>> deeply obscured by the idioms. That alone is enough to kill it. Your
>> "properties" example was clear and concise and easy to follow.
>>
>> No-brainer.

> Well, roughly speaking the idea was this:https://gist.github.com/892503#file_valve.java
>

Side channel should not be necessary to make the point.

> Here there are boolean properties which derive from the enum, i.e.
> whether there is traffic allowed or not and whether the traffic can flow
> unlimited.
>
> Now code which uses this enum need not create switches based on concrete
> enum instances but can use boolean properties in control flow (e.g.
> print a warning if no traffic at all is allowed).  One might later want
>

THe use of values in a 'switch' doesn't seem relevant to the decision
between your approaches at all.

> to add another enum value BROKEN(false, false) which is used to describe
> the state of a broken medium.  If this is done the code that uses those
> properties to make decisions does not need to be extended.
>

Enums are not the most amenable to that kind of refactoring
regardless.  But the so-called "properties" approach sure is easier
for that situation than the so-called "methods" approach, and for the
same reason I like it in the first place.

As for JVM effects, I would expect the two approaches to be
indistinguishable.  Constant variables are compiled into constants in
the code, so they get treated identically with constants (being, after
all, constants in truth) at run time.

--
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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