{"id":6511,"date":"2023-02-05T16:06:28","date_gmt":"2023-02-05T16:06:28","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/value-objects\/"},"modified":"2023-02-05T16:06:28","modified_gmt":"2023-02-05T16:06:28","slug":"value-objects","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/value-objects\/","title":{"rendered":"Value Objects"},"content":{"rendered":"<p>A value object is a small object whose equality is based on state, but not identity. Typical value objects are money, numbers, or strings.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6503\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png\" alt=\"ClassIdioms\" width=\"650\" height=\"335\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png 1224w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-300x154.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-1024x527.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-768x395.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The term Value Object goes back to the seminal book <a href=\"https:\/\/www.oreilly.com\/library\/view\/domain-driven-design-tackling\/0321125215\/\" target=\"_blank\" rel=\"noopener ugc nofollow\" class=\"au lc\">Domain-Driven Design<\/a> (DDD) by Eric Evans. But, what is a value object? Eric gave the answer in his book:<\/p>\n<p><em>An object that represents a descriptive aspect of the domain with no conceptual identity is called a Value Object. Value Objects are instantiated to represent elements of the design that we care about only for what they are, not who or which they are.<\/em><\/p>\n<p>If this is too formal for you, here is a nice example of the author:<\/p>\n<p><em>When a child is drawing, he cares about the color of the marker he chooses, and he may care about the sharpness of the tip. But if there are two markers of the same color and shape, he probably won\u2019t care which one he uses. If a marker is lost and replaced by another of the same color from a new pack, he can resume his work unconcerned about the switch.<\/em><\/p>\n<p>The key term in the formal definition and the example about the Value Object is equality.<\/p>\n<h2>Identity<\/h2>\n<p>In general, we have two types of equality: reference equality and value equality. For simplicity reasons, I will ignore id-based equality.<\/p>\n<ul>\n<li><strong>Reference equality<\/strong>: two objects are considered to be equal if they reference the same entity in the memory.<\/li>\n<li><strong>Value equality<\/strong>: two objects are considered to be equal if all their member have the same value.<\/li>\n<\/ul>\n<p>Let me switch to Python because Python makes it easy to compare both kinds of equality.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6508\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/EqualityPythonNew.png\" alt=\"EqualityPython\" width=\"500\" height=\"381\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/EqualityPythonNew.png 1397w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/EqualityPythonNew-300x228.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/EqualityPythonNew-1024x779.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/EqualityPythonNew-768x584.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>The short example in the Python shell should make the difference between reference equality and value equality clear.<\/p>\n<p>First, I define two lists <code>list1<\/code> and <code>list2<\/code> with the same elements. When I compare their identity (<code>list1 is list2<\/code>), they are different. When I compare their values, they are identical. Python uses for equality (and non-equality) comparison of the memory address of the compared objects. The <code>id<\/code> operator (<code>id(liste1)<\/code>) returns a decimal representation of its hexadecimal memory address.&nbsp; By assigning<code> list1<\/code> to<code> list3, <\/code>both lists refer to the same memory location. Consequentially,<code> id(list3)<\/code> is identical to <code>id(list1)<\/code>, and the call <code>list1<\/code> is <code>list3<\/code> returns T<code>rue<\/code>.<\/p>\n<p>What does this mean for modern C++20? In C++20, the compiler can generate the equality operator.<\/p>\n<h2>Compiler-Generated Equality Operator<\/h2>\n<p>&nbsp;For a user-defined type, you have to choose the appropriate equality semantics.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ equalityReferenceValue.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Date<\/span>{\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    Date(<span style=\"color: #007788; font-weight: bold;\">int<\/span> y, <span style=\"color: #007788; font-weight: bold;\">int<\/span> m, <span style=\"color: #007788; font-weight: bold;\">int<\/span> d)<span style=\"color: #555555;\">:<\/span> year(y), month(m), day(d){}\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">==<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Date<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n <span style=\"color: #9999ff;\">private:<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> year;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> month;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> day;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Man<\/span>{\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    Man(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string n, <span style=\"color: #007788; font-weight: bold;\">int<\/span> a)<span style=\"color: #555555;\">:<\/span> name(n), age(a){}\r\n    <span style=\"color: #007788; font-weight: bold;\">bool<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">==<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> Man<span style=\"color: #555555;\">&amp;<\/span>) <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n <span style=\"color: #9999ff;\">private:<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>string name;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> age;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>boolalpha <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    Date date1(<span style=\"color: #ff6600;\">2022<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">31<\/span>);\r\n    Date date2(<span style=\"color: #ff6600;\">2022<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">31<\/span>);\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"date1 == date2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (date1 <span style=\"color: #555555;\">==<\/span> date2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"date1 != date2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (date1 <span style=\"color: #555555;\">!=<\/span> date2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    Man man1(<span style=\"color: #cc3300;\">\"Rainer Grimm\"<\/span>, <span style=\"color: #ff6600;\">56<\/span>);\r\n    Man man2(<span style=\"color: #cc3300;\">\"Rainer Grimm\"<\/span>, <span style=\"color: #ff6600;\">56<\/span>);\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 == man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">==<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"man1 != man2: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> (man1 <span style=\"color: #555555;\">!=<\/span> man2) <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In C++20, the compiler can auto-generate the equality operator and use it as a fallback for the inequality operator. The auto-generated equality operator applies value equality. To be more precise, the compiler-generated equality operator performs a lexicographical comparison. Lexicographical comparison means that all base classes are compared left to right and all nonstatic members of the class in their declaration order.<\/p>\n<p>I have to add two important points:<\/p>\n<ul>\n<li>For strings or vectors, there is a shortcut: the compiler-generated == and != operators compare first their lengths and then their content if necessary.<\/li>\n<li>The compiler-generated three-way comparison operator (<code>&lt;=&gt;<\/code>) also applies value equality. You can read more about the three-way comparison operator in my previous posts:\n<ul>\n<li><a href=\"https:\/\/bit.ly\/ThreeWayComparison\">The Three-Way Comparision Operator<\/a><\/li>\n<li><a href=\"https:\/\/bit.ly\/MoreDetailsToSpaceship\">More Details to the Spaceship Operator<\/a><\/li>\n<li><a href=\"https:\/\/bit.ly\/OptimizedComparisonSpaceship\">Optimized Comparision with the Spaceship Operator<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Honestly, the example works as expected but does not seem right.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6509\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/equalityReferenceValue.png\" alt=\"equalityReferenceValue\" width=\"400\" height=\"273\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/equalityReferenceValue.png 454w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/equalityReferenceValue-300x205.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Two dates with identical values should be regarded as equal but not two men. The equality of two men should be based on their identity and not on their name and age.<\/p>\n<\/p>\n<h2>Value Objects<\/h2>\n<p>Let me discuss the details about Value Objects.<\/p>\n<h3>Properties<\/h3>\n<h4>Value Equality<\/h4>\n<p>After the last chapter, this should be obvious. The equality of a Value Object should be based on its state and not on its identity.<\/p>\n<h4>Immutability<\/h4>\n<p>A Value Object should not be mutable. This makes Value Objects ideal candidates for concurrency. Changing a Value Object means creating a new one with the modified attributes. This property, that an operation on an immutable object returns a new object has two excellent properties. For conciseness, I use Python once more.<\/p>\n<p>In Python, a string is immutable:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6510\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/immutable.png\" alt=\"immutable\" width=\"600\" height=\"204\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/immutable.png 1610w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/immutable-300x102.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/immutable-1024x348.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/immutable-768x261.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/immutable-1536x522.png 1536w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ol>\n<li>You simulate modification, by assigning the new value to the old name: s = s.upper()<code>. <\/code>The original <code>s<\/code> and the new<code> s<\/code> have different <code>addresses.<\/code><\/li>\n<li>An operation on the string returns a new string. Consequentially, you can chain string operations. In the function domain, this pattern is called a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Fluent_interface\">fluent interface<\/a>. By the way: arithmetic expressions such as <code>(5 +5) * 10 - 20<\/code> are based on the fluent interface. Each operation returns a temporary, on which you can apply the next operation. Of course, numbers are Value Objects.<\/li>\n<\/ol>\n<h4>Self-Validation<\/h4>\n<p>A Value Object should validate its attributes when created. For simplicity, I skipped this step in my previous <code>Date<\/code> class.<\/p>\n<p>What are the Pros and Cons of Value Objects:<\/p>\n<h3>Pros and Cons<\/h3>\n<p>The pros of Value Objects overweight their cons heavily.<\/p>\n<h4>Rich Types<\/h4>\n<p>You should use rich types instead of built-in types for dealing with primitive values. This has many implications. Let me compare the following two representations of a date:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">Date <span style=\"color: #cc00ff;\">date1<\/span>(<span style=\"color: #ff6600;\">2022<\/span>, <span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">5<\/span>);\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>string date2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #cc3300;\">\"2022 10 5\";  <\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>You cannot create an invalid date <code>Date(2022, 15, 5)<\/code>, because the constructor&#8217;s job is it to validate the input. This does not hold for the string value &#8220;<code>2022 15 5\"<\/code>, because someone mixed up the month and the day.<\/li>\n<li>Your program is easier to read. It is crystal clear from class <code>Date<\/code> documentation, what each component stands for.<\/li>\n<li>You can overload operators on <code>Date<\/code>. E.g.: subtracting two dates returns a time duration. A time duration should also be a Value Object.<\/li>\n<li>You can extend your Value Objects with user-defined literals for a day, a year, and a month. In this case, it is not necessary because we have them with C++20:&nbsp;<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/duration\">std::chrono::duration on cppreference.com<\/a>.<\/li>\n<\/ul>\n<h4>Performance<\/h4>\n<p>Value Objects are immutable. Consequentially, they give the optimizer additional guarantees and can be shared between threads without synchronization.<\/p>\n<h4>Proliferation of Classes<\/h4>\n<p>Only for the sake of arguments: You may end with too many small classes representing Value Objects.<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>A Null Object encapsulates a do nothing behavior inside an object. Let me show you in my next post the advantages of Null Objects.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A value object is a small object whose equality is based on state, but not identity. Typical value objects are money, numbers, or strings.<\/p>\n","protected":false},"author":21,"featured_media":6503,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6511","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6511"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6511\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6503"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}