1

Suggest the following Java class:

    /** Utility class for working with DOM nodes.
     */
    public class DomNodes {
         public static boolean isElement(Node pNode, String pElementName, String... pAttributes) {
              if (pNode.getNodeType() == Node.ELEMENT_NODE) {
                  final Element e = (Element) pNode;
                  final String uri = e.getNamespaceURI();
                  if (uri == null  ||  uri.length() == 0) {
                      if (pElementName.equals(e.getLocalPart())) {
                          if (pAttributes == null  ||  areAttributesMatching(pNode, pAttributes)) {
                              return true;
                          }
                      }
                  }           
              }
         }
    }

Now, suggest the following two uses of that class:

isElement(node, "foo");
isElement(node, "foo", "attribute0", value0, "attribute1", value1, "attribute2", value2);

It is obvious to me, that the second invocation depends on the construction of a string array. In other words, there is a small performance penalty for the construction of that array.

The first invocation, however, isn't obvious: The compiler could implement this by passing a newly constructed array with no elements. However, it might just as well pass the value null, or a constant array.

In the former case, I could help the compiler by adding a method isElement(Node pNode, String pElementName). Question: Would you recommend that?

user1774051
  • 843
  • 1
  • 6
  • 18
  • 1
    "However, it might just as well pass the value null" it would never do this, because `null` and empty array are different things. – Andy Turner Dec 17 '19 at 10:35
  • 2
    "Question: Would you recommend that?" I would recommend profiling the difference. – Andy Turner Dec 17 '19 at 10:36
  • 1
    Whilst it's not quite a duplicate, you may find https://stackoverflow.com/questions/41918934/what-is-the-point-of-overloaded-convenience-factory-methods-for-collections-in-j of use. – Andy Turner Dec 17 '19 at 10:42

0 Answers0