Combine Duplication
Explore how to detect and refactor duplication of fact, logic, and structure in Rails models through test-driven development. Understand practical methods like using constants, instance methods, and encapsulating logic to write cleaner, more maintainable code with custom RSpec matchers.
We'll cover the following...
We need to look out for three kinds of duplication:
-
Duplication of fact
-
Duplication of logic
-
Duplication of structure
Duplication of fact
Duplication of fact is usually easy to see and fix. A common case is a “magic number” used by multiple parts of the code, such as a conversion factor or a maximum value. We saw this in an earlier example with the 21 days we used to calculate velocity. Another common example is a status variable that has only a few valid values, and the list of those values is duplicated:
The remedy for duplication ...