Matt Gifford aka coldfumonkeh | Consultant Developer
View Github Profile

Property Value Shorthand proposition for CFML

Oct 21, 2019

The object property value shorthand has been available in JavaScript using ES6/ES2015 for some time. I personally think it’s a fantastic language feature that streamlines code and I would love to see it as part of future CFML engine updates.

Let’s take a look at a JavaScript example:

Here you can see that the object properties have been added without directly referencing the property names when applying the variables. This is because the variable names actually directly match the names of the properties being added.

Compare this to the older ES5 syntax, when you had to define the property names with the variables:

If, in ES6, the variable name differed for any of the properties, you would of course still need to define the property reference:

In the above example, the dog and bird variables still match the property names. However, we have a pussycat variable for the cat property, and so we need to explicitly define the property name.

Let’s consider this in a CFML context.

I think it would be amazing to have the same functionality when adding values into a CFML struct:

How about adding shorthand references in when sending argument values into a function?

I’ve created a very basic method in the following example with three argument values:

CFML functions already let you send in arguments without directly naming the property, but those depend on the values matching the order of the expected arguments within the function.

In the above example we are sending to, from and then message, but the function would automatically apply the from value to the message argument and the message value to the from argument.

Ideally I’d love all future CFML engines to be able to determine which variable is assigned to which argument based upon the variable name matching the argument name.

This situation would most likely help to ensure that any non-attributed arguments have the correct default values set and validation within the methods to handle data payloads and consistency of incoming parameters, which would rely on the developer to implement.

The function implementation might be a little bit of a stretch, but struct property shorthand should be relatively easy to implement (I would assume).

Latest Blog Posts

Jul 16, 2020
Github Actions with CommandBox and TestBox
Read More
Jul 9, 2020
Azure pipelines with CommandBox and TestBox
Read More
Dec 23, 2019
CFML content moderation detection component library
Read More