繁体   English   中英

在 IF 条件下调用方法是否被认为是可读的? [关闭]

[英]Is it considered readable to call methods inside the IF condition? [closed]

这种编写 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,则您的方法过于嵌套,应拆分为较小的方法。

只要方法调用没有任何副作用,这样做就是IMHO的良好编码习惯。

例如

如果checkIfIdInFirstRange()以下操作:

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();

尽管这种做法并没有错,也没有受到任何最佳实践指南的谴责,但如果您在if条件中有多个调用,则很难知道在调试期间哪个调用拒绝进入if语句session。

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
}

嗯,这主要取决于编码器,但声明int更具可读性。

可以在IF条件语句中编写方法。 但是,如果该方法将被多次使用,则应首先使用局部变量存储返回值,然后将该变量用作IF条件。

您可以着眼于编写函数/方法名称,以使代码在使用位置时更具可读性。 喜欢:

if (idInFirstRange()){
    //call Range1 handling method
}else if(idInSecondRange()){
    //call Range2 handling method
}else{

另外,返回布尔值的函数的常规约定是它们以is isIdInFirstRange

最后,尝试避免这种if-else(和switch)阶梯。 在这种情况下,请尝试使用字典。 https://stackoverflow.com/questions/6506340/if-if-else-or-switch-case/6506403#6506403

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM