DEV Community

loading...
Cover image for Reading Code: How to Write Abstract and Reusable Code

Reading Code: How to Write Abstract and Reusable Code

Ikechukwu
Writer of Code: PHP/Laravel open for work mr.ikunegbu@gmail.com
・3 min read

A code that is abstract and reusable is code not tied to unique details of the problem set when writing it. For instance, if you are building an application with core PHP, and have about ten tables in your database, each with its unique set of columns. Imagine having to write insert method for each of these tables, and then update methods for each. That will work but it is tedious and more prone to bugs.

How about you have a single insert method that when supplied with table name and inputs, viola it inserts the table in the table. Just any table at all-that is to say that this insert method is table agnostic. Behind the scene, this method is a dynamic MySQL query builder. Now, this is abstract and reusable code. This is how frameworks work.

In my early days in this business, I heard one developer say he doesn’t use frameworks, I was blown away, well it is simpler than I imagined (I am not advising you not to be using frameworks). He has his own set of reusable codes he uses in all projects.
Ability to write code this way is the hallmark of a professional or senior developer.

The question is, as a newbie, how do you get to this level in no time? There are two ways:

  1. Give it time. Continue writing code tied to unique details of the problem set while trying as much as possible to follow the DRY principle. Over time you will be able to synthesize a pattern out of it all. Then write abstract reusable code based on the patterns. The whole pattern wouldn’t emerge at once though.
    The problem now is that this will take you years and useful hours wasted. You don’t have time. So, what is the next option?

  2. The next option is: READ FINE CODEBASES. Yeah, read codebases where the developer wrote abstract reusable code. Your best bet is to find a codebase of a mini framework and read the hell out of it. Start with a mini framework meant for learning purposes. That will advance you fast. From there move to open source, production-grade lightweight frameworks.

Caution: you won’t understand it enough from your first reading to replicate or even integrate its concepts into your project. Even if you type along, you may still not understand it the first time. Maybe not even the second time. However, this is your best bet to drastically improve the quality of your code in next to no time.
Number two was how I did it. Most importantly, reading codes especially ones better than yours will improve your skills all around as a developer more than writing code can do.
What does you as a developer stand to benefit by writing abstract reusable code?

I would have told you that, it is the most professional way to do things, but I won’t. Humans are animals, driven more than anything else, by incentive and material gain. So here is what you stand to gain…
As an independent or employed developer, it makes your work faster so you can take on more work. More efficient as there will be a single or central point of failure. It is one of the skills you will need to work with a giant team. Each developer working on a different part of the project don’t and won’t have to reinvent the wheel. I will hope you don’t need me to spell out how this will turn into more money for you. Thank you for reading.

Discussion (0)