Sam Farmer head shot

Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

But I have become a passionate and pretty good web developer.


10 Little Ones: The New Array Features

Adobe ColdFusion 10 brings some improvements to manipulating arrays.

In this example we have two arrays called one and two. A new argument has been added to arrayAppend called arrayConcat that when true will append two arrays together.

one = ["a","c","e"];
two = ["b","d"];

arrayAppend( one, two, true );

writeDump( var=one, label="one after arrayAppend");

one after arrayAppend - array
1 a
2 c
3 e
4 b
5 d

Array one now has all the values. The order seems a little off though. Enter arraySort which as its second argument takes a closure/function where you can write your own sort logic. Basic sorting is straightforward and its nice to have the flexibility to extend it when needed.

arraySort( one, function( a, b ) {
	return ( a > b );
});

writeDump( var=one, label="one after arraySort");

one after arraySort - array
1 a
2 b
3 c
4 d
5 e

In the next example we'll filter the array by writing a closure to return an array with just the letters that are in my name.

filtered = arrayFilter( one, function( val ) {
	if ( reFindNoCase("s|a|m", val ) ) {
		return true;
	} else {
		return false;
	}
});

writeDump( var=filtered, label="filtered");

filtered - array
1 a

Slicing of arrays has also been added with the arraySlice function.

sliced = arraySlice( one, 1, 1 );
writeDump(sliced);

array
1 a

last = arraySlice( one, -1, 1 );
writeDump(last);

array
1 e

Where it makes send these functions also have list and struct equivalents.

10 Little Ones: What are Implicit Accessors?

ColdFusion 10 added implicit accessors and in this post I'll cover how to turn them on and use them.

Turning on implicit accessors is achieved via a straightforward setting in application.cfc:

view plain print about
1component {
2
3this.name = "implicitFun";
4this.invokeImplicitAccessor = true;
5
6}

Once on this means we can now access a getter and setter function of an object/cfc using the same syntax as if it where a struct. Let's look at a simple component called user.cfc:

view plain print about
1component accessors="true" {
2
3property firstname;
4property lastname;
5
6function getFirstname() {
7    return UCase( variables.firstname );
8}
9
10function getName() {
11    return variables.firstname & " " & variables.lastname;
12}
13
14}

Now let's see what happens when we make an object and start interacting with it:

view plain print about
1<cfscript>
2//using the automatic constructor -- there is no init
3
//function in user
4
user = new user( firstname="Homer", lastname="Simpson" );
5
6//calling firstname will run getter function
7
//in CF9 this was user.getFirstname()
8
writeOutput( user.firstname );
9
10/*DISPLAYS:
11HOMER
12*/

13
14//calling name which directly references variables scope does
15
//not call getter function
16
writeOutput( user.name );
17
18/*DISPLAYS:
19Homer Simpson
20*/

21
22//now lets change the name and call it again:
23
user.firstname = "Lisa";
24writeOutput( user.firstname );
25
26/*DISPLAYS:
27LISA
28*/

29
30
31
</cfscript>

As you can see from the code above when you call the implicit accessor on an object it calls the getter or setter function but within a cfc calling the variables scope directly calls the variable and not the getter or setter function.

10 Little Ones: Automatic CFC Init (#5)

One of the language enhancements in ColdFusion 10 beta is implicit/automatic constructors for CFC's. In short this means that if a component has defined properties and there is no init function ColdFusion will look to match the parameters passed in to the properties. Lets take a look at how it works:

view plain print about
1component accessors="true" {
2
3property firstname;
4property lastname;
5
6}

view plain print about
1<cfscript>    
2user = new user( firstname="Homer", lastname="Simpson" );
3
4writeDump( var=user, format="text", showudfs=false );
5
</cfscript>

Which will produce:

[cfc1] component user

Properties:

firstname: Homer

lastname: Simpson

Parameters can be passed in as name-value pairs or as a struct:

view plain print about
1<cfscript>
2user = new user( {firstname="Homer", lastname="Simpson"} );
3
</cfscript>

10 Little Ones: Specify Language Version on Per Project Basis in Builder (#4)

The ColdFusion 10 beta also includes ColdFusion Builder 2.0.1 beta (scroll down page). One nice addition is the option to specify language version on a per project basis rather than per editor.

For developers who work on multiple versions this is a great improvement.

10 Little Ones: Write and Append Enhancements for cffile (#3)

An enhancement in ColdFusion 10 to cffile makes it very easy to write or append content to a file on physical disk, Amazon S3 or RAM disk.

In the following example a new file will be created:

view plain print about
1<cffile action="append" file="#expandPath("test.txt")#">
2Here is my file content
3With several lines
4That go on
5</cffile>

In this example the content is appended to the file:

view plain print about
1<cffile action="append" file="ram://test.txt">
2And now
3appending a couple lines
4</cffile>

10 Little Ones: For in loop for Queries (#2)

ColdFusion 10, now in public beta, adds for-in loops for query recordsets. The variable name in the for loop will reference each column by the column name.

The currentrow can be referenced by queryname.currentrow as seen in the example below. Same for recordcount.

view plain print about
1<cfscript>
2getArt = new query( sql="SELECT artName, price FROM Art" ).execute().getResult();
3for ( row in getArt ) {
4    writeOutput( "<li>" & getArt.currentRow & ": " & row.artName & " " & row.price & "</li>");
5}
6
</cfscript>

10 Little Ones: Memory and CPU Figures (#1)

ColdFusion 10 beta adds three new functions that provide system stats:

  • getCPUUsage()
  • getSystemFreeMemory()
  • getSystemTotalMemory()
The first returns a percentage while the other two return the amount of bytes. Here is a quick example:

view plain print about
1<b>CPU Usage</b> #getCPUUsage()#<br><br>
2
3<b>System Memory</b><br>
4Total #getSystemTotalMemory()#<br>
5Free #getSystemFreeMemory()#<br>
6
7% Free: #numberFormat( ( getSystemFreeMemory()/getSystemTotalMemory() ) *100, "999.99")#

Which produces


CPU Usage 24.875624

System Memory
Total 8589934592
Free 1640640512
% Free: 19.10


Pretty straightforward and information that would look good in the new charting functionality...

(This is the first in a series to show off some of the smaller additions to ColdFusion 10).

BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner