简体   繁体   English

这种bool方法是一种不好的做法吗?

[英]Is this kind of a bool method a bad practice?

Sometimes I find myself writing a bool method that looks like this: 有时我发现自己写的bool方法看起来像这样:

public bool isRunning()
    {
        if (!(move == Moving.None) && staminaRegan == true)
        {
            if (keyState.IsKeyDown(Keys.Space))
            {
                EntityAnimation.interval = 10;
                return true;
            }
            else
            {
                EntityAnimation.interval = 65;
                return false;
            }
        }
        else 
        {
            EntityAnimation.interval = 65;
            return false;
        }
    }

(This is XNA by the way) As you can see, I have a bool isRunning in which I made an if statement where Im checking if (Player is moving) && (regains stamina, which is set to false once stamina reaches value lesser than 6.0f) and then I simply check if Space is pressed, if yes then my Animation is faster(the smaller the interval, the faster is spritesheet changing), and then It sends true value, which means that Player is running, else Im not cause Space is not pressed. (顺便说一下,这是XNA)正如你所看到的,我有一个bool isRunning,其中我做了一个if语句,我在这里检查是否(玩家正在移动)&&(恢复耐力,一旦耐力达到小于某个值,则设置为假6.0f)然后我只是检查Space是否被按下,如果是,则我的动画更快(间隔越小,spritesheet更改越快),然后它发送true值,这意味着Player正在运行,否则我不是原因没有按空格。

And then I have to repeat this 'else' code outside of the first if statement so it sends that Player is not running if Player is not moving or his stamina Regan is false; 然后我必须在第一个if语句之外重复这个'else'代码,这样如果Player没有移动或者他的耐力Regan是假的,它会发送Player没有运行;

So I was just wondering is this kind of a bool method considered a bad practice(where you retrun true and false value in nested if, and then return false outside nested if and repeat the same code) ? 所以我只是想知道这种bool方法被认为是一种不好的做法(你在嵌套if中重新获得true和false值,然后在嵌套if之后返回false并重复相同的代码)?

The method has a side effect , that's why it's a bad practice : 该方法有副作用 ,这就是为什么这是一个不好的做法

 public bool isRunning()

When looking on method's signature we expect just true / false answer and nothing more. 在查看方法的签名时,我们期待只有true / false答案,仅此而已。 However, the method changes the instance's state : 但是,该方法更改实例的状态

  ...
  if (!(move == Moving.None) && staminaRegan == true)
    {
        if (keyState.IsKeyDown(Keys.Space))
        {
            EntityAnimation.interval = 10; // <- Aaa! The interval is changed 
            return true;
        }
  ...

I suggest splitting the initial method into a property and a method 我建议将初始方法拆分为属性和方法

 // No side effect: just answer is running or not
 public bool IsRunning {
   get {
     return (move != Moving.None) && staminaRegan && KeyState.IsKeyDown(Keys.Space);
   }
 }

 // Put the right interval based on instance internal state 
 // (if it's running etc.)
 public void AdjustInterval() {
   if (IsRunning) // and may be other conditions
     EntityAnimation.interval = 10; //TODO: move magic number into constant 
   else 
     EntityAnimation.interval = 65; //TODO: move magic number into constant
 } 

It is a good practice to have one return statement inside a method. 在方法中包含一个return语句是一个好习惯。 Some argue about this, but it is an opinion. 一些人争论这个,但这是一个意见。

it is also a good practice to make the if statement clear by removing unnecessary code: 通过删除不必要的代码来清除if语句也是一种很好的做法:

public bool isRunning()
{
    bool result = false;
    if (move != Moving.None && staminaRegan)
    {
        if (keyState.IsKeyDown(Keys.Space))
        {
            EntityAnimation.interval = 10;
            result = true;
        }
        else
        {
            EntityAnimation.interval = 65;
        }
    }
    else
    {
        EntityAnimation.interval = 65;
    }

    return result;
}

You can rewrite the code as follows; 您可以按如下方式重写代码; then the code isn't repeated: 那么代码不会重复:

public bool isRunning()
{
    if (move != Moving.None && staminaRegan && keyState.IsKeyDown(Keys.Space))
    {
        EntityAnimation.interval = 10;
        return true;
    }
    else
    {
        EntityAnimation.interval = 65;
        return false;
    }
}

Or if you don't want the redundant else : 或者如果你不想要多余的else

public bool isRunning()
{
    if (move != Moving.None && staminaRegan && keyState.IsKeyDown(Keys.Space))
    {
        EntityAnimation.interval = 10;
        return true;
    }

    EntityAnimation.interval = 65;
    return false;
}

I would consider introducing a named boolean to self-document somewhat, and I'd rename staminaRegan to staminaIsRegenerating 我会考虑在某种程度上引入一个名为boolean的自我文档,并且我将staminaRegan重命名为staminaIsRegenerating

public bool isRunning()
{
    bool isMovingQuickly = (move != Moving.None) && staminaIsRegenerating && keyState.IsKeyDown(Keys.Space);

    if (isMovingQuickly)
        EntityAnimation.interval = 10;
    else
        EntityAnimation.interval = 65;

    return isMovingQuickly;
}

Most importantly, though, you should rename the method to more accurately describe what it's doing: 但最重要的是,您应该重命名该方法以更准确地描述它正在做的事情:

public bool CheckIfRunningAndSetAnimationInterval()

I think we write code for people(other developers), of course machine execute a code but 80% of developer's work is reading the code. 我认为我们为人(其他开发人员)编写代码,当然机器执行代码,但80%的开发人员的工作是阅读代码。
Based on that I think flow of reading must be exactly same as flow of executing code - that's why I think multiply return statement not a bad thing, even better then only one return statement on the bottom of your method. 基于此,我认为读取流程必须与执行代码流程完全相同 - 这就是为什么我认为乘法return语句不是坏事,甚至比方法底部只有一个return语句更好。

I like this style and i use it too. 我喜欢这种风格,我也喜欢它。 First, you can read the code more easily and second it has a debugging advantage as you can set breakpoints for the individual else cases. 首先,您可以更轻松地阅读代码,其次它具有调试优势,因为您可以为其他个案设置断点。 Otherwise you would need to use breakpoint conditions. 否则,您将需要使用断点条件。

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

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