How to reduce use of boilerplate code in Kotlin

Malwinder Singh
2 min readApr 3, 2018

--

Writing clean code has become an important skill. It is important to follow coding conventions and use coding design patterns. It is important that the code you wrote is understandable to an average developer, and has no boilerplate.

What is Boilerplate code?

Boilerplate code is that code which looks exactly the same to the code which was previously written but instead of reusing the previous code, you decided to write the code again.

Don’t write boilerplate code. Make Java methods (or Kotlin functions, depending on the language you are using) and call those again and again wherever required. Provide “arguments” to the functions or methods if needed. This will reduce the number of lines of code in the project.

Example of Boilerplate code:

The problem with above code is that we are launching two different activities and we are putting the same data in the Intent. Such code which is repeated is called boilerplate code.

Now, take a look at this example:

Here, we used Intent#replaceExtras() method to set the Bundle. Since, this Bundle is actually same for starting both the activities so we created method getExtras().

If you have any questions, then please write down response in response section.
If you liked this article, then please press 👏 icon. Thank you!

Hope you liked this article!

Medium does not allow Indian writers to monetize currently 😭. Please support this blog by contributing here: https://www.patreon.com/malwinder 🥰

Buy me a coffee 🥰

Even a small contribution would help 😍

If you have any questions, then please write down a response in the response section.
If you liked this article, then please press the 👏 icon.
Thank you!
Peace! 🙂

--

--

Malwinder Singh

Flutter, Kotlin & Android. Worked on projects of Emerson and Omron, 50k+ views on this blog, 6k+ repo on StackOverflow, Drone startup linkedin.com/in/malwinder