【发布时间】:2013-09-18 21:20:43
【问题描述】:
我在旧代码库中遇到了这个问题:
string[] deptNo = UPC.getDept().Split(new Char[] {'.'});
...并认为这会更简单:
string[] deptNo = UPC.getDept().Split('.');
...但如果我进行此更改,如果点的特征有可能产生某种“魔法”,导致整个意大利面屋滑落,则不想更改它。
更新
作为对给出的警告的回应:是的,我知道您的意思是,在吃旧意大利面时需要极其谨慎地进行操作。对这段代码看似无害的更改:
private void comboVendor_SelectedValueChanged(object sender, EventArgs e)
{
try
{
if ((comboVendor.SelectedIndex >= 0) && (cmbVendor.Text != comboVendor.Text))
{
string t = comboVendor.Text;
t = t.Substring(0,maxVendorChar);
t = t.Substring(0,substrLen);
t = t.Trim();
cmbVendor.Text = t;
cmbVendor.Focus();
}
}
catch (Exception ex)
{
Duckbill.ExceptionHandler(ex, "frmInv.comboVendor.SelectedValueChanged");
}
}
...即,将“Trim()”附加到“.Text”的实例,导致错误消息“指定的参数超出了有效值的范围。”因为“maxVendorChar”不大于被子字符串的值。所以我不得不把它改成这样才能让它工作:
private void comboVendor_SelectedValueChanged(object sender, EventArgs e)
{
int substrLen;
try
{
if ((comboVendor.SelectedIndex >= 0) && (cmbVendor.Text.Trim() != comboVendor.Text.Trim()))
{
string t = comboVendor.Text.Trim();
substrLen = GetLowerInt(t.Length, maxVendorChar);
t = t.Substring(0,substrLen);
t = t.Trim();
cmbVendor.Text = t;
cmbVendor.Focus();
}
}
catch (Exception ex)
{
Platypus.ExceptionHandler(ex, "frmInv.comboVendor.SelectedValueChanged");
}
}
private int GetLowerInt(int first, int second)
{
return first < second ? first : second;
}
在远处,我听到一只乌鸦在“再也没有”的叫声,但我不确定他在说什么。
更新 2
每当人们注意到这个项目是多么的僵硬,他们几乎总是建议我重构它;然而,如上所述,有时一点点重构就会引发一系列震耳欲聋的爆炸,让我希望自己已经病得够重了。真正的解决方案是用更好的方法和至少不那么古老的技术完全重写这个项目。这就是我的愿景;不过,就目前而言,维护模式似乎已成为主流。
【问题讨论】:
-
这两个调用约定在 C# 中使用
params关键字时是相同的,Split()就是这样做的。
标签: c# refactoring simplify