Friday, May 18, 2018

Enforce Your Class APIs with Module Enforcer

All Android developers will reach a day where they are faced with a monolithic activity chock-full of disparate logic. That logic makes it hard to navigate your code and write unit tests, so we split the behemoth into new single purpose classes (modules). Great! Now we have clean architecture, end of story!

I wish it were that simple.

I find, that as I create new modules, I need to hook them into different lifecycle changes. If you are the only developer and are never planning to maintain your app, the cognitive load of the hookups may be low. But come back to it in a couple months, or throw some more devs into the mix, and someone might remove a method call that results in an insidious bug or memory leak. This is a generic problem that comes with creating new classes that need to communicate/sync their state.

Module Enforcer Logo
One day, I'll pay for a real graphic designer
I wanted to annotate required methods in these classes (modules) and receive a warning when I didn't call said methods. This was just the job for Lint. So I set about creating a library for it. After a couple months of studying and some trials and tribulations (you can read about that here), I would like to present Module Enforcer to you.

Module Enforcer lints both Kotlin and Java files. In addition, you can use the lint gradle plugin to enable lint in non Android projects so you can enforce module apis across all of your Java and Kotlin projects! (disclaimer, I have not used this feature).

The utility of Module Enforcer increases as more developers use your module. If you are building a library that has multiple methods that must be called, Module Enforcer will let other developers know when they are miss-configuring your classes.

Currently Module Enforcer is still in early development and has some limitations that are better described in the README. Please try it out, and let me know if you have any feedback!


Post a Comment