len(x) 更好或 x NEQ “” CFML 更好吗?

发布于 2024-11-27 00:09:05 字数 147 浏览 1 评论 0原文

<cfif len(x)>

或者

<cfif x NEQ "">

哪一个更高效、更具可读性?函数调用与比较空字符串?

This

<cfif len(x)>

Or

<cfif x NEQ "">

Which one is more efficient and readable? Function call vs comparing empty string?

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

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

发布评论

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

评论(5

も让我眼熟你 2024-12-04 00:09:05

我同意 Scott 的观点:

<cfif len(trim(x))>

当您将数据库元素分配给具有“Null”值或单个空格的变量时,它将仅通过长度测试,修剪可以修复该问题。

我只在我自己的代码中使用它并且我确信我的变量已被声明时才使用它。然而,我经常在国外解决另一个程序员的问题,所以我的测试看起来像:

<cfif isdefined('x') AND len(trim(x)) gt 0>

至于可读性,它不是最漂亮的,但它是最彻底的。

I'm with Scott on this one:

<cfif len(trim(x))>

When you assign a database element to a variable, that has a "Null" value, or a single space, it will pass just the length test, the trim fixes that.

I only use just that if I'm in my own code and I'm positive that my variables have been declared. Often I'm abroad in another programmers problem however, so my test would look like:

<cfif isdefined('x') AND len(trim(x)) gt 0>

As for readability, it's not the prettiest, but it's the most thorough.

葵雨 2024-12-04 00:09:05

我使用:

 if(len("String"));

我认为代码更简洁,正如他们喜欢说的“富有表现力”。使用这种语法,您的代码所说的是“如果该字符串有长度”,则执行此操作,而相反,您的代码是“如果字符串的长度是空白字符串,则执行此操作”。

我还使用 ColdFusions 松散布尔值来处理很多事情,例如:

  if(a + b){
   ...
   }

I use:

 if(len("String"));

I think the code is much cleaner and as they like to say "expressive". With this syntax what you are saying with the code is "if this string has a length" do this where with the opposite your code is "saying if the length of the string is a blank string do this".

I also use ColdFusions loose boolean values for lots of things like in addition:

  if(a + b){
   ...
   }
自此以后,行同陌路 2024-12-04 00:09:05

就我个人而言,我通常使用:

len( trim( x ) )

Personally, I typically use:

len( trim( x ) )
好多鱼好多余 2024-12-04 00:09:05

瑞安很有钱,理所当然地得到了公认的答案。

在这种情况下,我的考虑是,积极的表达方式比消极的表达方式更容易让人大脑思考,因此就清晰度而言:使用 len() 比消极比较更好到一个空字符串。

另外,从实用的角度来看,您很可能想知道是否有一个具有长度的字符串,而不是该字符串恰好不是一个空字符串(如果您看到轻微的语义差异),因此 len() 方法将更贴近您的实际需求。

至于执行 trim():除非它来自用户输入并且删除空白填充很重要,否则我不会这样做。我坚信“垃圾进,垃圾出”。这也是对数据意图的事后猜测,我非常不喜欢那些不完全按照所告诉的去做的代码,不多也不少。

这里绝对没有与性能相关的现实世界的考虑因素,所以不要担心这类事情,而是关注什么使最可读的代码可以完成手头的工作。

Ryan's on the money and rightfully has the accepted answer.

The consideration I have in situations like this is that positively-phrased expressions are slightly easier to get one's brain around than negative ones, so as far as clarity goes: using len() is better than a negative comparison to an empty string.

Also from a pragmatic point of you, you're most probably wanting to know if you have a string with length, not that the string happens to not be an empty string (if you see the slight semantic difference), so the len() approach will more closely match your actual requirements.

As for doing a trim(): unless it's coming from user input and it's important that whitespace padding is removed, I would not do this. I am a firm believer in "garbage in, garbage out". It's also second-guessing the intent of the data, and I deeply dislike code that doesn't simply do precisely what it's told, no more, and no less.

There are absolutely no real-world considerations relating to performance here, so don't worry about that sort of thing, instead focus on what makes the most readable code that does the job at hand.

尬尬 2024-12-04 00:09:05

据我所知, len(x) 或者更确切地说 len(trim(x)) 比 x neq "" 更快

From what I remember, len(x) or rather len(trim(x)) is faster than x neq ""

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