Share
Embed
David Chelimsky, RubyConf 2010: Maintaining balance while reducing duplication
Slide source: http://web.archive.org/web/20101218001604/http://blog.davidchelimsky.net/2010/11/13/rubyconf-slides-2/
These slides: adapted by me to highlight my favorite DRY anti-example ever
Video: http://www.confreaks.com/videos/434-rubyconf2010-maintaining-balance-while-reducing-duplication
Benjamin Fleischer
November 13, 2010
Transcript
55 slides
-
This talk is about duplication About duplication, is this talk
Saturday, November 13, 2010 -
“Duplicate code is the root of all evil in software
design.” Saturday, November 13, 2010 -
Every piece of knowledge must have a single, unambiguous, authoritative
representation within a system Saturday, November 13, 2010 -
Every piece of knowledge must have a single, unambiguous, authoritative
representation within a system Saturday, November 13, 2010 -
Every piece of knowledge must have a single, unambiguous, authoritative
representation within a system Saturday, November 13, 2010 -
ALLOCATIONS = "allocations" ALLOCATIONS_ON_DATE = ALLOCATIONS + "/{sourceType}/on/{year}/{month}" CREATE_ALLOCATION =
ALLOCATIONS + "/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" DELETE_ALLOCATION = ALLOCATIONS + "/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}" Saturday, November 13, 2010 -
ALLOCATIONS = "allocations" SOURCE_TYPE = "/{sourceType}" ALLOCATIONS_ON_DATE = ALLOCATIONS +
SOURCE_TYPE + "/on/{year}/{month}" CREATE_ALLOCATION = ALLOCATIONS + SOURCE_TYPE + "/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" DELETE_ALLOCATION = ALLOCATIONS + SOURCE_TYPE + "/{sourceId}/{targetType}/{targetId}/{year}/{month}" Saturday, November 13, 2010 -
ALLOCATIONS = "allocations" SOURCE_TYPE = "/{sourceType}" SOURCE_ID = "/{sourceId}" TARGET_TYPE
= "/{targetType}" TARGET_ID = "/{targetId}" EFFECTIVE_DATE = "/{year}/{month}" ALLOCATIONS_ON_DATE = ALLOCATIONS + SOURCE_TYPE + "/on" + EFFECTIVE_DATE CREATE_ALLOCATION = ALLOCATIONS + SOURCE_TYPE + SOURCE_ID + TARGET_TYPE + TARGET_ID + EFFECTIVE_DATE + "/{percentage}" DELETE_ALLOCATION = ALLOCATIONS + SOURCE_TYPE + SOURCE_ID + TARGET_TYPE + TARGET_ID + EFFECTIVE_DATE Saturday, November 13, 2010 -
DELIMITERS = %w[/ { }] ALLOCATIONS = "allocations" EFFECTIVE_DATE =
DELIMITERS[0..1].join + "year" + DELIMITERS[2] + DELIMITERS[0..1].join + "month" + DELIMITERS[2] SOURCE = "source" TARGET = "target" ID = "Id" TYPE = "Type" SOURCE_TYPE = DELIMITERS[0..1].join + SOURCE + TYPE + DELIMITERS[2] SOURCE_ID = DELIMITERS[0..1].join + SOURCE + ID + DELIMITERS[2] TARGET_TYPE = DELIMITERS[0..1].join + TARGET + TYPE + DELIMITERS[2] TARGET_ID = DELIMITERS[0..1].join + TARGET + ID + DELIMITERS[2] ALLOCATION_SOURCE = SOURCE_TYPE + SOURCE_ID ALLOCATION_TARGET = TARGET_TYPE + TARGET_ID ALLOCATIONS_ON_DATE = ALLOCATIONS + SOURCE_TYPE + DELIMITERS[0] + "on" + EFFECTIVE_DATE CREATE_ALLOCATION = ALLOCATIONS + ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE + DELIMITERS[0..1].join + "percentage" + DELIMITERS[2] DELETE_ALLOCATION = ALLOCATIONS + ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE Saturday, November 13, 2010 -
def token(string) "/{#{string}}" end def allocation_path(path) "allocations#{path}" end EFFECTIVE_DATE =
token("year") + token("month") SOURCE = "source" TARGET = "target" ID = "Id" TYPE = "Type" SOURCE_TYPE = token(SOURCE + TYPE) SOURCE_ID = token(SOURCE + ID) TARGET_TYPE = token(TARGET + TYPE) TARGET_ID = token(TARGET + ID) ALLOCATION_SOURCE = SOURCE_TYPE + SOURCE_ID ALLOCATION_TARGET = TARGET_TYPE + TARGET_ID ALLOCATIONS_ON_DATE = allocation_path(SOURCE_TYPE + "/on" + EFFECTIVE_DATE) CREATE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE + token("percentage") ) DELETE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE ) Saturday, November 13, 2010 -
"/allocations/developer/37/project/2010/10/42" def token(string) "/{#{string}}" end def allocation_path(path) "allocations#{path}" end EFFECTIVE_DATE
= token("year") + token("month") SOURCE = "source" TARGET = "target" ID = "Id" TYPE = "Type" SOURCE_TYPE = token(SOURCE + TYPE) SOURCE_ID = token(SOURCE + ID) TARGET_TYPE = token(TARGET + TYPE) TARGET_ID = token(TARGET + ID) ALLOCATION_SOURCE = SOURCE_TYPE + SOURCE_ID ALLOCATION_TARGET = TARGET_TYPE + TARGET_ID ALLOCATIONS_ON_DATE = allocation_path(SOURCE_TYPE + "/on" + EFFECTIVE_DATE) CREATE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE + token("percentage") ) DELETE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE ) Saturday, November 13, 2010 -
"/allocations/developer/37/project/2010/10/42" ALLOCATIONS_ON_DATE = "allocations/{sourceType}/on/{year}/{month}" CREATE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" DELETE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}"
"allocations/{sourceType}/on/{year}/{month}" "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}" Saturday, November 13, 2010 -
def token(string) "/{#{string}}" end def allocation_path(path) "allocations#{path}" end EFFECTIVE_DATE =
token("year") + token("month") SOURCE = "source" TARGET = "target" ID = "Id" TYPE = "Type" SOURCE_TYPE = token(SOURCE + TYPE) SOURCE_ID = token(SOURCE + ID) TARGET_TYPE = token(TARGET + TYPE) TARGET_ID = token(TARGET + ID) ALLOCATION_SOURCE = SOURCE_TYPE + SOURCE_ID ALLOCATION_TARGET = TARGET_TYPE + TARGET_ID ALLOCATIONS_ON_DATE = allocation_path( SOURCE_TYPE + "/on" + EFFECTIVE_DATE ) CREATE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE + token("percentage") ) DELETE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE ) ALLOCATIONS_ON_DATE = "allocations/{sourceType}/on/{year}/{month}" CREATE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" DELETE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}" Saturday, November 13, 2010 -
def token(string) "/{#{string}}" end def allocation_path(path) "allocations#{path}" end EFFECTIVE_DATE =
token("year") + token("month") SOURCE = "source" TARGET = "target" ID = "Id" TYPE = "Type" SOURCE_TYPE = token(SOURCE + TYPE) SOURCE_ID = token(SOURCE + ID) TARGET_TYPE = token(TARGET + TYPE) TARGET_ID = token(TARGET + ID) ALLOCATION_SOURCE = SOURCE_TYPE + SOURCE_ID ALLOCATION_TARGET = TARGET_TYPE + TARGET_ID ALLOCATIONS_ON_DATE = allocation_path( SOURCE_TYPE + "/on" + EFFECTIVE_DATE ) CREATE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE + token("percentage") ) DELETE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE ) ALLOCATIONS_ON_DATE = "allocations/{sourceType}/on/{year}/{month}" CREATE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" DELETE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}" some duplicate letters/words Saturday, November 13, 2010 -
def token(string) "/{#{string}}" end def allocation_path(path) "allocations#{path}" end EFFECTIVE_DATE =
token("year") + token("month") SOURCE = "source" TARGET = "target" ID = "Id" TYPE = "Type" SOURCE_TYPE = token(SOURCE + TYPE) SOURCE_ID = token(SOURCE + ID) TARGET_TYPE = token(TARGET + TYPE) TARGET_ID = token(TARGET + ID) ALLOCATION_SOURCE = SOURCE_TYPE + SOURCE_ID ALLOCATION_TARGET = TARGET_TYPE + TARGET_ID ALLOCATIONS_ON_DATE = allocation_path( SOURCE_TYPE + "/on" + EFFECTIVE_DATE ) CREATE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE + token("percentage") ) DELETE_ALLOCATION = allocation_path( ALLOCATION_SOURCE + ALLOCATION_TARGET + EFFECTIVE_DATE ) ALLOCATIONS_ON_DATE = "allocations/{sourceType}/on/{year}/{month}" CREATE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}/{percentage}" DELETE_ALLOCATION = "allocations/{sourceType}/{sourceId}/{targetType}/{targetId}/{year}/{month}" some duplicate letters/words #!(*&?$@ Saturday, November 13, 2010 -
DRY does not mean “don’t type the same characters twice”
Saturday, November 13, 2010 -
every time you reduce duplication you increase coupling by introducing
new dependencies Saturday, November 13, 2010 -
it is important to depend on the right things Saturday,
November 13, 2010 -
depend on things which are less likely to change Saturday,
November 13, 2010 -
How do I know if this particular duplication is problematic?
Saturday, November 13, 2010 -
this can make it harder to reason about code Saturday,
November 13, 2010 -
Metaprogramming is helpful when you can’t know the runtime conditions
Saturday, November 13, 2010 -
Metaprogramming can also make it harder to reason about code
Saturday, November 13, 2010 -
Many problems in software can be traced back to duplication,
but ... Saturday, November 13, 2010 -
Every piece of knowledge must have a single, unambiguous, authoritative
representation within a system Saturday, November 13, 2010 -
This is not really about duplicated strings of characters Saturday,
November 13, 2010 -
every time you reduce duplication you increase coupling by introducing
new dependencies Saturday, November 13, 2010 -
depend on things which are less likely to change Saturday,
November 13, 2010 -
Duplicated Code Feature Envy Inappropriate Intimacy Large Class Long Method
Saturday, November 13, 2010 -
•Single Responsibility •Open/Closed •Liskov Substitution •Interface Segregation •Dependency Inversion SOLID
Principles Saturday, November 13, 2010 -
OO in one sentence keep it shy, DRY, and tell
the other guy! Saturday, November 13, 2010