In my last article My CQ JSP Development Best Practises we solely talked about JSP related issues, one of which was the use of JSTL/EL. Not having a reasonable approach for dealing with JSTL/EL can land you in just as much trouble as a poorly constructed JSP. These practises should help.
- Variables and Methods
- Comparison Operators
- Tagfiles vs. Classic Tags
- Scoped variables
1) Variable and Methods
There are two operators to address JavaBean attribute's (getXXX), List and Map values, and scoped variables. For consistency/readability, I suggest the following:
Addressing Map values : Use the pattern someVariable['someProperty'] - always use the square brace and single quote notation. The single quote is not required. However, if the property name contains special characters, the single quote is required. Be consistent for readability and predictability.
Addressing Methods : Use the pattern someVariable.someMethod - keep in mind that the method name must adhere to JavaBeans naming conventions. So, if "someVariable" has method "getAge", the EL version of this would be: "someVariable.age". Dot notation can be used to address Map values. However, for readability, don't use dot notation for that.
Being consistent will make it clear that when using dot notation you are always addressing a JavaBeans accessor, and bracket+quote notation you are addressing Map value.
2) Comparison Operators
In older versions of CQ I have seen issues with using the 4GL versions of the comparison Operators. Use the standard mathematical operators and circumvent these potential issues. In the event these idiosyncrasies have been addressed, I still recommended you settle on one convention for consistency and readability.
3) Tagfiles vs. Classic Tags
For those unfamiliar with writing JSTL tags and the semantics that go along with them, maintaining or extending classic tags can be difficult. Even for those who have written classic tags, it can be difficult to remember the necessary semantics for writing one. Using tagfiles circumvents this gap in understanding or memory, and greatly simplifies initial creation, maintenance, and extension.
While both types of tags typically reside within an OSGi bundle, classic tags require Java classes, and tagfiles are a special type of JSP. One thing to note is that a more relaxed approach to developing a JSP tagfile is employed (in reference to JSP "musts") to facilitate encapsulation of source code.
One of the major benefits to using tagfiles is that all of the taglibs available to a regular JSP are available to tagfiles as well. So this can greatly increase developer productivity when it comes to writing custom tags.
4) Scoped Variables
Use the same JSP variable name for the scoped variable name. This provides more consistency between variable usage in JSTL/EL and scriptlets, and reduces the amount of information a developer is required to deal with during development tasks. The consistency will also facilitate maintenance, and new developer understanding.
These simple guidelines should help you write readable, predictable, maintainable, and just about any other able you can think of.