Henry
Henry

Reputation: 32905

Coding conventions for writing cfc's in 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...

Upvotes: 6

Views: 1109

Answers (3)

James A Mohler
James A Mohler

Reputation: 11120

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

Upvotes: -1

Mike Causer
Mike Causer

Reputation: 8314

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;
}

Upvotes: 0

ale
ale

Reputation: 6430

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.

Upvotes: 2

Related Questions