It's a good practice to have a parent class with private methods that are only used by their descendents?

162 Views Asked by At

I'm trying to follow the SOLID concepts and at same time be DRY (not to repeat myself).

I have a parent class that is sharing a method between its subclasses, but this method is not used in the parent class itself:

class ParentClass
  def locals
    { id: ..., result: ... }
  end
  ...
  private

  def method_used_in_descendants  # this method is not used in ParentClass
  end
end

class ChildClass < ParentClass
  def locals
    super.merge { whatever: method_used_in_descendants, one_result: ... }
  end
  ...
end

class AnotherChildClass < ParentClass
  def locals
    super.merge { whatever: method_used_in_descendants, another_result: ... }
  end
  ...
end

# And not all the descendants of ParentClass need method_used_in_descendants:
class AnotherChildClass < ParentClass
  def locals
    super.merge { whatever: my_private_method }
  end
  ...
end

Is this method method_used_in_descendants created in the ParentClass a bad practice?

Thanks

0

There are 0 best solutions below