I'm creating a parser for a specific XML structure and I'm facing a possible hardcoding issue. Here:
我正在为一个特定的XML结构创建一个解析器,我正面临一个可能的硬编码问题。在这里:
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?
文本“default”是Xml结构中的有效标记,是硬编码吗?我可以使用另一种技术来搜索默认标签吗?
I considered using a doctype but, how can I specify than 'default' is the tag for defaults values?
我考虑过使用doctype,但是,如何指定“default”是默认值的标记?
Maybe is not hardcoding because this tag is my standard.
也许不是硬编码,因为这个标签是我的标准。
Thank you for your help.
谢谢你的帮助。
2 个解决方案
#1
1
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...
另一方面,使用常量for ?XML或“编码”是浪费时间,因为这些是众所周知的/定义良好的项……
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.
另一方面,打字错误的可能性也很大。当您使用一个常量时,您有编译时支持,以确保您在任何地方说“DEFAULTPROPERTY”时,它在任何地方或任何地方都是正确的。然而,使用字符串的方式做事情意味着一个问题可能直到运行时才会出现,或者直到它们发生在你的代码的一小部分中。
I guess all of that was a round about way of saying, "use a constant".
我猜所有这些都是关于“使用常数”的一种说法。
#2
5
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.
我用我的程序做了大量的XML解析,我通常做的是创建一个包含标记名的常量,并使用它。这样,如果XML标签发生了变化,您只需要在一个地方更改您的字符串,而不是在代码中的任何地方。
#1
1
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...
另一方面,使用常量for ?XML或“编码”是浪费时间,因为这些是众所周知的/定义良好的项……
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.
另一方面,打字错误的可能性也很大。当您使用一个常量时,您有编译时支持,以确保您在任何地方说“DEFAULTPROPERTY”时,它在任何地方或任何地方都是正确的。然而,使用字符串的方式做事情意味着一个问题可能直到运行时才会出现,或者直到它们发生在你的代码的一小部分中。
I guess all of that was a round about way of saying, "use a constant".
我猜所有这些都是关于“使用常数”的一种说法。
#2
5
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.
我用我的程序做了大量的XML解析,我通常做的是创建一个包含标记名的常量,并使用它。这样,如果XML标签发生了变化,您只需要在一个地方更改您的字符串,而不是在代码中的任何地方。