Skip to main content
Ben Nadel at Angular 2 Master Class (New York, NY) with: Matthew Kim
Ben Nadel at Angular 2 Master Class (New York, NY) with: Matthew Kim

Adding isSame() And isDifferent() Convenience Methods To ColdFusion

By
Published in Comments (3)

One part of Big Sexy Poems (BSP) that I've truly enjoyed is the inclusion of a global cfmlx.cfm extensions file. And while I'm a huge fan of encapsulation and strong boundaries, having a centralized ceremony-free collection of utility functions has been straight-up luxurious. It means that I get to address lots of little bits of friction that keep popping up in ColdFusion. For example, I do a lot of compare() calls in BSP in order to manage poem revision. And as helpful as compare() is for sorting, it just doesn't read well for comparison. So I created some simple wrappers: isSame() and isDifferent().

The compare() function works by performing some sort of numeric difference between the two strings. A 0 (zero) result means that the strings are exactly the same. A non-zero result means that the strings are different. My user defined function (UDF) wrappers do nothing more than translate this numeric result into an inverted Boolean that's more human friendly:

<cfscript>

	/**
	* I determine if the two arguments are different using a case-sensitive comparison.
	* This is just a semantic convenience wrapper for the underlying compare() method.
	*/
	private boolean function isDifferent(
		required string a,
		required string b
		) {

		return !! compare( a, b );

	}

	/**
	* I determine if the two arguments are the same using a case-sensitive comparison.
	* This is just a semantic convenience wrapper for the underlying compare() method.
	*/
	private boolean function isSame(
		required string a,
		required string b
		) {

		return ! compare( a, b );

	}

</cfscript>

The compare() function has a compareNoCase() variant; but, I have no need to wrap that variant since a case-insensitive comparison can be easily achieved with the normal == and != operators. The only reason I like the compare() function is specifically because it is case-sensitive.

To see this in action, we can compare and contrast strings that are different in case alone. Note that at the top of this CFML template I'm including my /core/cfmlx.cfm global extensions file. This is where the isSame() and isDifferent() UDFs are defined:

<cfscript>

	// ColdFusion language extensions (global functions).
	include "/core/cfmlx.cfm";

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	tests = [
		[ "ben", "ben" ], // Same
		[ "ben", "BEN" ], // Different
	];

	for ( [ a, b ] in tests ) {

		echo( 'isSame( "#a#", "#b#" ) ==> ' );
		echo( '#isSame( a, b )# <br>' );

	}

	for ( [ a, b ] in tests ) {

		echo( 'isDifferent( "#a#", "#b#" ) ==> ' );
		echo( '#isDifferent( a, b )# <br>' );

	}

</cfscript>

When we run this ColdFusion code, we get the following output:

isSame( "ben", "ben" ) ==> YES
isSame( "ben", "BEN" ) ==> NO

isDifferent( "ben", "ben" ) ==> NO
isDifferent( "ben", "BEN" ) ==> YES

At first, I used the names isEqual() and isDifferent(); but the naming felt incongruous. The term "same" feels like a more appropriate antonym for "different". That said, I'm not married to either name and if anyone has a better suggestion, I'm all ears.

Want to use code from this post? Check out the license.

Reader Comments

16,185 Comments

@Paolo,

I've had some issues with === in the past. I might be getting my platforms confused; but, I'm pretty sure that when I was using Lucee CFML, the === is actually an address comparison. Meaning, it's comparing the in-memory address of the two operands, not their actual value. I'll have to look at the Adobe ColdFusion to see if they 1) support it and 2) what it implies.

16,185 Comments

I'm looking at the Adobe ColdFusion identity operator now and I don't see anything about memory address. So this may have been something specific to Lucee CFML.

But, looking at the Lucee CFML oeprators, they do mention that it's case-insensitive, which is main reason I was trying to use the compare() method.

I'll have to look to see if === is case-sensitive in Adobe. Thanks for the suggestion, I'll post back later when I know more.

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed ColdFusion hosting services provided by:
xByte Cloud Logo