Hey, devs! Let's talk about a crucial aspect of our craft: code reuse and modularization. Reinventing the wheel can be a time-consuming and error-prone endeavor. So, how do you tackle this challenge in your projects? What strategies do you employ to make your code more reusable, efficient, and maintainable?
Share your tips, tricks, and experiences, and let's level up our coding game together!
Follow the DEVteam for more discussions and online camaraderie!
Top comments (3)
The snippets of code and packages I've seen reused most times had 3 things in common:
A shameless plug, if I may. I just wrote the other day on this topic and the gist of it is to keep things simple and predictable at least within a certain scope.
Plug & Play Modular Architecture for Scalable and Maintainable Apps
Valeria ・ Jun 27 ・ 5 min read
The one thing that will cover the most ground on those topics to me is Test Driven Development
When working on my own I try to follow It but it depends on the complexity of the project; for small POCs I avoid It as they are projects bound to die after proving the tool/concept. But when working as part of teams from 2 or more devs, Its a must have.
Reusable: With TDD you need to be able to properly abstract your test subject, this leads to invariably have to follow a clean architecture and inject dependencies (in any possible way) so you can mock them. Hence, makes you write reusable pieces of SW.
Efficient: You don't write the code and then "see if it works", you write the test that will 100% make your code exactly what you need It to do. The output of doing good TDD is always efficient.
Maintainable: Tests not only work towards the previous 2 topics but they are also a great source of Documentation; any future dev trying to figure how a components works can directly reference how Its being tested, and they are a safety net to prevent changing the behaviour of the piece by accident; e.g.: you update a library that changes the behaviour of another component that also depended on it and boom, tests are there for you failing your build :)
So in short, my answer is: TDD, TDD and TDD :)