💡How honest are your functions? How informative? What do you think of the following function❓
👉To begin with, the method above takes an input parameter of type int that is named as the year of birth. A simple question arises immediately, is 2100 a valid year of birth? are negative numbers valid?
👉To continue on, the method returns an int. We can't be sure that it returns an age. We can only assume based on the method's name. But the above argument applies here too. What if it's a negative number? What If it returns something above 100?
❕This is also called primitive obsession. What primitive obsession means is that we use primitives for modeling ideas that do not fully adhere to that primitive. In the above example, we can model both as a new type ie Year, Age and provide context, validation and intent.
⚡️To sum up, when designing your public api keep in mind primitive obsession and try to design your methods as informative and as honest you can. It simply makes life easier for everyone around you, including you! 😉
👀Pro-tip for #AndroidDev, you can leverage inline classes for this or in the future value classes. 🚀
You can follow @G_Theocharis.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.