More Depth to Refactoring and Using Design Patterns

Rayhan Muzakki
Pilar 2020
Published in
2 min readNov 20, 2020

I have mentioned in my previous blog(specifically about TDD) and mentioned the outline of refactoring, in this blog I will be writing more about what is refactoring and how to apply refactoring correctly by using design patterns.

Refactoring

Refactoring is the process of clarifying and simplifying the design of existing codes without changing the functionality of the codes.We use refactoring because by writing your code neatly less code smells will appear and this can improve the program’s performance,maintainability, scalability and flexibility. A simple example of refactoring would be applying clean codes to your program, however another application of refactoring will be using design patterns.

Example of refactoring by applying clean code, source: PILAR Project

Design Patterns

Design patterns are repeatable solutions to a commonly occuring problem in a program, design pattern is more like a template to solve a problem that can be used in many different situations. By using design patterns reduces code smell and increases code readability.

There are three types of design patterns commonly used:

1. Creational Pattern

A design pattern that focuses on object creation mechanism, this will improve the flexibility of a function creating an object in the most suitable situation.

2. Structural Patterns

A design pattern that simplifies the way to realize relationship between entities,this will improve code readability and more flexible to changes.

3. Behavioral Pattern

A design pattern that identifies communication patterns between objects, by using the pattern this will increase flexibility in carrying out this communication.

A Different Kind of Pattern- MVT

Model-View-Template(MVT for short) is a design pattern that is used in Django to develop web applications, the difference from the patterns above is that the controller part is taken care for us by the framework itself and MVT will divide into different files and these files will function as a type of pattern. An example would be comparing between observer pattern and a Django component Signals, both patterns identifies the communication pattern between objects.

Conclusion

I recommend apply refactoring to your code because refactoring can improve the program’s performance,maintainability, scalability and flexibility. Design pattern can be used in order to reduce code smells and improve code readability.

--

--