CF Snippets

Use FileExists() for confirming the existence of a file

You should probably use fileExists() on a file path before attempting to read, delete, or work with any file in any way - especially if that file path is user-provided.
if ( fileExists( filepath ) ){
  var data = fileRead( filepath );
  // Import JSON data into ElasticSearch...

Use the Safe Navigation Operator

The safe navigation operator is an awesome way to return a null instead of throwing a [key] not exists error when traversing a struct.
 * Returns the status code of the HTTP response.
 Will return null if no HTTP response yet.
function getStatusCode() {
  return variables.httpResponse?.statusCode;

Clear log files with fileDelete()

Cleanup all .log files from your logs directory using a one-two combo of directoryList() and fileDelete().
var logDir = expandPath( "/logs/" );
var logs = directoryList(
  path = logDir,
  listInfo = "name",
  filter = "*.log",
  type = "file",
  recurse = "false"
for( var log in logs ){
  var fullPath = logDir & log ;
  if( fileExists( fullPath ) ){
    fileDelete( fullPath );

Using the ternary operator

The ternary operator is great for single-line if statements. Be careful not to stick too much logic in a ternary - you can quickly end up with some ugly code if you try to do too much.
function buildURL(
  required string to,
  string querystring = ""
) {
  var protocol = cgi.https == "on" ? "https" : "http";
  var querystring = arguments.querystring > "" ? "?#arguments.querystring#";
  var port = listContains( "80,443", cgi.server_port ) ? "" : ":#cgi.server_port#";
  var domain = cgi.server_name;
  return "#protocol#://";

Using the Elvis Operator

The Elvis operator is shorthand syntax for returning a default value if the first item is null.
// dumps the this scope, usually the executing component.
writeDump( this ?: "that" );
// dumps "that" because this1 is null and undefined.
writeDump( this1 ?: "that" );

Using the Switch construct

Using switch or cfswitch helps you write cleaner logic statements if you are altering behavior based on the value of a single variable.
// null-safe our order by argument.
var orderBy = arguments.order ?: "";

switch( orderBy ) {
  case "dateCreated":
  case "title":
    // do nothing, these column names are OK to order by.
  default: {
    // set a default if the order column is none of the above.
    // prevents SQLi vulnerabilities by ensuring we can only use a set of values for the order by parameter.
    var orderBy = "dateCreated";

Select query using named datasource

If you do not have a default datasource configured, you'll need to use this variant to specify which datasource to use.
var queryOptions = { datasource: "appMain" };
var data = queryExecute(
  "SELECT * FROM users", {}, queryOptions

Lock Down Your App with Application.cfc

There are a few security-related configuration settings in Application.cfc which you should consider enabling by default for every app you make. If you find a setting is too restrictive, you can always turn it off later.
// Application.cfc
component { = "myApp";
  // we don't need uploads in this app; why leave a possible open hole?
  this.blockedExtForFileUpload = "*";
  // easy first line of defense against scripts pasted into forms.
  this.scriptProtect					 = "all";
  // more secure session cookies
  this.sessioncookie = {
    httpOnly: true,
    secure  : true

Get Generated Primary Key from an INSERT statement

If you use MySQL to generate database record ids for you (e.g. a PRIMARY KEY created via an autonumber field or UUID() call), use the result option to specify a variable name which will contain the result of the query - including a generatedKey field for retrieving the primary key of the new record.
var queryOptions = {
  result: "newUser"
var data = queryExecute(
  "INSERT INTO users (username, password)
  VALUES ( 'mike.born', UUID() )",
writeOutput( "New user id: #newUser.generatedKey#" );

Global Error Logging from Application.cfc

The onError function in Application.cfc is a great way to catch errors within your application and make sure that each error is logged. Be aware that if you catch an exception within your app, that exception will not "bubble up" to this onError handler unless you also use rethrow.
// Application.cfc
component {
  // ...
  function onError( exception ){
   // Log it with LogBox!
    writeOutput( "Logging error with logbox..." );
        "Error in cfSnippets app: #exception.message# #exception.detail#"

    // show error page
    include "views/error.cfm";

Rethrowing errors to handle errors at the local AND global level

The rethrow function is, in my opinion, one of the coolest things ever. rethrow() lets your app catch and handle exceptions locally before rethrowing the exception to log or handle the error at a global level. This is awesomely helpful if you need to undo a transaction, for example, or to recover from any other context-specific situation.
// First level of error handling
// Say, onError in Application.cfc
try {
  // somewhere in app-land...
  try {
    // do something error-able
    x = 1 / 0;
  } catch( any e ) {
    // Recover so we don't bork the app
    // let the parent-level catch handle error logging.
} catch( any exception ) {
  cfparam( name="exception.message", default="[no message]" );
  cfparam( name="exception.detail", default="[no detail]" );

  // Log it with LogBox!
  writeOutput( "Logging error with logbox..." );
    "Error in cfSnippets app: #exception.message# #exception.detail#"