Writing Clean Code….📝

Sri Priya P Kulkarni
Women in Technology
3 min readSep 4, 2023

--

Coding

Code is said to be clean when it is easily read by other developers other than the original author as well.

Writing clean code might be bit challenging at the beginning. But it allows us to maintain and develop a piece of software fast and robust in the long run.

Why should you write clean code?

We as Software Engineer write code for the computer to understand. At the same time, our code is not the same as we update the business logic or add and remove new features. Our code might get updated by other software engineers as well. Hence, we must write code that is readable and understandable by others.

How to write clean code?

We can write clean code by following a set of guidelines and principles known as Software Design Principles. Software Design Principles are a set of guidelines that helps us to write the code clean , readable and understandable.

A few of the guidelines to write clean code are:

  • Create functions that are small and do a single responsibility.
  • Structure the code for better readability.
  • Keep related code together and keep the lines smaller with proper comments.
  • Encapsulate related data and functions into small independent classes.
  • Enhance readability with proper comments.
  • Give meaningful names to variables, functions, classes, and other entities in the code.
  • Write readable, fast, independent, and repeatable test classes or methods.

Below are some of the popular and most useful software design principles:

  • Avoid code repetitions across the project (DRY).
  • Hide implementation details and show only required implementation(Abstraction).
  • Keep the code extensible so that it code can be added in future by others (Extensibility).
  • Keep the code modular with minimal overlap (Separation of Concerns)
  • Each module should do only one thing or one responsibility (Curly’s law).
  • Avoid unnecessary functionalities (YAGNI) and preoptimization (the root of all evil).
  • Keep the code simple (KISS).
  • Stay consistent and avoid surprises in the code (Principle of least astonishment).
  • Make the code better than how you found it (Boy-Scout Rule).

The SOLID principles are a set of golden rules that aim to improve the design and maintainability of software.

The SOLID Design Principles are:

  • The Single-responsibility principle: “There should never be more than one reason for a class to change.” Every class should have only one responsibility.
  • The Open–Closed principle: “Software entities … should be open for extension, but closed for modification.”
  • The Liskov substitution principle: “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it”.
  • The Interface segregation principle: “Many client-specific interfaces are better than one general-purpose interface.”
  • The Dependency inversion principle: “Depend upon abstractions,but not concretions.”

Let us see the above SOLID principles in the next article.

References: Clean Code is the bible of good coding practices written by Robert C. Martin (Uncle Bob).

Thanks for reading my article, if you have liked , please share it across.

--

--

Sri Priya P Kulkarni
Women in Technology

SDET| Blogger! | Automation Enthusiast! | On a journey of Continuous learning.... !