Value Objects to the rescue!

Value objects represent typed values, that have no conceptual identity, in your domain. They can help you write better code that is less error-prone, more performant and more expressive. Wow!

Paul Milian
Sep 8, 2019 · 8 min read
A lego super man
A lego super man
Photo by Esteban Lopez on Unsplash

Domain-Driven Design

Image of the book “Domain-Driven Design”
Image of the book “Domain-Driven Design”
the blue book

Main Characteristics

1. Value Equality

2. Immutability

3. Self-Validation

Example of a value object

Benefits

Reduce Primitive Obsession

Image for post
Image for post

Type safety

private float distance;
void sendEmail(String email, String subject, String body);
sendEmail("Some subject", "Some content", "john@doe.com");
sendEmail(EmailAddress recipient, Subject subject, Content content);
sendEmail(Email email);

Flexibility

Validation

Reduce duplication

The meme image of two spider man pointing at each other
The meme image of two spider man pointing at each other

Easier to read

Better performance

Hashing

Drawbacks

Summary

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store