1

特定のXML構造のパーサーを作成していますが、ハードコーディングの問題が発生している可能性があります。ここ:

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

リテラルの「デフォルト」はXml構造の有効なタグですが、これはハードコーディングですか?別の手法を使用してデフォルトタグを検索できますか?

Doctypeの使用を検討しましたが、デフォルト値のタグが「default」よりも指定するにはどうすればよいですか?

このタグが私の標準であるため、ハードコーディングされていない可能性があります。

ご協力ありがとうございました。

4

2 に答える 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.

于 2010-07-07T16:27:35.700 に答える
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...

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

于 2010-07-07T16:38:45.670 に答える