Home  /  Questions  /  Question



122   96.7
Mar 04, 2011


Correcting Code Smells with Design Patterns?

I think I understand what a code smell is (don't you love that term? -- reminds me of fish for some reason).

Here is what Wikipedia has to say about these smells:

A code smell is any symptom in the source code of a program that possibly indicates a deeper problem.
Often the deeper problem hinted by a code smell can be uncovered when the code is subjected to a short feedback cycle where it is refactored in small, controlled steps, and the resulting design is examined to see if there are any further code smells that indicate the need of more refactoring. From the point of view of a programmer charged with performing refactoring, code smells are heuristics to indicate when to refactor, and what specific refactoring techniques to use. Thus, a code smell is a driver for refactoring.


So, then it comes to refactoring.  Here is what Wikipedia has to say about refactorings:

Code refactoring is a disciplined way to restructure code, undertaken in order to improve some of the nonfunctional attributes of the software. Typically, this is done by applying series of refactorings, each of which is a (usually) tiny change in a computer program's source code that does not modify its functional requirements.

What I am not clear about is what role Design Patterns are playing in these refactoring. I have heard of Refactoring Patterns, but how do they relate to the GoF patterns and say the MVC, MVP, MVVC family of patterns?  Is there a place for these patterns in refactoring the code smells?

Thanks for your thoughts and feedback.



1,364   100.0
Mar 06, 2011
There is no direct relation of Refactoring patters (RF) with GOF Design Patterns (GoF).  In general correct usage of GoF design patterns may to some extent reduce the code smell (overuse may have the side effect).

But Refactoring  patterns are more at a basic level like

- Extract interface
- Extract Parameters
- Encapsulate Field
- Replace constructor with Factory Method (GoF Factory Method)
- Replace Magic Number with Symbolic Constant
- Rename Method
- Substitute Algorithm (GoF Strategy)
- AND MANY MORE.....

Adhering to basic GoF and related patterns like MVC, MVP etc follows some of the above high level objectives, but refactoring includes lots of low level code activity as well as can be gauged from the above list.

Hope this helps.