混乱,如果声明? [英] Confusing If Statement?

查看:120
本文介绍了混乱,如果声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是使用if语句(在C#),如(1替代);

I always use If statement (In C#) as (1. Alternative);

if (IsSuccessed == true)
{
   //
}

我知道有没有必要写==真为(2替代));

I know that there is no need to write "== true" as (2. Alternative));

if (IsSuccessed)
{
   //
}

不过,我用它,因为它更具有可读性而导致没有性能问题。当然,这是我的选择,我知道许多软件开发者更喜欢第一选择。什么是最好的使用,为什么?

But, I use it because it is more readable and cause no performance issue. Of course, this is my choice and I know many software developers prefer first alternative. What is the best usage, and Why?

推荐答案

如果布尔值的名称非常明确它是什么,然后我总是选择第2版。不过,有时你坚持,你不能改变,至少现在不能改变一个特别钝变量名......重构是一个好主意,但我。尽量避免功能变化时的代码,以及重构过重

If the name of the boolean value makes it perfectly clear what it is, then I'd always opt for version 2. However, sometimes you're stuck with a particularly obtuse variable name that you can't change, at least, can't change right now... Refactoring is all well and good, but I try and avoid refactoring too heavily when making functional changes to the code as well.

例如:

if (!NoDropDownInHeader == true)
{
  // Activates when there *is* a dropdown in the header)
}

其实我已经看到在生产代码这个特殊的例子并简化了它归结为:

I've actually seen this particular example in production code and simplified it down to:

if (NoDropDownInHeader == false)
{
 // Activates when there *is* a dropdown in the header
}

和我个人认为,这两个例子都更具可读性(虽然可以说是第一个例子可能是堪与这一个精神分析的难度)比:

And I personally think that both examples are more readable (although arguably the first example may be on par with this one for difficulty of mental parsing) than:

if (!NoDropDownInHeader)
{
 // Activates when there *is* a dropdown in the header
}

注意:是的,我知道变量是严重命名,但改变它的,它存在的地方会众我正在变化的范围之外,由于名额是否会影响

这篇关于混乱,如果声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆