Thursday, June 12, 2008

mysterious smells

One of our problems at this company is the need to write, re-write, and edit a huge amount of the code base really fast. Since we have demos, releases and branches coming up in a few weeks (less now that I'm going to be going back to Japan soon), there is a feeling of muted urgency.


The good news: we're pretty good at doing what is needed very quickly.

The bad news: our code is starting to smell.


What do I mean by smell? A 'code smell' is a term I just learned the other day. Basically it's a combination of less-than-optimum coding practices, usually introduced by a lack of refactoring, or using procedural programming techniques (because they're more intuitive/easier to implement) when in the long run the code would be much easier to maintain and understand using object oriented principles.


Here is a list stolen from http://wiki.java.net/bin/view/People/SmellsToRefactorings
(which in turn was summarized from the classic book "Refactoring" by Kent beck.)















































SmellDescriptionRefactorings
Comments Should only be used to clarify "why" not "what".

Can quickly become verbose and reduce code clarity.
Extract Method

Rename Method

Introduce Assertion

Long Method The longer the method the harder it is to see what it’s doing.
Extract Method

Replace Temp with Query

Introduce Parameter Object

Preserve Whole Object

Replace Method with Method Object

Long Parameter List Don't pass in everything the method needs; pass in enough so that the method can get to everything it needs.
Replace Parameter with Method

Preserve Whole Object

Introduce Parameter Object

Duplicated Code
Extract Method

Pull Up Field

Form Template Method

Substitue Algorithm

Large Class A class that is trying to do too much can usually be identified by looking at how many instance variables it has. When a class has too many instance variables, duplicated code cannot be far behind.
Extract Class

Extract Subclass

Type Embedded in Name Avoid redundancy in naming. Prefer schedule.add(course) to schedule.addCourse(course)
Rename Method

Uncommunicative Name Choose names that communicate intent (pick the best name for the time, change it later if necessary).
Rename Method

Inconsistent Names Use names consistently.
Rename Method

Dead Code A variable, parameter, method, code fragment, class, etc is not used anywhere (perhaps other than in tests). Delete the code.
Speculative Generality Don't over-generalize your code in an attempt to predict future needs.
If you have abstract classes that aren't doing much use Collapse Hierarchy

Remove unnecessary delegation with Inline Class

Methods with unused parameters - Remove Parameter

Methods named with odd abstract names should be brought down to earth with Rename Method



There are more on that site, and even more in the book. I should really buy it. But it might just sit on the shelf like my 'Design Patterns' book. Oh well, at least my coworker found use for it and has been semi-permanently borrowing it.
But I digress.

No comments: