下面的两个代码块在逻辑上是等价的吗?

发布于 2024-10-08 09:50:59 字数 497 浏览 0 评论 0原文

以下两个块之间有逻辑差异吗?有一种形式比另一种更正确吗?它们都将驻留在自己的函数中——我在这里省略了这一点。

    <cfset local.result = 1 />
    <cfset local.i = 1 />
    <cfloop from="1" to="5" index="i">
        <cfset result = result * i />
    </cfloop>

    <cfset local.result = 1 />
    <cfset local.i = 1 />
    <cfloop from="1" to="5" index="i">
        <cfset local.result = local.result * local.i />
    </cfloop>

Is there a logical difference between the following two blocks? And is there one form more correct than the other? They would both reside in their own function--something I omitted here.

    <cfset local.result = 1 />
    <cfset local.i = 1 />
    <cfloop from="1" to="5" index="i">
        <cfset result = result * i />
    </cfloop>

And

    <cfset local.result = 1 />
    <cfset local.i = 1 />
    <cfloop from="1" to="5" index="i">
        <cfset local.result = local.result * local.i />
    </cfloop>

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

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

发布评论

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

评论(2

忆离笙 2024-10-15 09:51:00

是的。在你的第二个例子中,你得到了完全相同的结果;但是,通过明确标识要修改的范围,您提高了可读性——这是一件好事。

ColdFusion,会首先搜索LOCAL范围,所以,你并没有节省ColdFusion太多的处理;不过,现在代码更清晰了。如果结果存在于 CLIENT 或 COOKIE 范围中,您将节省 ColdFusion 必须首先评估四个或五个其他范围的工作。

我曾经使用过“var result = 0;”将变量本地化到函数的风格,但是,我现在明确标识了所有范围,以帮助确保我正确地确定了所有变量的范围,并使代码更容易被其他人理解。

总而言之,代码对于机器来说是完全相同的,但现在对于人类来说更容易理解。

Yes. In your second example you are making the exact same result; however, you have improved readability by explicitly identifying the scope you intend to modify--which is a good thing.

ColdFusion, will first search the LOCAL scope, so, you have not saved ColdFusion much processing; however, the code is cleaner now. If result existed in the CLIENT or COOKIE scope you would have saved ColdFusion the work of having to first evaluate four or five other scopes.

I once used to use the 'var result = 0;' style of localizing variables to a function, but, I now explicitly identify all my scopes to help ensure I have correctly scoped all variables and make the code easier to understand for others.

To summarize, the code is exactly the same to the machine but is now easier to understand for the human.

渔村楼浪 2024-10-15 09:51:00

一个建议...更改:

<cfset local.i = 1 />
<cfloop from="1" to="5" index="i">

少一行

<cfloop from="1" to="5" index="local.i">

代码,甚至更清楚发生了什么。

One suggestion... change:

<cfset local.i = 1 />
<cfloop from="1" to="5" index="i">

to

<cfloop from="1" to="5" index="local.i">

one less line of code, even more clear what's going on.

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