My team has started to look into the use of Java Annotations to help get rid of some of the boilerplate that can be involved with Android development. We created annotations for injecting view references in Activities and Fragments. We created annotations for handling the registration of broadcast listeners. We created annotations for getting and maintainer references to certain classes, like to the host Activity from the Fragment. The pilot code that uses the annotations looks a lot cleaner (less casting, fewer lines of code). It looks easier (for me) to maintain. It had fewer bugs. Are those good reasons for removing boilerplate, though?
Saying the code has fewer bugs seems like a slam dunk. Lets analyse the bugs that occur, though. All the bugs that are prevented by using the annotations are related to the lifecycles of Activities and Fragments. Our developers were grabbing onto references to other objects incorrectly. Sometimes, they grab them in the wrong method (onCreateView vs onActivityCreated). Sometimes they don't handle saving the state to the bundle correctly. These are the type of mistakes that junior developers make more often than senior developers. Using annotations to hide these details prevents junior developers from learning the details of Android lifecycles. It holds them back, making it harder for them to become senior level. It also makes it harder to on-board mid-level developers. Mid-level developers will be used to seeing the boilerplate. It will take time for them to get used to the annotations.
One of the other developers on my team pointed out that debugging issues could be harder. The annotations make assumptions about the code it is in. If you inject a view with the id of R.id.button1, the assumption is a layout was already inflated that has a view with id R.id.button1. What happens if that assumption is wrong? Currently, you get a NullPointerException the first time you try use the reference to the view. A mid-level developer might spend hours diving into the framework, accusing it if being crap. They will curse the seniors to writing it, saying what they are doing is fine. The problem won't go away until a senior developer points out that they inflated their layout incorrectly, or they were in landscape mode and forgot to add R.id.button1 to the landscape layout. They might make this mistake because they aren't getting exposed to the type of mistakes that forces them to learn more about how Android works.
The annotations remind me a lot of the other frameworks that I have either used or written. They accomplish two things very well. First, junior developers can write code with fewer bugs. The frameworks do this by hiding the advanced details of what they are doing. This reduces bugs at the cost of never allowing them to advance as developers. The second thing these frameworks do is allow senior developers to write code faster. They can write code faster without sacrificing code quality.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.