Search code examples
xmldesign-patternsdoctypeanti-patternshardcode

Is this hardcoding? How can I avoid it?


I'm creating a parser for a specific XML structure and I'm facing a possible hardcoding issue. Here:

private function filterDefaultParams($param){
    #FIXME Hardcoding?
    return array_key_exists('default',$param);
}

The literal 'default' is a valid tag in the Xml structure, is this hardcoding? May I use another technique to search for defaults tags?

I considered using a doctype but, how can I specify than 'default' is the tag for defaults values?

Maybe is not hardcoding because this tag is my standard.

Thank you for your help.


Solution

  • Is it hardcoding? Yes.

    That said, you need to weigh a couple of factors. First, consider the likelihood of the "default" property name ever changing versus the additional code required to declare and track various constants.

    Another thing to consider is consistency. If you have other places where the property names might change then you'll want to use constants for all of them.

    On the other hand, using a constant for ?XML or "encoding" is a waste of time as those are well known/well-defined items...

    Ont he other other hand, is the likelihood of typos. When you use a constant you have compile time support to ensure that everywhere you say "DEFAULTPROPERTY" it is correct everywhere or nowhere. Whereas using the string way of doing things means that a problem might not show up until runtime or until they happen upon that one little used section of your code.

    I guess all of that was a round about way of saying, "use a constant".