How To Write Clean Code

Kemal Talha KOÇ
2 min readDec 26, 2021

In this blog post, I will talk about the requirements of writing clean code in bullet points.

1-Pay Attention To SOLID Principles

Firstly, I want to talk about SOLID Principles. SOLID principles are the first letters of the 5 basic principles used in software design. The principles make codes maintainable, smell and flexible. The principles are:

Single Responsibililty-All classes and methods should have a single responsibility.

Open Closed- Written codes should be closed to change and open to extension.

Liskov Substitution -Derived classes must be able to use all the properties of the base classes.

Interface Segregation -It is the principle of differentiating the interfaces for each client.

Dependency Inversion -It is the communication of classes with each other over abstract classes instead of concrete classes.

2- Knowing Object Oriented Principles Well

Secondly, object oriented principles are very important for smell code. The principles make codes reusable, flexible and modular. Object Oriented basic principles are:

Encapsulation -Encapsulation is to protect the methods and properties of classes from external effect and misuse.

Abstraction -Abstraction is the ability of classes to hide behind abstract classes.

Polymorphizm -Polymorphism is the define of the common interface for different purposes of different types of classes.

Inheritance -Inheritance is the transfer of all properties of a class by inheriting from another class.

3-Using Design Patterns

Thirdly, desing pattens make codes maintable, flexible, understandable. Major design patterns such as factory, builder, singleton, strategy should definitely be learned and used. By using these patterns, complex ‘if else’ problems should be solved.

4-Learning Architecture

Fourthly, The architecture used should be studied and its details should be learned well. Unless necessary, the architectural foundations should not be exceeded.

5-Naming

Fiftly, While coding, try to use understandable variable names.Comment lines should not be used unless absolutely necessary. What will be explained with the comment line should be explained with the variable and method names.

6-Writing Testable Codes

Finally, if testable code is written, the other principles mentioned above are also applied. Unittest should be written for all codes written. In addition, test driven development is a widely used and recommended methodology.

--

--

Kemal Talha KOÇ

I am a software engineer. I am trying to develop easy to maintain, clean, understandable, sustainable codes. I like software and i like to write about it.