【发布时间】:2011-12-01 09:14:28
【问题描述】:
我不确定这个问题是不是很傻,但我真的很想得到一些意见。我将直接提供一个我目前正在做的事情的例子。
我正在使用 HtmlAgilityPack 来解析一些 HTML 页面。
我的代码中有一个方法,它接收 HtmlDocument 并进行解析。目前,代码是这样的:
private void OperateOnDocument (HtmlDocument pageSource)
{
HtmlNode node;
node = pageSource.DocumentNode.SelectSingleNode (/*XPath to find a node */);
// do some operation on the extracted HtmlNode.
node = pageSource.DocumentNode.SelectSingleNode (/* XPath to find another node */);
// do some operation on the newly acquired node.
// Likewise, reuse the same reference variable "node" to extract all the nodes and operate on them.
}
优点:使用单个引用变量对所有节点进行操作。
缺点:您不知道单个解析提取的节点是什么,因为所有提取的节点都有不同的值和用途(一个节点可能包含费率,另一个可能包含城市名称。)
另一种方法是:
private void OperateOnDocument (HtmlDocument pageSource)
{
HtmlNode idNode = pageSource.DocumentNode.SelectSingleNode (/*XPath to find the node containing id*/);
// parse the text and store it in string.
HtmlNode rateNode = pageSource.DocumentNode.SelectSingleNode (/* XPath to find the node containing the rates */);
// parse the text into decimal.
// Likewise, use separte meaningful names of reference variables to extract all the nodes and operate on them.
}
优点:为每个正在操作的节点单独命名有意义的名称。
缺点:有时,操作数可能会增加到 10;因此,有 10 个不同的参考变量。
那么,我的问题是第二种方法是否真的值得做?或者由于参考变量太多,它可能会有点昂贵? .Net 中的这些变量有多昂贵?或任何其他一般的语言/框架?
【问题讨论】:
标签: .net reference language-design