in

ASP.NET Weblogs

This Blog

Syndication

Graphics

ZZZ-Subscribe to this Weblog

Tobler.SoftwareArchitecture()

John Tobler's somewhat ordered collection of thoughts and resources mostly related to software architecture and software engineering.

[Laws] John Tobler's "Three Strikes" Refactoring Rule

So many lines of code written in various languages for so many systems in so many industries; so many reworked programs, procedures, methods, and classes; so many code reviews -- finally, I have boiled down one of my pet peeves into another rule statement that I can claim as my own.  Here is

John Tobler's "Three Strikes" Refactoring Rule:

"Refactor to avoid mindless repetition; if you have done the same thing three times within a single context, find a better way!"

Restatement of John Tobler's "Three Strikes" Refactoring Rule:

"If you have done the same thing more than three times within a single context, you have incarcerated yourself and your company in a mental prison for the rest of your natural (or unnatural) lives."

Enjoy!

Comments

 

Pawel Pabich said:

I would even replace "three times" with "twice". If you repeat your code it means that something is wrong and I'm deadly serious :).
April 13, 2006 9:52 PM
 

John Tobler said:

Regarding the first comment, perhaps my restatement (above) clarifies the seriousness of my "Three Strikes" Refactoring Rule!

About the second, Phil Haack indeed sees this problem in a similar way but he refers to it as "the rule of threes," which would put it in with a stew of ambiguity:

http://www.google.com/search?en&q=%22rule+of+threes%22

I also think my "single context" distinction is essential to defining the "Three Strikes" Refactoring Rule correctly.

Thanks for the feedback!
April 14, 2006 3:09 PM
 

John Tobler said:

The Phil Haack comment, above, referred to a comment that has subsequently been withdrawn. The URL where Phil references "the rule of threes" is here:

http://haacked.com/archive/2005/09/19/10231.aspx

Note that Phil's statement of the rule is not really equivalent to mine.
April 14, 2006 3:13 PM
 

John Tobler said:

Here was the comment that somehow went missing:

Sender: Jon Galloway
Url: http://weblogs.asp.net/jgalloway
re: [Laws] John Tobler's "Three Strikes" Refactoring Rule
I like it. Phil recommended a "third time refactor" guideline before, too:
http://haacked.com/archive/2005/09/19/10231.aspx
-----
April 16, 2006 12:45 AM

Leave a Comment

(required)  
(optional)
(required)  
Add