How to improve code quality in simple language

Writing good quality code is an art in itself. In this article I’ll attempt to explain what I’ve learnt about code quality enhancement throughout my software engineering career. Lets get started.

Lets say one day your friend who doesn’t believe in code quality sends his/her python code to you for reviewing. It looks like:

Were you able to understand what these 2 functions are trying to achieve? Don’t bother yourself by trying to understand this code, as it is unreadable. So how do we make this readable? The first thing which we can do is to improve its formatting and variable nomenclature.

As you can see, simply formatting (yes, whitespaces can be beautiful XD) and renaming the variables has improved code readability by a lot. I think now the first function would be clear, but some of you might have an issue with the second one, to resolve that we can write a small comment which explains about the function. A comment is a part of code which the interpreter ignores and doesn’t execute, it is used to improve the code readability.

So the comment makes everything clear. Now that we’ve understood what the two functions are doing, it would be a good idea to make our code error proof. So how can we do this? To do this we need to think about apocalyptic scenarios which may lead incorrect results. Lets think about some apocalyptic scenarios for our SimpleInterest function.

Lets say due to some issue with our code one of the input to our function, say time becomes negative. One thing which immediately comes to our mind is that this issue would be easy to notice as simple interest calculated would be negative and we can very well rectify the piece of code causing this problem after noticing a negative interest. Things are okay when we get a wrong result after getting a wrong input.
Lets make things a bit harder. Lets assume that there’s an issue with our code which randomly makes our input (principalAmt, interestRate, time) negative. So at a time our function can receive 0 or 1 or 2 or 3 negative values. It would work fine when it receives 0 or 2 negative values (multiplying 2 negative values makes the result positive) while it won’t work fine when we receive 1 or 3 negative values. So this would create a situation where our code would work correctly sometimes in spite of receiving wrong input which makes finding the problem with our code difficult and time consuming. Things start to get mysterious when we get the right result in spite of receiving wrong inputs XD.

So how do we go about making our SimpleInterest function resistant to invalid inputs. A simple conditional statement printing a small message would be the simplest way of handling it. There are many advanced ways handling these scenarios as well like Assertions and Exception Handling but I’ll perhaps discuss about them in my upcoming posts. For now lets handle this in the most easiest way with our conditional statements.

So this finally makes code resistant to any outside errors. I’ll try to cover the issue of code quality in more detail in my upcoming posts. Thanks for reading!