Understand
Generics in Functions and Classes
In under 5 minutes — Day 2 of #100DaysOfCode
5 minutes! Let’s get straight into it!
Generic Functions
There might be times when you have a function which is doing the same operation for different data types returning you with an appropriate data type.
A simple example for all readers: You are in the initial coding phase in your project and you have to write a logic to find the greater of the two number. You create a function in Util class to do so, which returns you the greater of the two numbers.
Things look clean and simple, well that’s how they always are at the beginning of the project. Right? :P
Now after some time, there is a requirement to write a similar function that compares and returns the greater of the two Double numbers. Now there is a voice in your head saying to just copy-paste the similar function and change data types and you would be done.
This is where Generic Classes and Functions come and help you as a programmer to define a common logic in a single function which will work for your different data types and also will provide you with compile-time safety to help you catch invalid types.
So even if there is another similar requirement in the future, you won’t have to create another function, you could use the existing Generic function logic, or define your own for the particular data type. All inside the same function. Cool and Clean!
Here we have created one generic function called findGreater. The key thing to note here is that <T> means its a generic type and since we are using compareTo method, we define <T extends Compareable<T>>
Now let’s see how we can reuse this Generic function.
Why should you use Generics?
- It helps you wrap similar logic functions into one generic function.
- Gives your compile-time safety and serves as a contract for you to pass certain values only. Helping you avoid bugs which might creep out when you have multiple functions.
- There is no type casting involved. Sweet!
Generic Classes
Similar to functions, you have the option to make your classes Generic as well. This helps you wrapping similar logic into a single class instead of creating two classes.
Let’s say we have to create a class to deal with Dimensions, we create this class and this class also has a generic method compareTo which we created above.
A simple example for all readers for Generic Classes: You are building a car with a petrol engine which you have fitted inside the hood of the car. Now seeing the market trends of people moving towards hybrid/electric cars. You also decide to put up an electric motor in your car. Would you create another hood to fit in this electric motor? No! That’s stupid and ugly, right? You would put both the petrol engine and the electric motor in your hood. Similarly, you be smart and write beautiful code using Generic Classes.
Once you get into the implementation of Generics, you will love how simple and clean your codebase becomes. — devDeejay
One last bad example, I had a Networking Repository in my Android Project which was hitting multiple API endpoints and gives back a JSONString in response. If there were 10 endpoints I wrote 10 separate functions to make those calls and give the back JSON response. So if you can find out a common pattern here, some common networking parameters and a common JSON response, the whole thing can be generified into one function in Networking Repository.
P.S. Use Retrofit for Networking in Android.
That’s all for Day 2 folks! Thanks for giving your time, hope you learned something fruitful.
Checkout Day 1 of 100Days of Code — Learn SOAP, REST & GraphQL under 5 minutes.