All images are uploaded from Pixabay.com
Any software developer in the world can write code that the computers can understand but in order to write a code that human as well can understand it easily, that’s only the job for good software developer.
What is clean code in software engineering term?
Clean code are code that can be easily understand by fellow software developer, that is it is able to test actual business logic, it can be refactor easily, small changes won’t affect major things and it is scalable.
Why is clean code is important in the first place ?
Clean code is easily maintainable and extensible which will save a lot of time when it comes to maintenance. We understand what clean code is and why clean code is important but then how can we achieve it ? This article will show you some of the tips that can help you or any software developer to write clean code.
1. Use software design pattern in your code
What is software design pattern ? a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design.
There are 6 important design patterns that must be considered to implement into your code. They are Singleton design pattern, factory method, strategy design pattern, observer design pattern, and builder design pattern.
- Singleton design pattern: is use to limit the creation of a class to only 1 object. It is very useful when one object is needed to use across the system.
- Factory method: just like a factory in real world produce goods, a software factory produce objects. It does so without give the specific class of thee object to be created.
- Strategy design pattern: this design pattern allow related algorithm under an abstractions.
- Observer design pattern: Just as the name imply, this design pattern is a one-to-many dependency between objects as it is observer one object state and notify all other objects about it when there are changes in the object state.
- Builder: builder design pattern is use to build objects. It use several sub-object to create an object.
2. Use SOLID design principles
- S: single-responsibility principle: a should have only one reason to change, or u can say that it should have only 1 job
- O: open-closed principle: object or entities should be open for extension but closed for modification
- L: liskov substitution principle: Every subclass should be substitutable for their parent class
- I: interface segregation principle: a client should never be force to implement an interface that it doesn’t use
- D: dependency inversion principle: entities must depend on abstraction not concretion.
3. Dependency injection
- this is a design pattern used to implement IOC. It is a technique whereby one object suppliers the dependency of another object.
4. Defining a contract with interfaces
- is a methods that implementing classes must support.If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
5. Abstraction with adapter
- adapter is implemented to allow 2 incompatible interfaces to work together. An adapter class convert an interface of one class into an interface that can work with the other interface.