当你看到这样的事情时你会重构吗?或者你只是插上鼻子继续前进?
public Collection<DataValidationRuleBase> GetFieldValidationRules(String key)
{
Collection<DataValidationRuleBase> found = null;
try
{
this.mRules.TryGetValue(key, out found);
}
catch (ArgumentException ex)
{
//log the error
Log.Error(ExceptionHandling.BuildExceptionMessage(ex));
return null;
}
return found;
}
Run Code Online (Sandbox Code Playgroud) 我有一些代码是相同的,除了一定的分配顺序发生在略有不同的顺序.
很容易将其分解为类型的方法参数int[],它表示顺序.
但是,我发现它并不是最清楚的.
另一种选择是将它们分解为类型的对象AssignmentOrders.我也可以对对象构造函数中的值进行验证,而这些值是我无法对数组进行的.这将是从书,"引入参数对象"重构重构.
我想知道这种特殊的重构是否过度,我应该坚持下去int[]?
码:
原件的三个样本:
private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections)
throws Exception {
String[] sections = line.split(deliminator);
String value1 = sections[0].trim();
String value2 = sections[1].trim();
String value3 = sections[4].trim();
String value4 = sections[2].trim();
String value5 = sections[3].trim();
//........
}
private static PersonDetails parseLine(String line, String deliminator)
throws Exception {
String[] sections = line.split(deliminator);
String value1 = sections[1].trim();
String value2 = sections[0].trim();
String value3 = sections[2].trim();
String …Run Code Online (Sandbox Code Playgroud) 我是javascript的新手并试图重构一些代码,显然我在javascript中缺少一些我想学习的东西.所有5个列表框都选择了以下内容后,此代码会生成一个值:
function GetTotal() {
//_listSeverity = document.getElementById("_listSeverity");
function ParseListBoxvalue(listBox) {
return parseInt(GetListBoxValue(listBox),10);
}
_listSeverity = document.getElementById("<%= _listSeverity.ID %>");
_listAssociate = document.getElementById("<%= _listAssociateImpact.ID %>");
_listCustomerImpact = document.getElementById("<%= _listCustomerImpact.ID %>");
_listRegulatoryImpact = document.getElementById("<%= _listRegulatoryImpact.ID %>");
_listShareholderImpact = document.getElementById("<%= _listShareholderImpact.ID %>");
_calculatedTotal = (ParseListBoxvalue(_listAssociate) +
ParseListBoxvalue(_listSeverity) + ParseListBoxvalue(_listCustomerImpact)
+ParseListBoxvalue(_listRegulatoryImpact) + ParseListBoxvalue(_listShareholderImpact)
)/ 5;
if (isNaN(_calculatedTotal))
document.getElementById("_total").innerHTML = "Not enough information";
else
document.getElementById("_total").innerHTML = _calculatedTotal;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试重构为for循环以消除一些代码重复.我尝试了很多方法,if(typeof _calculatedValue !='undefined')我在谷歌上发现,看看是否可以解决它.据我了解,我没有遇到范围问题,因为唯一的实际范围是由function(){}声明限制的.这永远不会产生价值.我意识到/ 5它还没有进入它,但这对我来说似乎并不是因为它总能产生一个NaN.
function GetTotal() {
//_listSeverity = document.getElementById("_listSeverity"); …Run Code Online (Sandbox Code Playgroud) 如何将下面的代码中的PHP函数转换为非函数.
<?php
require_once ('./mysqli_connect.php'); // Connect to the db.
function make_list ($parent)
{
global $tasks;
echo '<ol>';
foreach ($parent as $task_id => $todo)
{
echo "<li>$todo";
if (isset($tasks[$task_id]))
{
make_list($tasks[$task_id]);
}
echo '</li>';
}
// Close the ordered list:
echo '</ol>';
}
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT task_id, parent_id, task FROM tasks WHERE date_completed='0000-00-00 00:00:00' ORDER BY parent_id, date_added ASC");
if (!$dbc)
{
// There was an error...do something about it here...
print mysqli_error();
} …Run Code Online (Sandbox Code Playgroud) 我有下面这个重复的代码,我假设这可以合并,但如果你注意到每个字典是不同的通用字典:
dictionary1是类型的
Dictionary<int, ContinuousIntegrationSolution>
Run Code Online (Sandbox Code Playgroud)
而dictionary2的类型是:
Dictionary<int, BugTracker>
DataTable dt = GetDataTable("CI");
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
int id = Convert.ToInt32(dr["id"]);
string name = dr["name"].ToString();
_dictionary1[id] = new ContinuousIntegrationSolution(){Name = name};
}
DataTable dt1 = GetDataTable("Bug_Tracking");
for (int i = 0; i < dt1.Rows.Count; i++)
{
DataRow dr = dt1.Rows[i];
int id = Convert.ToInt32(dr["id"]);
string name = dr["name"].ToString();
_dictionary2[id] = new BugTracker() { Name = name };
}
DataTable dt2 = GetDataTable("SDLC"); …Run Code Online (Sandbox Code Playgroud) 在Visual C#中,我可以根据其定义重命名实体,只需两次单击,就可以更新对该实体的所有引用.我如何在Visual C++中执行此操作?如果它不受支持,是否有另一个支持它的IDE?
请注意,在C++的情况下,我还想要自动标头/实现同步,所以我几乎不需要做重复的工作.
我有一个关于重构PHP代码的快速问题.以下是三个功能.前两个看起来非常相似,只有一个if语句不同.第三个通过使用标志结合前两个.这是最好的做法吗?这里似乎可以使用一个标志,但如果我们将来需要添加更多标志呢?什么是最佳做法?
谢谢.
function check_contact_email($email)
{
$this->db->select('COUNT(login) AS count');
$this->db->from('users');
$this->db->where('email', $email);
$query = $this->db->get();
$row = $query->row();
return ($row->count > 0);
}
function check_contact_email_id($email)
{
$this->db->select('COUNT(login) AS count');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('user_id !=', $_POST['user_id']);
$query = $this->db->get();
$row = $query->row();
return ($row->count > 0);
}
function check_contact_email($email, $id = FALSE)
{
$this->db->select('COUNT(login) AS count');
$this->db->from('users');
$this->db->where('email', $email);
if ($id) $this->db->where('user_id !=', $_POST['user_id']);
$query = $this->db->get();
$row = $query->row();
return ($row->count > 0);
}
Run Code Online (Sandbox Code Playgroud) 这个假设的例子说明了我似乎无法通过的几个问题,即使我一直在努力!...假设原始代码是一个长事件处理程序,在UI中编码,当用户单击网格中的单元格时触发.表示为伪代码:
if Condition1=true then
begin
//loop through every cell in row,
//if aCell/headerCellValue>1 then
//color aCell red
end
else if Condition2=true then
begin
//do some other calculation adding cell and headerCell values, and
//if some other product>2 then
//color the whole row green
end
else show an error message
Run Code Online (Sandbox Code Playgroud)
我看着这个并说"啊,重构策略模式!代码将更容易理解,更容易调试,以后更容易扩展!" 我明白了.
我可以轻松地将代码分解为多个程序.
问题最终与范围有关.假设伪代码广泛使用网格属性,单元格中显示的值,甚至可能是内置网格方法.如何在不引用UI中的网格组件的情况下将所有这些移动到另一个单元 - 这会破坏松散耦合的所有"规则",使OOP有价值?...
我真的很期待回应.谢谢,一如既往 - Al C.
我目前正在使用本地应用程序,该应用程序可以访问SQL Server 2008 Express.我所做的是创建一个管理数据库上所有访问和操作的类(例如INSERT,DELETE等),但是这个类变得非常大,并且至少有40个方法.所以我的问题是:有没有办法重构这类课程?或者那样是正常的吗?
Delphi 2009在"搜索用法"对话框中显示一个带有"T"的小图标,而不是普通的IDE应用程序图标(见下图).这个符号是什么意思?
refactoring ×10
c# ×2
delphi ×2
c++ ×1
codeigniter ×1
delphi-2009 ×1
icons ×1
ide ×1
java ×1
javascript ×1
loops ×1
oop ×1
php ×1
social ×1