Submission: switch (...) instanceof feature

Gabriel Belingueres belingueres at gmail.com
Wed Apr 22 06:39:09 PDT 2009


You may try to get rid of the instanceof by using "another level of
indirection". For example in this case, with a Strategy as the cache
value:

interface ValueStrategy {
  CharSet lookup();
}

class CharSetContentStrategy implements ValueStrategy {
  CharSet value;
  public Charset lookup() {
    ....
  }
}

class StringContentStrategy implements ValueStrategy {
  String value;
  public Charset lookup() {
    ....
  }
}

declare:
final Map<String, ValueStrategy> cache;

then the lookup method would be:

synchronized Charset lookup(String lowCanonical) {
  ValueStrategy v = cache.get(lowCanonical);
  return v.lookup();
}

Please note that in some point the above code may require that you
make an instanceof to know which strategy to create, but this code can
be isolated in a unique method, instead of in many methods as you have
now. (For example, creating a factory class of strategies.)

(Of course, another different question is if this will perform faster
than using instanceof everywhere.)

2009/4/22, Ulf Zibis <Ulf.Zibis at gmx.de>:
> Am 22.04.2009 06:55, Derek Foster schrieb:
> > Like Gabriel, I have severe reservations about this proposal. I am
> concerned that it will encourage people to avoid the common OOP "best
> practice" of using virtual method dispatch in favor of doing an explicit
> "switch" on each subtype of a type, in cases where doing so is not
> necessary.
> >
> > I concede that there are a few (FEW!) places where doing the equivalent of
> a switch on instances of a type is necessary (for instance, if the type is
> in a library you don't have the ability to change). I can see some value in
> this proposal for that purpose. However, I would very much not like to see
> use of this construct become commonplace. I already see too many instances
> of people doing the equivalent of "if (object.type == FOO) {doThis(); } else
> if (object.type == BAR) { doThat(); }" instead of writing
> "object.doSomething();"
> >
> >
> >
>
> I like to provide an example where
>
> "object.doSomething();"
>
>  doesn't work:
>
>   synchronized Charset lookup(String lowCanonical) {
>       // cache is initialized with available Charset classes names
>       Object o = cache.get(lowCanonical);
>       // Check cache first
>       if (o instanceof String) {
>           // Instantiate new charset
>           Charset cs = newCharset((String)o, lowCanonical);
>           // Cache it
>           if (cs != null)
>               cache.put(lowCanonical, cs);
>           return cs;
>       }
>       return (Charset)o;
>   }
>
>
>  This would look much nicer, as it avoids casting:
>
>   synchronized Charset lookup(String lowCanonical) {
>       // cache is initialized with available Charset classes names
>       switch (Object o = cache.get(lowCanonical) instanceof ?) {
>           case String :
>               // Instantiate new charset
>               Charset cs = newCharset(o, lowCanonical);
>               // Cache it
>               if (cs != null)
>                   cache.put(lowCanonical, cs);
>               return cs;
>           case Charset :
>               return o;
>       }
>   }
>
>
> Refer:
> https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/tags/milestone4/src/sun/nio/cs/FastCharsetProvider.java?rev=684&view=markup
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6790402
> http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001182.html
> http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001328.html
>
> - My example could also be smartly coded without switch..case statement, if
> we would enhance if statement accordingly
> - I also would like to have possibility to declare variables in if clause,
> like it's possible in for clause:
>
>       if ((Object o = cache.get(lowCanonical)) instanceof String)
>           ... ;
>       else if (o instanceof Charset)
>           ... ;
>
> -Ulf
>
>
> > Derek
> >
> > -----Original Message-----
> >
> >
> > > From: Gabriel Belingueres <belingueres at gmail.com>
> > > Sent: Mar 30, 2009 7:31 AM
> > > To: Jeroen van Maanen <jeroen at entreact.com>
> > > Cc: coin-dev at openjdk.java.net
> > > Subject: Re: Submission: switch (...) instanceof feature
> > >
> > > IMO I'm against this.
> > >
> > > First, it is against current best practices for the design of
> > > object-oriented software to make easier to code something with a case
> > > statement on types/classes.
> > >
> > > Second:
> > > void log(Object object) {
> > >  switch (object) instanceof {
> > >  case String:
> > >    logger.debug("'" + object + "'");
> > >  case Date:
> > >    logger.debug(object.getTime());
> > >  case void:
> > >    logger.debug("null");
> > >  default:
> > >    logger.debug("<" + object.toString() + ">");
> > >  }
> > > }
> > >
> > > It think it is clearer (when possible) writing it with several
> > > overloaded methods and double dispatching.
> > >
> > > ....
> > >
> > >
> >
>
>



More information about the coin-dev mailing list