Things I Do To Write Clean Code

Manoj Mohanty
4 min readNov 3, 2021

--

As software developers, we all know how important is clean code and it should be one of the important things as professionals we should always strive for. We tend to follow various architecture patterns, design patterns, tools, frameworks to make sure the code we write is easy to understand, change and fix. Here are small things that can be done to make sure the code is clean and easy to change.

Change is constant with Software Development it is even more evident with clean code it becomes easier for all to make changes and ship changes faster.

Let Functions Do What They Say:

You might have come across code where function reads something like savePhoneNumberToDB ( phoneNumber) but they tend to do more than what it means, it can have code that validates if the input is a valid number or not and other validations but as the name suggests it should do only do one thing that is to save the phone number and that’s it.

Don’t Pass Too Many Arguments To Functions:

Passing too many arguments to a function is an indication that the function is doing too much work which should be avoided and broken down into logical pieces. If the arguments are related then it should be converted to an object and passed as a parameter e.g. name, phoneNumber, email, etc can be clubbed into one and passed. There is no rule of how many arguments should be passed but the less the better I avoid more than three arguments whenever possible.

Avoid Global Mutable variables:

The issue with global mutable variables is they can be updated anywhere in the code which makes it difficult to debug and especially when working in the multi-threaded code base.

Let The Code Be Easy To Understand Rather than Using Comments/Avoid Comments:

We all know comments are important to understand code blocks but the issue with comments is as time passes the comments are not updated based on changes inside the block and can be misleading to new people who try to understand what is happening with the code by reading comments. If you write comments make sure it is updated. Comments are necessary when indicating deprecation of functions, class, etc, indicating the working of the code block in specific platform versions, etc.

If Possible Use Wrapper Around Libraries/Avoid tight coupling with Libraries:

As a developer, we like to use many libraries which makes our life easier when it comes to application development, but it’s important to understand they shouldn’t be everywhere in the code. As libraries are open source they might not be maintained, there might be better alternatives or could be any other reason and you plan to use a new library in this scenario you need to remove/replace all the references in the code for the existing library e.g. In Android we have Glide/Picasso/Coil and other libraries for dealing with images if we used Picasso all over the app and planned to remove it then we need to refactor all the implementations, having a Wrapper around maybe something like ImageLoaderHelper which wraps all the work the library does it would be way easier to replace Picasso with Glide because now we need to update the implementation in ImageLoaderHelper without impacting our code.

Use Constants/Avoid hardcoding:

If you are using some data it can be a string, integer value more than once it’s always better to use constants. Remember replacing one constant is always easier compared to doing find replace in across project

Move Duplicate Code To Common Function (DRY):

If you see some code is being duplicated it’s always better to move it into a common function (many tend to copy an existing block and use it directly just because it wasn’t already available via a function and it goes on)

Don’t Get Into Comparison Which Architecture Is Better MVC vs MVP vs MVVM etc

There are many architectures it is not useful to compare but it’s good to have an understanding of all. At times these are platform/ide tools dependent e.g. Android with ViewModel support made life easier to some extent for some scenarios and the majority of them like MVVM what if ViewModel is deprecated down the line then what? Will they go back to MVP?. So it is important to decide based on what fits right for you not what others say and how easy it is to add or remove stuff with time. Think beyond frameworks and tools when selecting architecture. And remember the above architecture solves how to deal with data with respect to UI so what ever architecture you go with club it with something like Clean Architecture

Other things

  • Try to clean up code when you see something can be improved while doing your code changes
  • Don’t add libraries for small things and make sure already added libraries are updated
  • Follow SOLID Design principles and TDD if possible
  • Its good to be early adopters of new things but don’t try them in production code until you are completely sure. Don’t use something just because everywhere you see it happening do it only if it adds value.
  • Learn from other’s codes, try to question and find answers for them discuss with people who are interested.
  • Take code reviews seriously.
  • Write code with thought process that not only you but others will edit it down the line.
  • Drink lots of water, take care of your health, spend time in nature, share your tips/feedback in comments and also this article 😛 if you find it useful.

--

--

Manoj Mohanty

Android Engineer @PayPay | Ex Rapido, Medibuddy, Carnot Technologies, Deloitte