在 CF9 中编写 cfc 的编码约定?
有了 CF9 中编写 CFC 的新方法,CF9 有哪些新的编码约定?
以下是我能想到的一些...
- 始终使用 LOCAL 范围
- 始终包含返回自身的
init()
方法,因为New
如果找到,将调用 init() 。 - 如果它是 ORM 实体,请不要将必需的参数放入
init()
中,否则会出现异常... - 始终在其中使用
THIS.setXXX
XXX 是 init() 中的属性名称,因此它将调用隐式 setter 或自定义 setter(如果可用)。 - 放弃 CF8 之前的实例范围约定,请参阅:http://henrylearnstorock.blogspot.com/2009/08/should-we-abandon-instance-scope-in-cf9.html
- 对于脚本样式 CFC 中的组件和函数,没有输出 = false,请参见: http://www.coldfusionjedi.com/index.cfm/2009/8/26/Ask-a-Jedi-Impact-of-whitespace-and-script-based-CFCs
- 使用清洁剂和比
isDefined()
更高效
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...
- always use LOCAL scope
- always include
init()
method that returns itself, sinceNew
will call init() if found. - do not put required arguments in
init()
if it is an ORM entity, otherwise expect Exceptions... - always use
THIS.setXXX
in where XXX is property name inside init(), so that it will call the implicit setters or custom setter if available. - abandon the pre-CF8 INSTANCE scope convention, see: http://henrylearnstorock.blogspot.com/2009/08/should-we-abandon-instance-scope-in-cf9.html
- no output=false for component and functions in script style CFC, see: http://www.coldfusionjedi.com/index.cfm/2009/8/26/Ask-a-Jedi-Impact-of-whitespace-and-script-based-CFCs
- use the cleaner and more efficient
isNull(arguments.optionalArg)
instead ofisDefined()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不这么认为。
本质上会抑制任何空格,并且需要writeOutput()
才能获得任何输出。I wouldn't think so.
<cfscript>
by its nature suppresses any whitespace and needswriteOutput()
in order to have any output at all.如果您使用“new my.cfc()”语法调用 init() 方法,则不必返回“this”作用域。真实的故事。
如果您在 cfc 中并且想要设置属性,请不要使用 this.setFoo(),只需使用 setFoo() 即可。
getFoo() 也是如此。
this.xxx() 就像走出前门只是为了回来。此外,您的 access=private 自定义 getters 和 setters 不会工作,因为函数不会在 this 范围内。
“var foo”与“local.foo” - 就我个人而言,我更喜欢 var 变量,因为 a) 需要输入的代码更少,b) 需要阅读的代码更少。
使用 javadocs 风格的注释。文档是你的朋友。
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.
Use javadocs style comments. Documentation is your friend.
所有更改数据的函数都应该返回某个值,即使它是当前始终为 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