Value Objects are objects that represent a value (such as a dollar
amount) rather than a unique, identifiable entity (such as a particular
Value objects often implement information derived from a primitive
object, such as the dollars and cents from a float, or the user name and
domain from an email string.
- Prevent duplicated
code from making the same observations of primitive objects
throughout the code base.
- Remove large classes by
splitting out query methods associated with a particular variable.
- Make the code easier to understand by fully encapsulating related
logic into a single class, following the single
- Eliminate divergent
change by extracting code related to an embedded semantic type.
InvitationsController is bloated with methods and logic
relating to parsing a string that contains a list of email
@recipient_list ||= recipients.gsub(/\s+/, '').split(/[\n,;]+/)
We can extract a new
class to offload this responsibility:
@recipient_string = recipient_string
@recipient_list ||= RecipientList.new(params[:invitation][:recipients])
- Search the application for duplicated code related
to the newly extracted class.
- Value objects should be immutable. Make sure the extracted class
doesn’t have any writer methods.
The canonical reference for writing fantastic Rails applications from authors who have created hundreds.
Work with us to make a new Rails app, or to maintain, improve, or scale your existing app.