Created
February 25, 2014 13:39
-
-
Save bennadel/9208870 to your computer and use it in GitHub Desktop.
The Elvis Operator vs. IsNull() In ColdFusion 11 Beta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing unscoped values. | |
| elvis = ( theKing ?: "default" ); | |
| nully = ( isNull( theKing ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing function results. | |
| public any function getNothing() { | |
| return; | |
| } | |
| elvis = ( getNothing() ?: "default" ); | |
| nully = ( isNull( getNothing() ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing undefined return values (native methods). | |
| values = []; | |
| noop = function() {}; | |
| elvis = ( values.each( noop ) ?: "default" ); | |
| nully = ( isNull( values.each( noop ) ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing function results as containers. | |
| public any function getNullContainer() { | |
| return; | |
| } | |
| elvis = ( getNullContainer().foo ?: "default" ); | |
| nully = ( isNull( getNullContainer().foo ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing function results as containers. | |
| public any function getNullContainer2() { | |
| return; | |
| } | |
| elvis = ( getNullContainer2()[ "foo" ] ?: "default" ); | |
| nully = ( isNull( getNullContainer2()[ "foo" ] ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing undefined arguments. | |
| public any function testArguments( string theKing ) { | |
| elvis = ( theKing ?: "default" ); | |
| nully = ( isNull( theKing ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| } | |
| testArguments(); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing non-enabled sessions. | |
| elvis = ( session ?: "default" ); | |
| nully = ( isNull( session ) ? "default" : session ); | |
| writeOutput( "Elvis: " & isStruct( elvis ) & "<br />" ); | |
| writeOutput( "Nully: " & isStruct( nully ) & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing custom tag's CALLER scope. | |
| parent = {}; | |
| cf_tagfail(); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing scoped values. | |
| elvis = ( url.theKing ?: "default" ); | |
| nully = ( isNull( url.theKing ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing bracket notation. | |
| elvis = ( url[ "theKing" ] ?: "default" ); | |
| nully = ( isNull( url[ "theKing" ] ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing deeply-nested values. | |
| elvis = ( url.foo.bar ?: "default" ); | |
| nully = ( isNull( url.foo.bar ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing strict null values. | |
| javaNull = javaCast( "null", "" ); | |
| elvis = ( javaNull ?: "default" ); | |
| nully = ( isNull( javaNull ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing strict null members. | |
| request.strictNull = javaCast( "null", "" ); | |
| elvis = ( request.strictNull ?: "default" ); | |
| nully = ( isNull( request.strictNull ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing undefined array index. | |
| values = []; | |
| values[ 2 ] = 2; | |
| elvis = ( values[ 1 ] ?: "default" ); | |
| nully = ( isNull( values[ 1 ] ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing out-of-bounds array index. | |
| values = []; | |
| elvis = ( values[ 2 ] ?: "default" ); | |
| nully = ( isNull( values[ 2 ] ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing out-of-bounds list index. | |
| list = "a,b,c"; | |
| elvis = ( list.getAt( 5 ) ?: "default" ); | |
| nully = ( isNull( list.getAt( 5 ) ) ? "default" : "false-positive" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing "undefined" CGI members. | |
| elvis = ( form ?: "false-negative" ); | |
| nully = ( isNull( form ) ? "false-negative" : form ); | |
| writeOutput( "Elvis: " & isStruct( elvis ) & "<br />" ); | |
| writeOutput( "Nully: " & isStruct( nully ) & "<br />" ); | |
| // NOTE: Long-standing issue. | |
| // http://www.bennadel.com/blog/1773-IsNull-vs-IsDefined-For-ColdFusion-9-Scope-Detection.htm | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing "undefined" CGI members. | |
| elvis = ( cgi.cf11 ?: "false-negative" ); | |
| nully = ( isNull( cgi.cf11 ) ? "false-negative" : "" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing new "member" functions. | |
| value = "ColdFusion 11"; | |
| elvis = ( value.ucase() ?: "false-negative" ); | |
| nully = ( isNull( value.ucase() ) ? "false-negative" : "" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing Java functions. | |
| values = [ 1 ]; | |
| elvis = ( values.size() ?: "false-negative" ); | |
| nully = ( isNull( values.size() ) ? "false-negative" : "" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing Java functions. | |
| request.getSomething = function() { | |
| return( true ); | |
| }; | |
| elvis = ( request.getSomething() ?: "false-negative" ); | |
| nully = ( isNull( request.getSomething() ) ? "false-negative" : "" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing first-class native functions. | |
| elvis = ( ucase ?: "false-negative" ); | |
| nully = ( isNull( ucase ) ? "false-negative" : "" ); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing user defined functions | |
| public any function noop() { | |
| return; | |
| } | |
| elvis = ( noop ?: "false-negative" ); | |
| nully = ( isNull( noop ) ? "false-negative" : noop ); | |
| writeOutput( "Elvis: " & isCustomFunction( elvis ) & "<br />" ); | |
| writeOutput( "Nully: " & isCustomFunction( nully ) & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| // Testing custom tag's CALLER scope. | |
| parent = { | |
| member: "exists" | |
| }; | |
| cf_tagpass(); | |
| writeOutput( "Elvis: " & elvis & "<br />" ); | |
| writeOutput( "Nully: " & nully & "<br />" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- NOTE: ColdFusion 11 was in BETA at the time of this writing. ---> | |
| <cfscript> | |
| value = ( url.foo ?: "bar" ); | |
| // ------------------------------------------------------ // | |
| // ------------------------------------------------------ // | |
| value = ( isNull( url.foo ) ? "bar" : url.foo ); | |
| // ------------------------------------------------------ // | |
| // ------------------------------------------------------ // | |
| if ( isNull( url.foo ) ) { | |
| value = "bar"; | |
| } else { | |
| value = url.foo; | |
| } | |
| // ------------------------------------------------------ // | |
| // ------------------------------------------------------ // | |
| // NOTE: This one is a little different since it's actually | |
| // changing the state of url.foo. | |
| cfparam( name = "url.foo", default = "bar" ); | |
| value = url.foo; | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfscript> | |
| caller.elvis = ( caller[ "parent.member" ] ?: "default" ); | |
| caller.nully = ( isNull( caller[ "parent.member" ] ) ? "default" : "false-positive" ); | |
| </cfscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfscript> | |
| caller.elvis = ( caller[ "parent.member" ] ?: "false-negative" ); | |
| caller.nully = ( isNull( caller[ "parent.member" ] ) ? "false-negative" : caller[ "parent.member" ] ); | |
| </cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment