xom-interest AT lists.ibiblio.org
Subject: XOM API for Processing XML with Java
List archive
- From: Elliotte Rusty Harold <elharo AT metalab.unc.edu>
- To: Laurent Bihanic <laurent.bihanic AT atosorigin.com>, XOM-interest <XOM-interest AT lists.ibiblio.org>
- Subject: [XOM-interest] Code compactness vs instanceof
- Date: Thu, 19 Sep 2002 10:18:45 -0400
At 10:33 AM +0200 9/19/02, Laurent Bihanic wrote:
- Code compactness: As I already said on the jdom-interest mailing list, there are cases (e.g. TreeNode.getChild()) where instanceof tests make the code very unclear and thus harder to maintain.
I know Elliotte does not like the idea of a int getNodeType() method but we need to find a way to remove these ugly tests.
I agree the situation is imperfect. I think there's a bit of a mismatch between the XML model and the Java model, but I don't see how we can do any better than I've done.
Why would
if (node.getNodeType() == Node.ELEMEBT_TYPE)
be preferable to
if (node instanceof Element)
?
They're both ugly. I think the second one is less ugly. With getNodeType() you could call it once at the top of the tree:
int type = node.getNodeType();
if (type == Node.ELEMENT_TYPE) ...
else if (type == Node.TEXT_TYPE) ...
but I still think the instanceof approach looks at least as clean and arguably cleaner. Int types would also allow switch statements, but that's even uglier and less clear for at most a very minor performance optimization.
Do you see something different? What do you think the code should look like? (Actually what I think the code should look like is polymorphic method dispatching, where I can pass a Node to a process() method and have the runtime choose the overloaded method to dispatch based on the node's subtype, but that's not a feature Java gives us so we have to deal. :-( )
--
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo AT metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
| XML in a Nutshell, 2nd Edition (O'Reilly, 2002) |
| http://www.cafeconleche.org/books/xian2/ |
| http://www.amazon.com/exec/obidos/ISBN%3D0596002920/cafeaulaitA/ |
+----------------------------------+---------------------------------+
| Read Cafe au Lait for Java News: http://www.cafeaulait.org/ |
| Read Cafe con Leche for XML News: http://www.cafeconleche.org/ |
+----------------------------------+---------------------------------+
-
[XOM-interest] Firts impressions,
Laurent Bihanic, 09/19/2002
- Re: [XOM-interest] Firts impressions, Elliotte Rusty Harold, 09/19/2002
- [XOM-interest] ClassLoader issues, Elliotte Rusty Harold, 09/19/2002
- [XOM-interest] Code compactness vs instanceof, Elliotte Rusty Harold, 09/19/2002
-
[XOM-interest] CDATA handling,
Elliotte Rusty Harold, 09/19/2002
-
Re: [XOM-interest] CDATA handling,
John Cowan, 09/19/2002
- Re: [XOM-interest] CDATA handling, Elliotte Rusty Harold, 09/19/2002
-
Re: [XOM-interest] CDATA handling,
John Cowan, 09/19/2002
-
[XOM-interest] Document portions,
Elliotte Rusty Harold, 09/19/2002
- Re: [XOM-interest] Document portions, Laurent Bihanic, 09/20/2002
- [XOM-interest] Automatic detach, Elliotte Rusty Harold, 09/19/2002
-
[XOM-interest] Tree walking,
Elliotte Rusty Harold, 09/19/2002
-
Re: [XOM-interest] Tree walking,
Johannes Döbler, 09/19/2002
- Re: [XOM-interest] Tree walking, Elliotte Rusty Harold, 09/19/2002
-
Re: [XOM-interest] Tree walking,
Johannes Döbler, 09/19/2002
- [XOM-interest] HTML Serializer, Elliotte Rusty Harold, 09/19/2002
Archive powered by MHonArc 2.6.24.