Search⌘ K
AI Features

Find Missing Abstractions

Explore how to identify missing abstractions in Rails models by recognizing duplicated structures and shared attributes. Understand when to refactor code into new classes and how this simplifies testing with TDD. Learn to create isolated, easy-to-test objects that reduce dependencies and improve code clarity.

We'll cover the following...

Duplication of structure

Duplication of structure often means there’s a missing abstraction, which in Ruby generally means we can move some code into a new class.

One symptom is a set of instance attributes that are always used together, especially if the same group of attributes is being passed to multiple methods. The use of a common set of variables often indicates that we want a new class with those friendly attributes as the instance attributes.

Another common symptom is a group of methods that all share a prefix or a suffix, such as logger_init, logger_print, and logger_read. Often this means we need a class corresponding to the common word.

In ActiveRecord, one side effect of discovering ...