我不喜欢这个……这是在欺骗语言吗?

发布于 2024-07-19 07:41:58 字数 721 浏览 14 评论 0 原文

我已经见过几次类似下面的东西了……我讨厌它。 这基本上是在“欺骗”语言吗? 或者..您是否认为这是“可以”,因为 IsNullOrEmpty 始终首先被评估?

(我们可能会争论一个字符串从函数中出来时是否应该为 NULL,但这并不是真正的问题。)

string someString;
someString = MagicFunction();

if (!string.IsNullOrEmpty(someString) && someString.Length > 3)
{
    // normal string, do whatever
}
else
{
   // On a NULL string, it drops to here, because first evaluation of IsNullOrEmpty fails
   // However, the Length function, if used by itself, would throw an exception.
}

编辑: 再次感谢大家提醒我这门语言基础知识。 虽然我知道它“为什么”有效,但我不敢相信我不知道/记得这个概念的名称。

(如果有人想要任何背景..我在对代码的不同位置由 NULL 字符串和 .Length > x 异常生成的异常进行故障排除时遇到了这一点。所以当我看到上面的代码时,除了其他所有内容之外,我的挫败感从那里开始。)

I have seen something like the following a couple times... and I hate it. Is this basically 'cheating' the language? Or.. would you consider this to be 'ok' because the IsNullOrEmpty is evaluated first, all the time?

(We could argue whether or not a string should be NULL when it comes out of a function, but that isn't really the question.)

string someString;
someString = MagicFunction();

if (!string.IsNullOrEmpty(someString) && someString.Length > 3)
{
    // normal string, do whatever
}
else
{
   // On a NULL string, it drops to here, because first evaluation of IsNullOrEmpty fails
   // However, the Length function, if used by itself, would throw an exception.
}

EDIT:
Thanks again to everyone for reminding me of this language fundamental. While I knew "why" it worked, I can't believe I didn't know/remember the name of the concept.

(In case anyone wants any background.. I came upon this while troubleshooting exceptions generated by NULL strings and .Length > x exceptions... in different places of the code. So when I saw the above code, in addition to everything else, my frustration took over from there.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

帅哥哥的热头脑 2024-07-26 07:41:58

您正在利用称为短路的语言功能。 这并不是欺骗语言,而是实际上完全按照其设计用途使用了某个功能。

You're taking advantage of a language feature known as short circuiting. This is not cheating the language but in fact using a feature exactly how it was designed to be used.

怀中猫帐中妖 2024-07-26 07:41:58

如果您问是否可以依赖“短路”关系运算符 &&||,那么是的,完全没问题。

If you are asking if its ok to depend on the "short circuit" relational operators && and ||, then yes thats totally fine.

雄赳赳气昂昂 2024-07-26 07:41:58

这没有什么问题,因为您只是想确保不会出现空指针异常。

我认为这样做是合理的。

通过扩展,您可以使其更清晰,但基本概念仍然有效。

There is nothing wrong with this, as you just want to make certain you won't get a nullpointer exception.

I think it is reasonable to do.

With Extensions you can make it cleaner, but the basic concept would still be valid.

国粹 2024-07-26 07:41:58

这段代码完全有效,但我喜欢使用 Null Coalesce Operator 来避免 null 类型检查。

string someString = MagicFunction() ?? string.Empty;
if (someString.Length > 3)
{
    // normal string, do whatever
}
else
{
   // NULL strings will be converted to Length = 0 and will end up here.
}

This code is totally valid, but I like to use the Null Coalesce Operator for avoid null type checks.

string someString = MagicFunction() ?? string.Empty;
if (someString.Length > 3)
{
    // normal string, do whatever
}
else
{
   // NULL strings will be converted to Length = 0 and will end up here.
}
人心善变 2024-07-26 07:41:58

这没有什么问题。

if(条件是从左到右评估的,所以像这样堆叠它们是完全可以的。

Theres nothing wrong with this.

if(conditions are evaluated from left to right so it's perfectly fine to stack them like this.

小伙你站住 2024-07-26 07:41:58

在我看来,这是有效的代码(尽管声明一个变量并在下一行分配它非常烦人),但您可能应该意识到,您也可以在字符串长度为 < 的情况下输入 else 块; 3.

This is valid code, in my opinion (although declaring a variable and assigning it on the next line is pretty annoying), but you should probably realize that you can enter the else-block also in the condition where the length of the string is < 3.

萝莉病 2024-07-26 07:41:58

在我看来,这就像是对逻辑短路的完全合理的使用——如果有的话,那就是对语言的欺骗。 我最近才使用 VB6,它从来没有短路过,这确实让很恼火。

需要注意的一个问题是,您可能需要在 else 子句中再次测试 Null,因为正如所写的那样,您最终会得到 Null 字符串和长度小于 3 的字符串。

That looks to me like a perfectly reasonable use of logical short-circuitting--if anything, it's cheating with the language. I've only recently come from VB6 which didn't ever short-circuit, and that really annoyed me.

One problem to watch out for is that you might need to test for Null again in that else clause, since--as written--you're winding up there with both Null strings and length-less-than-three strings.

江心雾 2024-07-26 07:41:58

这是完全有效的,并且以这种方式使用它没有任何问题。 如果您遵循该语言记录的行为,那么一切都很好。 在 C# 中,您使用的语法是条件逻辑运算符,可以在 MSDN

对我来说,这与在同一语句中进行乘法和加法时不使用括号相同,因为语言记录了将首先执行乘法运算。

This is perfectly valid and there is nothing wrong with using it that way. If you are following documented behaviour for the language than all is well. In C# the syntax you are using are the conditional logic operators and thier docemented bahviour can be found on MSDN

For me it's the same as when you do not use parenthesis for when doing multiplication and addition in the same statement because the language documents that the multiplication operations will get carried out first.

善良天后 2024-07-26 07:41:58

在大多数情况下,依靠短路是“正确的做法”。 它可以生成简洁的代码,并减少移动部件。 这通常意味着更容易维护。 在 C 和 C++ 中尤其如此。

我会认真重新考虑雇用不熟悉(并且不知道如何使用)短路操作的人。

Relying on short-circuiting is the "right thing" to do in most cases. It leads to terser code with fewer moving parts. Which generally means easier to maintain. This is especially true in C and C++.

I would seriously reconsider hiring someone who is not familiar with (and does not know how to use) short-circuiting operations.

雾里花 2024-07-26 07:41:58

我觉得没问题:) 你只是确保不访问 NULL 变量。
实际上,我总是在对变量进行任何操作之前进行此类检查(此外,在索引集合等时) - 它更安全,是最佳实践,仅此而已。

I find it OK :) You're just making sure that you don't access a NULL variable.
Actually, I always do such checking before doing any operation on my variable (also, when indexing collections and so) - it's safer, a best practice, that's all ..

裸钻 2024-07-26 07:41:58

这是有道理的,因为 C# 默认情况下会短路条件,所以我认为利用它来发挥你的优势是很好的。 在 VB 中,如果开发人员使用 AND 而不是 ANDALSO,可能会出现一些问题。

It makes sense because C# by default short circuits the conditions, so I think it's fine to use that to your advantage. In VB there may be some issues if the developer uses AND instead of ANDALSO.

じее 2024-07-26 07:41:58

我认为这与这样的事情没有什么不同:

INT* pNumber = GetAddressOfNumber();

if ((pNUmber != NULL) && (*pNumber > 0))
{
  // valid number, do whatever
}
else
{
  // On a null pointer, it drops to here, because (pNumber != NULL) fails
  // However, (*pNumber > 0), if used by itself, would throw and exception when dereferencing NULL
}

它只是利用了语言中的一个功能。 我认为,自从 C 开始以这种方式执行布尔表达式(或任何首先执行此操作的语言)以来,这种习惯用法已经很常用了。)

I don't think it's any different than something like this:

INT* pNumber = GetAddressOfNumber();

if ((pNUmber != NULL) && (*pNumber > 0))
{
  // valid number, do whatever
}
else
{
  // On a null pointer, it drops to here, because (pNumber != NULL) fails
  // However, (*pNumber > 0), if used by itself, would throw and exception when dereferencing NULL
}

It's just taking advantage of a feature in the language. This kind of idiom has been in common use, I think, since C started executing Boolean expressions in this manner (or whatever language did it first).)

又怨 2024-07-26 07:41:58

如果您将 c 代码编译成汇编语言,那么短路不仅是正确的行为,而且更快。 在机器语言中,if 语句的各部分被依次求值。 不短路速度较慢。

If it were code in c that you compiled into assembly, not only is short-circuiting the right behavior, it's faster. In machine langauge the parts of the if statement are evaluated one after another. Not short-circuiting is slower.

岁月静好 2024-07-26 07:41:58

编写代码对公司来说要花费很多美元。 但维护成本更高!

所以,我同意你的观点:这行代码很可能不会被那些必须在两年内阅读并纠正它的人立即理解。

当然,他会被要求纠正一个关键的生产错误。 他会到处寻找,但可能没有注意到这一点。

我们应该总是为下一个人编码,而他可能不如我们聪明。 对我来说,这是唯一值得记住的事情。

这意味着我们使用明显的语言特征并避免其他特征。

祝一切顺利,西尔万。

Writing code cost a lot of $ to a company. But maintaining it cost more !

So, I'm OK with your point : chance are that this line of code will not be understood immediatly by the guy who will have to read it and correct it in 2 years.

Of course, he will be asked to correct a critical production bug. He will search here and there and may not notice this.

We should always code for the next guy and he may be less clever that we are. To me, this is the only thing to remember.

And this implies that we use evident language features and avoid the others.

All the best, Sylvain.

故乡的云 2024-07-26 07:41:58

有点偏离主题,但如果你像这样在 vb.net 中运行相同的示例,

dim someString as string
someString = MagicFunction()
if not string.IsNullOrEmpty(someString) and someString.Length > 3 then
    ' normal string, do whatever
else
    ' do someting else
end if

这会导致空(无)字符串,但在 VB.Net 中,你可以按如下方式对其进行编码,请在 C# 中执行相同的操作,

dim someString as string
someString = MagicFunction()
if not string.IsNullOrEmpty(someString) andalso someString.Length > 3 then
    ' normal string, do whatever
else
    ' do someting else
end if

添加并使其表现良好同样的方式,读起来也更好。 作为同时进行 vb 和 c' 开发的人,第二个 vb 显示登录略有不同,因此更容易向某人解释存在差异等。

Drux

A bit off topic but if you rand the same example in vb.net like this

dim someString as string
someString = MagicFunction()
if not string.IsNullOrEmpty(someString) and someString.Length > 3 then
    ' normal string, do whatever
else
    ' do someting else
end if

this would go bang on a null (nothing) string but in VB.Net you code it as follows do do the same in C#

dim someString as string
someString = MagicFunction()
if not string.IsNullOrEmpty(someString) andalso someString.Length > 3 then
    ' normal string, do whatever
else
    ' do someting else
end if

adding the andalso make it behave the same way, also it reads better. as someone who does both vb and c' development the second vb one show that the login is slighty different and therefor easyer to explain to someone that there is a differeance etc.

Drux

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文