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 value开发者_如何学Gos?
Maybe is not hardcoding because this tag is my standard.
Thank you for your help.
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.
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".
精彩评论