Eneroth3
Eneroth3

Reputation: 1297

Is there an established name for this anti-pattern?

Quite often I see code that use redundant properties for data, when one value could have been calculated on the fly from two others. To me it is an obvious anti-pattern, as it makes further work on the code much harder. Every time you change one value you are expected to know how it relates to other values and change those too. If you mess this up, there is an ambiguous situation where you don't know what values are correct and what aren't.

However, I don't know if this is a "known" anti-pattern with an established name.

# Bad
class Rectangle
  attr_accessor: height
  attr_accessor: width
  attr_accessor: ratio

  def initialize
    @height = 1
    @width = 1
    @ratio = 1
  end
end

# Good
class Rectangle
  attr_accessor: height
  attr_accessor: width

  def initialize
    @height = 1
    @width = 1
  end

  def ratio
   @width.to_f / @height
  end
end

Upvotes: 0

Views: 64

Answers (1)

DevSolar
DevSolar

Reputation: 70263

Basically a violation of don't repeat yourself?

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.


Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for "write every time", "write everything twice", "we enjoy typing" or "waste everyone's time".

Upvotes: 1

Related Questions