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

Published 13 April 06 03:35 PM | CSharpener

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 on April 13, 2006 09:52 PM:

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

# John Tobler said on April 14, 2006 03:09 PM:

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!

# John Tobler said on April 14, 2006 03:13 PM:

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.

# John Tobler said on April 16, 2006 12:45 AM:

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
-----

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

.NET Languages

Architecture

Blogs I Read

CSharp

DotNet

General

Graphics

Other Languages

Semantic Web

The Human Factor

Tools

Unology

XML

ZZZ-Subscribe to this Weblog

Syndication