Understanding ListLen(var) vs. var.ListLen in ColdFusion
Understanding ListLen(var) vs. var.ListLen in ColdFusion
In ColdFusion, developers frequently work with lists, and one of the key functions for handling them is ListLen(). However, confusion may arise when using ListLen(var) compared to accessing var.ListLen directly. Both approaches can behave differently depending on the type of data being used, especially when variables contain strings, numbers, or other data types.
In this article, we will explain the differences between using ListLen(var) and var.ListLen, and why one works well with strings but causes an error with numbers.
1. What is ListLen() in ColdFusion?
ListLen() is a built-in ColdFusion function used to count the number of elements in a list, where elements are separated by a delimiter (default is a comma). This function operates directly on strings.
Syntax:
ListLen(list [, delimiters])
Example:
<cfset fruits = "apple,banana,orange">
<cfset lengthOfList = ListLen(fruits)>
<cfoutput>#lengthOfList#</cfoutput>
Output:
3
Here, ListLen(fruits) counts the number of elements in the string fruits, separated by commas, and returns 3.
2. What is var.ListLen in ColdFusion?
var.ListLen refers to a property or method that belongs to the variable var. This is not the same as the built-in ListLen() function. Depending on the type of var, accessing var.ListLen can yield different results or even cause an error.
3. Key Differences Between ListLen(var) and var.ListLen
4. Examples of Differences in Use
Example 1: Using ListLen(var) with a String
<cfset myList = "one,two,three">
<cfset listLength = ListLen(myList)>
<cfoutput>#listLength#</cfoutput>
Output:
3
Here, ListLen() works as expected. It counts the number of elements in myList, separated by commas, and returns 3.
Recommended by LinkedIn
Example 2: Using var.ListLen with a String
<cfset myString = "apple,banana,orange">
<cfoutput>#myString.ListLen#</cfoutput>
Output:
3
In some cases, ColdFusion can treat strings as objects with built-in properties, so accessing myString.ListLen returns the number of elements in the list implicitly. However, this behavior can vary and isn't as reliable as using the explicit ListLen() function.
Example 3: Using ListLen(var) with a Number
<cfset myNumber = 12345>
<cfset numberLength = ListLen(myNumber)>
Output:
1
In this case, ListLen(myNumber) returns 1 because ColdFusion treats the number 12345 as a single element. It doesn't consider it a list, so it counts the entire number as one item.
Example 4: Using var.ListLen with a Number
<cfset myNumber = 12345>
<cfoutput>#myNumber.ListLen#</cfoutput>
Output:
Error: Element LISTLEN is undefined in a Java object of type class java.lang.Double.
Here, ColdFusion throws an error because numbers don’t have a ListLen property. This illustrates the risk of trying to access var.ListLen when the variable is not a string or object that supports such a property.
5. Why Does This Happen?
ColdFusion automatically handles certain data types like strings and implicitly allows accessing properties like ListLen. However, this implicit behavior does not extend to other data types like numbers or structs.
6. Best Practices
<cfset listLength = ListLen(myVariable)>
<cfif isNumeric(myVariable)>
<cfoutput>#myVariable# is a number, ListLen will not work#</cfoutput>
<cfelse>
<cfset listLength = ListLen(myVariable)>
<cfoutput>List length: #listLength#</cfoutput>
</cfif>
7. Conclusion
The difference between ListLen(var) and var.ListLen in ColdFusion is significant, and understanding this distinction is key to avoiding errors. While ListLen() is a built-in function designed specifically for counting elements in a list, var.ListLen refers to a property or method of the variable, which can cause errors when applied to data types like numbers. To write more robust code, always use ListLen() explicitly and validate the data type of your variables before accessing properties or methods.
Senior Full Stack Developer at Civix
2moInteresting article. However, early on, I got into a habit of avoiding lists. Many times stepped in it when the list elements are strings with comma and CF taking comma as a default separator.
Great Article Tiago!