views:

114

answers:

2

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.

+5  A: 

I wind up doing a lot of XML parsing with my programs, and what I usually do is to create a constant containing the tag name and use that instead. That way, if the XML tag ever changes, you only have to change your string in one spot instead of everywhere in the code.

fire.eagle
-1 If the XML syntax changes, you're gonna have to change a lot more than just the one string.
Pete Kirkham
By the syntax, are you referring to the structure? Because of course that would involve more work than changing a string. I only mentioned tag names being changed. If this xml doc is being generated by someone else and they decide to change one particular tag name, then the constant comes in handy because you only have to change the string value in one place, rather than all over your code.
fire.eagle
I found problem is a not enough well desig. Read my comment on Answer.
juanjosegzl
+1  A: 

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".

Chris Lively
I've been thinking about 'using a constant for XML ... is a waste of time...' I agree with you, I found the root problem (when you wrote well known / well defined) my xml schema is not well defined enough that's why I'm concerned about future problems.I will propose an stronger design for the XML structure and decrease risk. Thank you.
juanjosegzl