Attention, Android developers: Behold a magic Eclipse setting that will save you hours of work modifying each generated setter by hand.
One thing I love about being a software engineer is that even after 20 years I am still learning. A lot of that learning occurs from trial and error, but last week a colleague shared a simple configuration tip for Eclipse that will save me hours of work over the years to come (or at least until Google finally moves Android Studio with IntelliJ out of beta).
Just what is this magic setting you'll wonder how you ever lived without? Well, it has to do with auto-generated getters and setters and the Android de-facto coding convention of prefixing private fields with a lower case "m" character.
Consider the following common scenario.
package com.example.sample; public class Demo { private String mMyString; private int mMyNumber; }
Here I created a simple container class with a couple of private variables. LINT immediately complains because there are no getter / setters for them.
Eclipse offers to fix the issue for me. All I have to do is click: "Create getter and setter for...". Unfortunately, by default, Eclipse generates the following code when left to its own devices.
public void setMyString(String mMyString) { this.mMyString = mMyString; }
Technically this is correct, but the intent is convoluted, especially if you are using Javadocs to generate documentation on the method signature.
What we really wanted was this.
public void setMyString(String myString) { mMyString = myString; }
It is easy enough to manually make that change -- in fact, I have done so many, many times over the last five years. And then last week I was doing a bit of pair programming, and my colleague asked "why don't you just configure Eclipse to do that for you?"
Really? I was sure such a time-saving option would require lines of configuration drudgery and possibly custom scripts. In truth, it turned out to be just a few mouse clicks: Preferences | Java | Code Style.
Edit the field conventions to include the prefix as illustrated above, click Apply, and you're done!
I'm sure in time I will come to accept not having to go back and modify every generated setter by hand, but for now I grin every time Eclipse does it for me.