Reusability: It’s good!
“Reusability”, “Reusable Components”… we’ve all heard it before.
In my last post, I spoke about the Single Responsibility Principle. Now, if we take this idea and apply it to “reusable components” or “reusability” it makes total sense. But aside from reusable components, what other ways can we make our code more modular and reusable?
Let’s put it in the context of the code below:
This is a very rudimentary example, but I have found a lot in my experience as a front-end engineer that when writing code, sometimes there is not a thought to abstract something that could be calculated / used somewhere else.
How might we make the above code a bit more abstract? Or how might we break out some functionality to be able to be used elsewhere?
Take this modified code sample for example:
The above takes the hard-to-read bit in the
console.log() from the first script block and makes it easier to read and makes it so we can call it anywhere.
Writing code in a way that is very easy to read is caring for your fellow developer, and it makes it easier for you to come back to in a few months and not want to pull your hair out!