在IF条件内调用方法是否被认为是可读的?
本文关键字:认为是 是否 条件 IF 调用 方法 | 更新日期: 2023-09-27 18:09:32
这种编写IF条件的方式在Java和c#语言中是否被认为是良好的编码风格?
if (checkIfIdInFirstRange()){
//call Range1 handling method
}else if(checkIfIdInSecondRange()){
//call Range2 handling method
}else{
//call error handling method
}
我想知道IF条件本身内部的方法,或者它会更好,使它像:
int idInRange = getIdInRange();
//handle isInRange
我认为这很好。
如果你把你的方法表述成一个问题,或者用if语句来表达就更好了
if (thisConditionIsTrue()){
// Do this
}elseif(anotherConditionIsTrue()){
// Do this instead
}elseif(isThisParameterOkay(someParameter)){
// Yeh do this
}
一些核心的纯粹主义者甚至会说,如果你有> 3层缩进,你的方法嵌套太深,应该分成更小的方法。
这样做是很好的编码实践只要方法调用没有任何副作用。
。
如果checkIfIdInFirstRange()
这是OK的:
private bool checkIfIdInFirstRange()
{
return firstRange.Contains(ID);
}
但是这样做可能会造成混淆:
private bool checkIfIdInFirstRange()
{
SomeStringProperty = "totally new value that no caller would ever expect after a call to this method";
return firstRange.Contains(ID);
}
另一个可能的解决方案——取决于手头问题的实际类型——可能是定义一个接口/基类并使用多态性。
例子:
internal abstract class A
{
public void DoSomething(int ID)
{
if(IsInRange(ID))
DoSomethingProtected(ID);
}
protected abstract bool IsInRange(int ID);
protected abstract void DoSomethingProtected(int ID);
}
internal class B : A
{
private List<int> firstRange = new List<int> { 42, 23, 5};
protected override bool IsInRange(int ID)
{
return firstRange.Contains(ID);
}
protected override void DoSomethingProtected(int ID)
{
Console.WriteLine("{0}", ID);
}
}
public class Program
{
public static void Main(string[] args)
{
B foo = new B();
foo.DoSomething(3);
foo.DoSomething(42);
}
}
注意:编写没有IDE的代码。
是。如果您只使用一点空白,那么它将更具可读性。像那样把它堆在一起使得很难分辨事物的开始和结束位置,并且使else if()
看起来像一个函数调用。
if ( checkIfIdInFirstRange() ) {
//call Range1 handling method
}
else if ( checkIfIdInSecondRange() ) {
//call Range2 handling method
}
else {
//call error handling method
}
添加额外的变量可能会使代码更难阅读,因为你必须在if/else堆栈之前定义它们。然而,这完全取决于具体情况。有时,如果您将多次使用一个昂贵的函数,或者如果您可以使变量具有比函数更具描述性的名称,则使用变量可能会更好。
实际上,如果您想测试多个方法并使用短路评估,也需要使用
例如,这是安全的:
if (isResourceAvailable() && isResourceValid()) {
...
}
虽然这可能不是:
bool resAvailable = isResourceAvailable();
bool resValid = isResourceValid(); // can you call that alone?
if (resAvailable && resValid ) {
...
}
只要你调用的方法不只是做一些如果适当编码就会更清楚的事情,这就是好的风格:
if ( a > 0 && a < 10 ) doSomething();
优于
if ( isInRange(a, 0, 10) ) doSomething();
嗯,这主要取决于编码器,但是声明int更容易读。
可以在IF条件语句中编写方法。但是,如果该方法将被多次使用,则应该首先使用一个局部变量来存储返回值,并将该变量用作if条件
您可以将目标放在编写函数/方法名上,使代码在使用它们的地方更具可读性。如:
if (idInFirstRange()){
//call Range1 handling method
}else if(idInSecondRange()){
//call Range2 handling method
}else{
而且,通常返回bool值的函数的约定是以is
- isIdInFirstRange
最后,尽量避免这种if-else(和switch)阶梯。在这种情况下尽量使用字典。(https://stackoverflow.com/questions/6506340/if-if-else-or-switch-case/6506403#6506403)
虽然这种做法没有错,也没有受到任何最佳实践指南的谴责,但如果在if 条件中有多个调用,那么在调试会话期间,很难知道哪个调用拒绝进入if 语句
。if (idInFirstRange() && idInSecondRange()){
//call Range handling method
//if didn't reach this point, who was the responsible (idInFirstRange or idInSecondRange)?
}else if(idInSecondRange()){
//call Range2 handling method
}else{
//call something else
}