Adding isSame() And isDifferent() Convenience Methods To ColdFusion
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
Hi Ben,
why didn't you consider the use of the ===?
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →