{"id":301,"date":"2009-12-30T15:25:19","date_gmt":"2009-12-30T15:25:19","guid":{"rendered":"http:\/\/pragmaticcraftsman.com\/?p=301"},"modified":"2009-12-30T15:25:19","modified_gmt":"2009-12-30T15:25:19","slug":"final-fields-immutable-objects","status":"publish","type":"post","link":"https:\/\/pragmaticcraftsman.kubasek.com\/?p=301","title":{"rendered":"Make fields final, objects immutable"},"content":{"rendered":"<p><?php quote(\"Just as it is a good practice to make all fields private unless they need greater visibility, it is a good practice to make all fields final unless they need to be mutable.\", \"Brian Goetz\", \"in Java Concurrency in Practice (page 48)\") ?><\/p>\n<p>Are you following these fundamental principles?<\/p>\n<p>Just so we&#8217;re on the same page, an immutable object is on whose state cannot be changed after construction.<\/p>\n<p>This view is supported by Joshua Bloch in Effective Java (2nd). Item 13 states: &#8220;Minimize the accessibility of classes and members.&#8221;<\/p>\n<p><?php quote(\"The single most important factor that distinguishes a well-designed module from a poorly designed one is the degree to which the module hides its internal data and other implementation details from other modules. \", \"Joshua Bloch\", \"\") ?><\/p>\n<p>He goes on to say some fundamental principles.<\/p>\n<p><?php quote(\"A well designed module hides <strong>all <\/strong>of its implementation details, cleanly separating its API from its implementation. Modules then communicate only through their APIs and are oblivious to each others&#8217; inner workings.&#8221;, &#8220;Joshua Bloch&#8221;, &#8220;Effective Java (2nd), page 67&#8221;) ?><\/p>\n<p>These are really the basics of information hiding or encapsulation. Basics of OOP programming! It&#8217;s a good idea to learn these well.<\/p>\n<p>Think twice before making any fields public. Hold it. No, don&#8217;t make it public! Think twice before making it any other than private!<\/p>\n<p>But to make sure you only expose what is absolutely needed requires some thought. You need your own judgment and experience.<\/p>\n<p>As for the second part, item 15 states: &#8220;Minimize mutability.&#8221;<\/p>\n<p>I think this one is a little harder to justify and not so obvious. But Bloch has some very good arguments. Together with Goetz, they convinced me that I should utilize immutability more often when I&#8217;m designing classes.<\/p>\n<p><?php quote(\"Immutable classes are easier to design, implement, and use than mutable classes. They are less prone to error and are more secure.\", \"Joshua Bloch\") ?><\/p>\n<p>Convinced!<\/p>\n<p>But how? Follow these 5 steps (as per Bloch).<\/p>\n<p>1. Don&#8217;t provide any methods that modify the object&#8217;s state.<br \/>\n2. Ensure the class can&#8217;t be extended.<br \/>\n3. Make all fields final.<br \/>\n4. Make all fields private.<br \/>\n5. Ensure exclusive access to any mutable components.<\/p>\n<p>Goets says it a little bit differently.<\/p>\n<p>An object is <strong>immutable<\/strong> if:<br \/>\n  &#8211; Its state cannot be modified after construction;<br \/>\n  &#8211; All its fields are final;<br \/>\n  &#8211; It is properly constructed (the this reference does not escape during construction)<br \/>\nImmutable objects pack a lot of goodies in them.<\/p>\n<p>One more time. Tell me <strong>what are the benefits of immutable objects<\/strong>!<\/p>\n<p>They are simple to understand.<br \/>\nThey are thread safe.<br \/>\nThey require no synchronization.<\/p>\n<p>Let&#8217;s end with a great summary note from Bloch, <strong>&#8220;Classes should be immutable unless there&#8217;s a very good reason to make t hem mutable.&#8221; <\/strong><\/p>\n<p>But.<\/p>\n<p>If&#8230;<\/p>\n<p><strong>&#8220;If a class cannot be made immutable, limit its mutablity as much as possible.&#8221;<\/strong><\/p>\n<p>Strong statements.<\/p>\n<p>Learn these design principles!<\/p>\n<p>All in all, I recommend reading Item 15 from the Effective Java book. And re-read a few times until it becomes part of your design logic.<\/p>\n<p>It&#8217;s all about getting better.<\/p>\n<p>It&#8217;s all about improving.<\/p>\n<p>It&#8217;s good to learn from the pros. Everybody needs a little guidance. I know I do. (These 2 books are excellent! <a href=\"http:\/\/pragmaticcraftsman.com\/category\/books-i-recommend\/\">Recommended.<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you following these fundamental principles? Just so we&#8217;re on the same page, an immutable object is on whose state cannot be changed after construction. This view is supported by Joshua Bloch in Effective Java (2nd). Item 13 states: &#8220;Minimize the accessibility of classes and members.&#8221; He goes on to say some fundamental principles.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10],"tags":[16,24],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-better-coder","category-java","tag-abstraction","tag-immutability"],"_links":{"self":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=301"}],"version-history":[{"count":0,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/301\/revisions"}],"wp:attachment":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}