在 CF9 中编写 cfc 的编码约定?

发布于 2024-08-02 17:43:25 字数 954 浏览 9 评论 0原文

有了 CF9 中编写 CFC 的新方法,CF9 有哪些新的编码约定?

以下是我能想到的一些...

With the new ways of writing CFC in CF9, what are some of the coding convention new to CF9?

Here are some I can think of...

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

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

发布评论

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

评论(3

猫弦 2024-08-09 17:43:25

在脚本风格的CFC中,我们还需要为组件和函数设置属性output=false吗?

我不这么认为。 本质上会抑制任何空格,并且需要 writeOutput() 才能获得任何输出。

do we still need to set attribute output=false for component and functions in script style CFC?

I wouldn't think so. <cfscript> by its nature suppresses any whitespace and needs writeOutput() in order to have any output at all.

不醒的梦 2024-08-09 17:43:25

如果您使用“new my.cfc()”语法调用 init() 方法,则不必返回“this”作用域。真实的故事。

如果您在 cfc 中并且想要设置属性,请不要使用 this.setFoo(),只需使用 setFoo() 即可。
getFoo() 也是如此。
this.xxx() 就像走出前门只是为了回来。此外,您的 access=private 自定义 getters 和 setters 不会工作,因为函数不会在 this 范围内。

“var foo”与“local.foo” - 就我个人而言,我更喜欢 var 变量,因为 a) 需要输入的代码更少,b) 需要阅读的代码更少。

// there isnt a huge difference here
var today = now();
var tomorrow = dateAdd( 'd', 1, today );
local.today = now();
local.tomorrow = dateAdd( 'd', 1, local.today );

// but when you start getting more complex examples, it quickly blows out
var out = method( var1, var2, var3, var4, var5 );
local.out = method( local.var1, local.var2, local.var3, local.var4, local.var5 );

使用 javadocs 风格的注释。文档是你的朋友。

/**
* @hint This is a hint for the whole function
* @arg1 This is an argument hint
* @arg2 This is another argument hint
**/
public void function myFunction( string arg1 = 'default', boolean arg2 ) {
    return true;
}

Your init() method doesn't have to return the "this" scope if you are calling it using the "new my.cfc()" syntax. True story.

If you are inside a cfc and want to set a property, dont use this.setFoo(), just go setFoo().
Same goes for getFoo().
The this.xxx() is like going out the front door only to come back in. Also, your access=private custom getters and setters wont work as the functions wont be in the this scope.

"var foo" vs "local.foo" - personally, I prefer var'd variables as there is a) less code to type, and b) less code to read.

// there isnt a huge difference here
var today = now();
var tomorrow = dateAdd( 'd', 1, today );
local.today = now();
local.tomorrow = dateAdd( 'd', 1, local.today );

// but when you start getting more complex examples, it quickly blows out
var out = method( var1, var2, var3, var4, var5 );
local.out = method( local.var1, local.var2, local.var3, local.var4, local.var5 );

Use javadocs style comments. Documentation is your friend.

/**
* @hint This is a hint for the whole function
* @arg1 This is an argument hint
* @arg2 This is another argument hint
**/
public void function myFunction( string arg1 = 'default', boolean arg2 ) {
    return true;
}
护你周全 2024-08-09 17:43:25

所有更改数据的函数都应该返回某个值,即使它是当前始终为 true 的布尔值。函数最终需要返回 false

all functions that alter data should return some value even if it is a boolean that is currently always true. Functions have a way of eventually needing to return false

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