Upgrade to Pro — share decks privately, control downloads, hide ads and more …

David Chelimsky, RubyConf 2010: Maintaining bal...

David Chelimsky, RubyConf 2010: Maintaining balance while reducing duplication

Benjamin Fleischer

November 13, 2010
Tweet

More Decks by Benjamin Fleischer

Other Decks in Programming

Transcript

  1. “Duplicate code is the root of all evil in software

    design.” Saturday, November 13, 2010
  2. Every piece of knowledge must have a single, unambiguous, authoritative

    representation within a system Saturday, November 13, 2010
  3. Every piece of knowledge must have a single, unambiguous, authoritative

    representation within a system Saturday, November 13, 2010
  4. Every piece of knowledge must have a single, unambiguous, authoritative

    representation within a system Saturday, November 13, 2010
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. "/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
  11. "/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
  12. 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
  13. 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
  14. 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
  15. every time you reduce duplication you increase coupling by introducing

    new dependencies Saturday, November 13, 2010
  16. Every piece of knowledge must have a single, unambiguous, authoritative

    representation within a system Saturday, November 13, 2010
  17. every time you reduce duplication you increase coupling by introducing

    new dependencies Saturday, November 13, 2010
  18. OO in one sentence keep it shy, DRY, and tell

    the other guy! Saturday, November 13, 2010