Being Productive with Code Generators

Read Time 2 minutes

Being a programmer we have to write a lot of code which we should not be writing . One such example is writing constructors and getters/setters of a very simple class. We can use below tools/libraries to generate code which is repetitive, universal and well tested and spend our time on more important things such as business logic.

Android Studio

We can generate code using android studio IDE. Define fields and press  Alt+Insert to generate code for constructor, getters  setters and toString().

JSON2Pojo

Json is mostly used as a data format for RESTful web APIs. You can use Json2Pojo to convert your Json objects to Java class.

Parcelabler

In order to efficiently pass java objects between different components (i.e activities, fragments) we need to implement Parcelable interface. You can use Parcelabler to get the Parcelable implementation of your java class!

AutoValue

Writing code for Value Types can be tedious. We can use Google’s AutoValue library for generating code for your Value Types (equals(), hashCode() etc) . Now with extension we can  even generate code for our Parcelable classes. An Introduction to Auto Value is a great article explaining it’s usage.

Retrofit

Retrofit simplifies the the communication with HTTP REST Apis. All you need to do is to define an interface specifying url, http method and request and response types.  Retrofit — Getting Started and Create an Android Client is great article to start with.

Dagger

Dagger is dependency injection library from Square. It generates all the factory classes for you and inject where you need, anywhere in android application. I have written an article Dependency Injection with Dagger for easy setup and start with dagger. Some other useful resources are: Tasting Dagger 2 on Android and this video  .

ButterKnife

ButterKnife is a resource injection library from very famous Jake Wharton. You don’t have to use findViewById and getColor etc with ButterKnife. Its home page is quite self explanatory.

 

If you know any other such tool, please comment below.

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *