Improving code readability: Abstractions
refactoring
In my previous post, I wrote about using abstractions to improve code readability.
I had a life changing moment when I came across a conference video by Ben Orenstein titled Refactoring from Good to Great. During the talk, he discussed about how abstraction lets you focus on what a code does by hiding it’s implementation details. Therefore, wheoever reads the code can quickly understand what it does by focusing on it’s high-level intent instead of being bothered by implementation details.
Before using abstraction
Code readability is alright and it is easy to follow through what the code does. However, there is room for improvement and we can do so by saving the reader the trouble of reading the implementation details.
After using abstraction
At first glance, the code appears much longer and you will ask the question “Is it actually easier?”
Let me breakdown the 2 things I did here:
- Replacing details of statuses to be returned with a constant
- Pull out validation implementation into a private method
What this means is that when someone is reading what check_out
does, they are able to understand what it does faster. Only if they wish to understand what the statuses are or how the implementation of the different validations are being done, are they required to look into the private methods.
Other benefits of abstraction…
Even though the purpose of this post is to share more about how abstraction helps with readability, I thought it would help if I briefly share about the other reasons why you should abstract aggressively.
Reusability
DRY principle is one of the best practices talked about by developers (Although I highly recommend Dan Abramov’s talk on The WET Codebase). This means that you are able to reuse the status STATUS_EMPTY_CART
or methods like is_cart_empty
in other part of your code.
Easier to change the code
One of the reasons why we care about readability is because we would like to be able to make changes to our code quickly without breaking anything. By abstracting these logics into separate methods, we are able to do so much faster. For example, if you wish to change how are_products_unavailable
is being implemented, you can do so quickly and confidently as it has been isolated out into a standalone method.
A little about what I do at NodeFlair…
The world today runs on code written by developers that solve the world’s problems and impact lives.
Now, imagine a world where developers get to code at a place where they find purpose in their work. This meaning could translate into drive that pushes boundaries to solve more of the world’s problems.
That’s why at NodeFlair, we make it our mission to improve the world by empowering developers to code() at where they love.