How to name getters/setters correctly in Groovy for Camel Case field properties

I would start out by qualifying this post by saying Groovy normally automatically creates getters/setters for you by default, so you don’t normally need to do this. It’s one of the joys of programming in Groovy over Java. But if you want to make individual properties immutable because it’s the result of say a long calculation and the result gets and stored in a transient say. You’d want to make the setter body have no content. and the getter perform the calculation if the transient property is null the first time (ie:lazy loaded properties).
As you know one of the quirks of Groovy, is that you can bypass getters/setters altogether by using the @ field access modifier,  (I like Scott Davis’s quote from Groovy Recipes here)

I’d rather have a sharper scalpel and a better trained surgeon than a duller blade.

But I still think it’s still a useful exercise create your own getters/setters under such conditions.

Anyway, back to the task at hand…

Ever get confused as to how to name getters and setters for properties?
If property is all lowercase you just convert first letter to uppercase and that’s easy enough.
If it’s all uppercase, no problem too. Just leave it as is.
Java & Groovy convention dictates properties shouldn’t begin with upper case and then revert to lower, so you can’t define getters and setters for such a property (Tx below). This is normally reserved for class names, so a getter/setter never gets called when you don’t manually call the property via the get/set method names and resort to property syntax alone (ie field name).
If they are camelCase like qText or rText, things get a bit more tricky to remember.
I made each accessor/mutator (getter/setter) write some output when invoked to test this out.
As you can see, uppercasing qText to QText property isn’t correct. It fires a blank. So you leave the property in it’s camel case state.
You can see this is right by examining the output of LHS (setter) or RHS (getter) property access when you access the property with field style syntax.

package jgf
class J {
 def ptext
 def qText
 def rText
 def ST
 def Tx
 void setPtext(s) {
   println 'setter ptext'
   this.ptext = s
 def getPtext() {
   println 'getter ptext'
   return ptext
 void setQtext(s) {
   println 'setter qText'
   this.ptext = s
 def getQtext() {
   println 'getter qText'
   return ptext
  void setrText(s) {
   println 'setter rText'
   this.rText = s
 def getrText() {
   println 'getter rText'
   return rText
  void setST(s) {
   println 'setter ST'
   this.ST = s
 def getST() {
   println 'getter ST'
   return ST
  void setTx(s) {
   println 'setter Tx'
   this.Tx = s
 def getTx() {
   println 'getter Tx'
   return Tx

def x = new J()
println '-'*5 + ' p: ' + '-'*5
x.ptext = 'a'
println x.ptext
println '-'*5 + ' q: ' + '-'*5
x.qText = 'b'
println x.qText
println '-'*5 + ' r: ' + '-'*5
x.rText = 'c'
println x.rText
println '-'*5 + ' s: ' + '-'*5
x.ST = 'd'
println x.ST
println '-'*5 + ' t: ' + '-'*5
x.Tx = 'e'
println x.Tx
----- p: -----
setter ptext
getter ptext
----- q: -----
----- r: -----
setter rText
getter rText
----- s: -----
setter ST
getter ST
----- t: -----

About this entry