I was introduced to “Coding by Abstraction” at a very early stage in my career. It got my attention because I realized it’ll enable me to neatly structure my code. Coding is hardly a “snap the finger” magical transformation. It needs a focused approach to learn coding and understand how this aligns well with unit testing.
Implementing Coding with Abstraction sets up a developer for the journey into application/class design because once you develop well-structured code, your thought process is more aligned to the design principles in application design.
So, what is coding by abstraction?
It encourages the developer to focus on the main logic in the flow being coded and ensure that the sub-flows are parked till the main flow completes - sounds "abstract," doesn't it?
Let’s take an example. Here, we are using java snippets for the examples below:
We have a service method that gets a http request, we need to enrich this with additional information, place this enriched message into a message Q and finally respond back to the sender. The assumption in this example is that each step is a significant unit of work.
Choosing the right approach
One approach is to code the above flow in the exact sequence one after another, switching back between functional and infrastructure code, diving into exceptions, retries etc.
The developer has already baked in the notion that a database and Kafka will be used and codes the methods one after another assuming this will give them clarity on what needs to be handled in the following steps. But as you’ll see below, the design thought process does not start at classes and thereafter systems, it starts with code.
The “coding with abstraction” approach is to first focus on the main functional flow, abstracting out the infrastructure and sub-flow implementations such as reading from a DB, putting a message into a Message Q and additional processing for enrichment.
The next steps
The reason why you want to define interfaces and their implementations when working with infrastructure components is because you might need to change from, say, RDBMS to NoSQL DB or use a distributed cache.
You can take the same approach for sendMessage. This approach will enable you to structure your code development and unit testing. It also encourages good design while enrichingyour coding and design thought process.
With that,I hope now you will be able to implement a code development approach that encourages cleaner code structure and better unit testing.