Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Introduction to Clean Code and Software Design Principles

Gaurav Chandak
Gaurav Chandak

Clean Code and Design Principles Complete Guide

  • Introduction to Clean Code and Software Design Principles
  • Writing Meaningful Variable Names
  • Designing Good Functions and Classes
  • Software Design Principles - I (DRY, YAGNI, KISS, etc)
  • Software Design Principles - II (Abstraction, Extensibility, Cohesion)
  • Software Design Principles - III (SOLID Principles)

Code is clean when it is easily readable by developers other than the original author as well.

Writing clean code might seem like an overhead at the beginning. But it allows us to maintain and develop a piece of software fast in the long term. I've created this series of articles by building on the ideas discussed in the book 'Clean Code'. Clean Code is the bible of good coding practices written by Robert C. Martin (Uncle Bob).

Why should you write clean code?

We as developers write code for the computer to understand. At the same time, our code is not going to remain the same as we update the business logic or add new features. Our code might get updated by other developers as well. Hence, we must write code in such a way that other developers can also read and understand it.

How to write clean code?

We can write clean code by following a set of guidelines known as Software Design Principles. Software Design Principles is a set of guidelines proven to work over the years.

A few of the broad guidelines to write clean code are:

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

I've listed down some of the most popular and useful design principles. You should try to enforce them in your code with every iteration.

  • Avoid code repetitions (DRY).
  • Keep the code simple (KISS).
  • Hide implementation details (Abstraction).
  • Keep the code extensible (Extensibility).
  • Keep the code modular with minimal overlap (Separation of Concerns).
  • Each module should do only one thing (Curly's law).
  • Avoid unnecessary functionalities (YAGNI) and preoptimization (the root of all evil).
  • Stay consistent and avoid surprises in the code (Principle of least astonishment).
  • Make the code better than how you found it (Boy-Scout Rule).

There are many more guidelines/principles. We will be discussing the most popular principles in the upcoming sections/articles.

A set of principles known as SOLID Design Principles have been derived from various design principles. The SOLID Design Principles are:

  • The Single-responsibility principle: "There should never be more than one reason for a class to change." In other words, 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, [not] concretions."

We will learn more about the SOLID principles in further articles.

References: Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (Uncle Bob)

---

Read next

  • Writing Meaningful Variable Names
  • Designing Good Functions and Classes
  • Software Design Principles - I (DRY, YAGNI, KISS, etc)
  • Software Design Principles - II (Abstraction, Extensibility, Cohesion)
  • Software Design Principles - III (SOLID Principles)
3
Gaurav Chandak
Gaurav Chandak
Gaurav is the co-founder of workat.tech and has previously worked at Flipkart and Microsoft. He intends to actively contribute to the future of education through workat.tech.
Related Content
How to Write Meaningful Variable Names? | Writing Clean Code
How to Design Good Functions and Classes | Writing Clean Code
Software Design Principles (Basics) | DRY, YAGNI, KISS, etc
Software Design Principles | Abstraction, Extensibility, Cohesion
SOLID Design Principles | Software Design Principles | Uncle Bob
Machine Coding Round Practice Questions for Interviews | Flipkart, Uber, Swiggy, Udaan, Gojek
SDE Bootcamp - Become a software engineer at a product-based company
Practice Machine Coding
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet