<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Private vs Protected</title>
    <link>http://blog.objectmentor.com/articles/2007/01/21/private-vs-protected</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Private vs Protected</title>
      <description>&lt;p&gt;Someone on comp.object recently asked why anyone would make a field private since privacy ruins extensibility.&lt;/p&gt;


	&lt;p&gt;I recently read an article on comp.object that asked the following question:&lt;/p&gt;


&lt;blockquote&gt;While I can see that the &amp;#8216;private&amp;#8217; modifier has
its uses, I&amp;#8217;m puzzled as to why it&amp;#8217;s advocated so much given that one
of the strong points of OO is extensibility.&lt;/blockquote&gt;

	&lt;p&gt;I responded with:&lt;/p&gt;


&lt;p&gt;&lt;blockquote&gt;The Open-Closed Principle of &lt;span class="caps"&gt;OOD&lt;/span&gt; (See &lt;a href="http://www.objectmentor.com/resources/articles/ocp.pdf"&gt;article&lt;/a&gt;) says that objects should be open for extension but closed for modification.  In other words, you should be able to change what a module does without changing the module.  Extensibility, in OO, is best achieved when you keep the code you are extending safe from modification.&lt;/p&gt;

&lt;p&gt;How do you protect a module from the forces that would try to modify it?  One technique is to keep the variables that module depends upon private.  If a variable is not private, then it is open to be used in a way that the module that owns that variable does not intend.  Indeed, using a variable in an unintended way is the &lt;strong&gt;only&lt;/strong&gt; reason to make the variable public or protected.  But when you use a variable in an unintended way you likely force modifications into the owner.  If, on the other hand, all the variables of a module are private, then no modification can be caused through unintended useage.&lt;/p&gt;

&lt;p&gt;Privacy does not preclude extensibility.  You can create public or protected accessor methods that: 1) provide extenders access to certain variables, and 2) ensure that the extenders don&amp;#8217;t use the variable in an unintended way.&lt;/p&gt;

&lt;p&gt;For example, given a variable &lt;code&gt;v&lt;/code&gt; used by a module &lt;code&gt;m&lt;/code&gt;, such that &lt;code&gt;v&lt;/code&gt; should never be negative.  If you make &lt;code&gt;v&lt;/code&gt; &lt;code&gt;public&lt;/code&gt; or &lt;code&gt;protected&lt;/code&gt; someone could set it to a negative number breaking the code in &lt;code&gt;m&lt;/code&gt; and possibly forcing mofidication to &lt;code&gt;m&lt;/code&gt;.  However, if &lt;code&gt;v&lt;/code&gt; is private but is accessible through &lt;code&gt;getV&lt;/code&gt; and &lt;code&gt;setV&lt;/code&gt; methods; and if the &lt;code&gt;setV&lt;/code&gt; method throws an exception if you pass it a negative number, then &lt;code&gt;m&lt;/code&gt; is safe, and extenders are forced to follow the rules that &lt;code&gt;m&lt;/code&gt; expects.&lt;/p&gt;

&lt;p&gt;To be fair, while I am a big proponent of keeping variables private, I have also come to rely much more on my unit tests to enforce the appropriate use of variables.  When the code enjoys 90+% unit test coverage those tests will uncover and prevent variable misuse.  This softens the need for the compiler to enforce privacy.  This is not to say that you should not make your variables private, you should.  It is to say that if you use &lt;span class="caps"&gt;TDD&lt;/span&gt;, the cost/benefit ratio changes, and you may find that you can soften access to some variables.&lt;/p&gt;&lt;/blockquote&gt;</description>
      <pubDate>Sun, 21 Jan 2007 07:28:58 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4ec55763-27c8-41ba-a68d-87e73c2647d9</guid>
      <author>Uncle Bob</author>
      <link>http://blog.objectmentor.com/articles/2007/01/21/private-vs-protected</link>
      <category>Uncle Bob's Blatherings</category>
    </item>
    <item>
      <title>"Private vs Protected" by eirikm</title>
      <description>&lt;p&gt;Funny thing then that Eiffel doesn&amp;#8217;t have a &amp;#8216;private&amp;#8217; modifier (not the last time I checked, that is). AFAIK, The Open-Closed Principle was first mentioned in Object-Oriented Software Construction by Dr. Bertrand Meyer, who incidentally is the designer of Eiffel.&lt;/p&gt;</description>
      <pubDate>Wed, 24 Jan 2007 04:03:35 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9674d40b-5e62-492f-83ca-76e1bef21978</guid>
      <link>http://blog.objectmentor.com/articles/2007/01/21/private-vs-protected#comment-47</link>
    </item>
    <item>
      <title>"Private vs Protected" by belugabob</title>
      <description>&lt;p&gt;&amp;#8220;When the code enjoys 90+% unit test coverage those tests will uncover and prevent variable misuse.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;...providing, of course, that you&amp;#8217;re not shipping your code as an API, in which case you can&amp;#8217;t predict what the users of the API will do.&lt;/p&gt;</description>
      <pubDate>Tue, 23 Jan 2007 06:07:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ade38712-e55c-46ab-b7c5-e0d3d716de70</guid>
      <link>http://blog.objectmentor.com/articles/2007/01/21/private-vs-protected#comment-45</link>
    </item>
  </channel>
</rss>
