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 ===?
@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.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.@Paolo,
I just confirmed that, at least in ACF, the
===is case-insensitive. I added this to my internal test:... and got this output:
That said, I should look into the
===identity operator more; I've never really used it outside of JavaScript.Hi @Ben,
the === is case-insensitive but compare the type of variables
see here
https://coldfusion.adobe.com/2020/11/strict-equality-equality-operator-savecontent-new-syntax/
As an old school guy, I too still prefer Compare()
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →