Writing good clean code is a work of art that takes a lot of practice and understanding. One of the first practices you should use is what’s called design smells. Design smells are indicators of possible poor design principles that will eventually impact the quality of your project.
We’ve all had that point when we first started learning how to write code where we wrote some pretty ugly “spaghetti” code. To start, the names of these smells are rigidity, fragility, immobility, viscosity, needless complexity, needless repetition and opacity. Most of these tend to go hand in hand or could stand alone.
It’s good to know their meanings before talking about their importance.
Rigidity: when your software is difficult to change in even the simplest ways.
Fragility: having a tendency for your program to break in other places when you make a change.
Immobility: having useful code that could be used in other systems, but can’t be integrated very easily
Viscosity: when it’s not easy to make only one change.
Needless complexity: containing things that don’t really have any use yet.
Needless repetition: repeated code that could be abstracted instead or written over and over.
Opacity: not visually clear.
While the use of being able to identify these design smells may be obvious to some. We will start with some examples before ending with why identifying these smells will be helpful in the long run.
When first starting a project sometimes it’s hard to know where to really start. That is why developers have tools such as class diagrams, but that is for another blog. Starting off with no real design in mind it’s easy to have needless repetition pop up. Once you’ve identified you can break certain things off and turn them into their own methods, you may also realize it has caused rigidity and fragility. Moving those lines might be much harder than you think. Those problems also lead into viscosity if you end up with a big enough mess (speaking from experience).
Next is the problem of needless complexity. Without having a class diagram you might find yourself jumping ahead to try and fix a problem or do something you don’t have a need for yet. The problem is exacerbated if you don’t end up needing it at all. No one likes useless code. This can lead to opacity when your code is making the goals and intents of your work unclear.
With the previously discussed class diagrams (see previous blog) to guide you. Along with an understanding of poor design principles it’s easy to see how being able to identify these smells can keep your work in much better shape for yourself and others. We have stuck with the design process for now, a future blog topic will cover how to make the code itself “cleaner”.
From the blog Mikes CS 343 by Michael St. Germain and used with permission of the author. All other rights reserved by the author.