Operator Precedence in ColdFusion

Posted by Dan on Mar 6, 2008 @ 11:47 AM

Simon Whatley had a good post yesterday on operating precedence in ColdFusion. It's important to understand the priority in which statements are evaluated, but what I think is more important is to explicitly define the precedence by using parenthesis. This makes the code much easier to read and much less likely to be misinterpreted. Take the equation:

x = 3 + 4 * 5;

What is the value of x? The correct value is 23, since the multiplication operator takes precedence over the addition operator. However, it's very easy to misread the equation as 3 + 4 = 7, 7 * 5 = 35. Here's the equivalent line of code, but with the precedence of the operations explicitly defined:

x = 3 + (4 * 5);

This is much clearer to the reader on how the equation should be executed. Writing code that easy to maintain should always be one of your top priorities and using parenthesis around your statements is one great way to help the readability of your code.

Categories: HTML/ColdFusion


Add Comment

Leave this field empty