July 12, 2018
Writing CFMs without Semicolons
Comments
(5)
July 12, 2018
Writing CFMs without Semicolons
Newbie 3 posts
Followers: 2 people
(5)

Semi-colons have been statement separators for most programming languages. Earlier when processing was slow and memory was expensive, language designers needed to split programs into multiple statements. A few languages required each statement to be on a new line so that the carriage return acted as a delimiter. Some languages required a free format text layout as a separator. Thus, the semi-colon was chosen as a separator so that it doesn’t conflict with other characters, for example, operators for math operations.

Many languages do not use semi-colons as statement separator, for example, JavaScript, Python, Golang etc. ColdFusion used semi-colons as separators from the first version. In the 2018 release of ColdFusion, we have made semi-colons optional.

There are some cases where it is mandatory to use semi-colons just to avoid ambiguity. CFML now  gives the flexibility to write code without semicolons in majority of the language constructs except a few, where it becomes necessary to avoid the ambiguity.

Some code snippets without semicolons as delimiter are shown below:

<cfscript>
animals = ['cat','dog','fish','bison'] 
lastAnimal=animals.last()
writeOutput("The last element of the array is: " & lastAnimal)
</cfscript>
<cfscript>
cfhttp(url="http://localhost:8500", timeout="60")
</cfscript>

component{
function myfunc(){
writeoutput("Optional Semi Colon Example")
}
}

There are some constructs where semi-colons have to be used to resolve ambiguity and proper functioning of code, for example:

<cfscript>
param name="num" default="3" max="100";
writeOutput(num)
</cfscript>

In the above example if we don’t use semi-colons it creates an ambiguity whether we are trying to define the max value for a variable or  creating a variable with the name max. Hence semicolons will be required here.

5 Comments
2019-10-28 15:49:08
2019-10-28 15:49:08

Pointless feature. The exceptions where you must use a semicolon actually serve to highlight weaknesses inherent in the language.
`param name=”num” default=”3″ max=”100″;`
If CF where a true OO language these would just be properties.

Like
(1)
>
Savaticus
's comment
2019-10-28 17:21:31
2019-10-28 17:21:31
>
Savaticus
's comment

It’s not “pointless”, when the very reason indicated for doing it above is to be more consistent with other languages, and when folks here and in other discussions on the web celebrate it. It may not be YOUR cup of tea (for them to have bothered), but do you really need to piss in the cup of others?

As for that edge case where the semi-colons are still needed, that’s not so much an “inherent weakness in the language”. That older param variant for cfparam in script is merely a way to do it that was added originally in the previous decade.

Since CF11 (from 2014), one CAN write about any tag as script, and one could pass the args in a way that does not require semicolons:

cfparam(name=”num”,default=”3″)

or

cfparam(name:”num”,default:”3″)

If you may feel that’s still not suitable to a “true OO language”, can you share how you would prefer to see it be written instead? Enlighten us, rather than denigrate.

Or if your beef is not so much with param/cfparam, or semicolons, but some other facet of CF that is lacking in OO sensibilities, share that or those.  You may be surprised that many you would list have long-since been addressed, but if you’re really wanting to advance the conversation, here’s a good chance.

Like
(1)
2019-10-15 18:51:18
2019-10-15 18:51:18

We updated to 2018 this year and I was pleasantly surprised to see that semi-colons were no longer required.

Thank you!

Like
2018-07-13 09:39:05
2018-07-13 09:39:05

Your include example requiring semicolons makes no sense.

include “blah.cfm” writeoutput(“test”)

is totally clear as the speach marks indicate the file names.

If you really wanted to include a file called “blah.cfm” writeoutput(“test” then you’d need to escape the marks, just link in any other string.

Like
(1)
>
Tom Chiverton
's comment
2018-07-16 07:16:57
2018-07-16 07:16:57
>
Tom Chiverton
's comment

Thanks Tom for the feedback, i have added a more appropriate use-case which shows a scenario where semicolons can be used to avoid the ambiguity.

Like
Add Comment