作为敏捷开发周期中的Java开发人员,我了解到必须确保以一种能够轻松地重构它们的方式设计我的类,而不会有太多痛苦.我想知道,您在日常设计/开发周期中遵循的最佳实践是什么,可以帮助您轻松执行重构.
例如,我知道我应该隐藏接口背后的实现细节.因此,如果我明天更改实现,那么我不会打扰使用此API的客户端代码.同样,我应尽可能使用"工厂设计模式",以便可以从一个工厂类控制实现类的更改,而不是找出所有位置并更改它们.
同样,我想知道你所遵循的所有最佳实践对我有什么帮助.
在工作中,我最近为从已发布的规范实现的类编写了一个小于运算符,该类具有许多属性,其中六个用于唯一标识类的实例.(为了这个问题,我们将这些属性称为af.)此外,这六个属性有六种不同的类型.我将运算符定义为:
bool operator<(const Class& lhs, const Class& rhs)
{
bool retval = (&lhs != &rhs);
if (retval == true)
{
if (lhs.a == rhs.a)
{
if (lhs.b == rhs.b)
{
if (lhs.c == rhs.c)
{
if (lhs.d == rhs.d)
{
if (lhs.e == rhs.e)
{
retval = (lhs.f < rhs.f);
} else {
retval = (lhs.e < rhs.e);
}
} else {
retval = (lhs.d < rhs.d);
}
} else {
retval = (lhs.c < rhs.c);
}
} else { …Run Code Online (Sandbox Code Playgroud) 这段代码一直困扰着我,部分是因为
if (result != OpResult.Success) { // return
Run Code Online (Sandbox Code Playgroud)
代码重复,到处都是.
1..n执行一系列评估.每次评价后,进行检查,以确保手术很成功(利用枚举派生的自定义返回值): OpResult.Success.
这是一个例子(带有示例对象等):
OpResult result = OpResult.Sucess;
result = performOperationOne(commonObjectArgument);
if (result != OpResult.Success)
{
trace.Exit(); // Exit logging mechanism
return result;
}
result = performOperationTwo(commonObjectArgument);
if (result != OpResult.Success)
{
trace.Exit();
return result;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,if (result != OpResult.Success)用作流控制,即除非所有先前的操作都成功,否则下一个操作不会运行.
使用.Net 4.*,C#在语法上已经具备了一些非常令人难以置信的功能.我可以采取哪些措施来消除每次操作后需要重新编写此评估的内容吗?
很抱歉这个问题的标题含糊不清,但我不确定如何确切地问这个问题.
以下代码在Arduino微处理器上执行时(为ATMega328微处理器编译的c ++)工作正常.返回值显示在代码中的注释中:
// Return the index of the first semicolon in a string
int detectSemicolon(const char* str) {
int i = 0;
Serial.print("i = ");
Serial.println(i); // prints "i = 0"
while (i <= strlen(str)) {
if (str[i] == ';') {
Serial.print("Found at i = ");
Serial.println(i); // prints "Found at i = 2"
return i;
}
i++;
}
Serial.println("Error"); // Does not execute
return -999;
}
void main() {
Serial.begin(250000);
Serial.println(detectSemicolon("TE;ST")); // Prints "2"
}
Run Code Online (Sandbox Code Playgroud)
如预期的那样,它输出"2"作为第一个分号的位置.
但是,如果我将detectSemicolon …
关于以下代码是否更好,我看到了相互矛盾的建议
def function():
ret_val = 0
if some_condition():
ret_val = 2
else:
ret_val = 3
return ret_val
Run Code Online (Sandbox Code Playgroud)
或者这是否更好:
def function():
if some_condition():
return 2
else:
return 3
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子,我用python风格编写它,但我正在寻找的一般原则是什么时候使用一些"累加器"变量来跟踪返回值,或者是否使用多个出口点.我知道不同的语言可能有不同的原因使用一种风格而不是另一种风格,所以我很欣赏不同的观点,为什么特定的语言可能会坚持特定的风格.(特别是在过去,我听说C中的结构化编程避免了函数的多个退出点.)
我正在编写一个简单的WinForms应用程序,我允许用户在TreeView控件中拖动TreeNodes.我强制执行的规则之一是不允许用户将TreeNode拖动到其自己的子节点之一.我以递归样式编写了以下函数来检查目标节点的父级.在编译时,我得到错误,并非所有代码路径都返回此函数的值.据我所知,我对这个逻辑的每个可能的分支都有一个回复声明......但我显然是错的.请有人指出我的错误.
private bool IsDestinationNodeAChildOfDraggingNode(TreeNode draggingNode, TreeNode destinationNode) {
if (draggingNode.Nodes.Count == 0)
return false;
else {
if (draggingNode.Nodes.Contains(destinationNode))
return true;
else {
foreach (TreeNode node in draggingNode.Nodes)
return IsDestinationNodeAChildOfDraggingNode(node, destinationNode);
}
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的例子中使用一个if与一个直接相结合的return可接受的做法,而不是在if里面有一块代码{}?这些在实践中是等同的还是其中一种方法的缺点?
Java中的一个例子:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext sc = this.getServletContext();
// Throw exception for fatal error (Servlet not defined in web.xml ?)
if( sc == null )
return; // old-style programming
// Careful with silent bugs ! Correct way of handling this is:
// throw new RuntimeException( "BookDetail: ServletContext is null" );
BookList bookList = WebUtil.getBookList( sc );
Run Code Online (Sandbox Code Playgroud) 可能重复:
函数是否只有一个return语句?
你好,
gcc 4.4.4 c89
从函数中的1点返回是一种很好的编程习惯吗?
我在下面写了一个函数.但是,我从2个可能的点返回.
这是好风格吗?
static int init_data(struct timeout_data_t *timeout_data)
{
if(timeout_data == NULL) {
fprintf(stderr, " [ %s ] [ %d ]\n",
__func__, __LINE__);
return FALSE;
}
/* Assign data */
timeout_data->seconds = 3;
timeout_data->func_ptr = timeout_cb;
return TRUE;
}
Run Code Online (Sandbox Code Playgroud) 这两种模式产生相同的结果.使用哪一个是否重要?为什么?
我更喜欢第二种,它有较少的缩进,只是看起来更清洁,但我没有看到它用得太多(在我去过的地方).如果由于某种原因不明智,我不想满足于某些事情并全部使用它.
如果别的
if not packages:
help('download')
else:
for p in packages:
do_download(p)
verify_md5(p)
etc(p)
Run Code Online (Sandbox Code Playgroud)
IF ...回报; 隐含的
if not packages:
help('download')
return
for p in packages:
do_download(p)
verify_md5(p)
etc(p)
Run Code Online (Sandbox Code Playgroud) 我是一个使用python的初学程序员,我想知道函数返回的最佳实践是什么.我有很多条件,但只要其中一个是真的,我想杀死该函数并返回一个bool值.说,我们有:
a)
def foo():
if condition1:
return True
if condition2:
return True
if condition3:
return True
return False
b)
def foo():
bar = False
if condition1:
bar = True
elif condition2:
bar = True
elif condition3:
bar = True
return bar
Run Code Online (Sandbox Code Playgroud)
有一种方式比另一种更好吗?为什么?或者这是一些完全垃圾,应该以完全不同的方式实现?在其他语言中它是否与python不同(或者,是否有"pythonic"方式)?提前感谢大家的答案.BTW,是否有最佳实践的标签,或类似的东西?
python ×3
c# ×2
c++ ×2
coding-style ×2
.net ×1
arduino ×1
c ×1
compilation ×1
conditional ×1
embedded ×1
flow-control ×1
indentation ×1
refactoring ×1
resources ×1
return-value ×1