<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Keep Your Methods Private and your APIs Minimal</title>
	<atom:link href="http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/</link>
	<description>Longer than a blog; shorter than a book</description>
	<lastBuildDate>Sat, 31 Jul 2010 14:11:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Christopher</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-422550</link>
		<dc:creator>Christopher</dc:creator>
		<pubDate>Fri, 17 Jul 2009 04:09:41 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-422550</guid>
		<description>We always allow fully extendable classes. After 10 years of building software products with Java.

Times we had a bug from being open: 0
Times project almost failed due to a library with private and final classes: 20+

Keep things open! Final and private code is like black box programming.</description>
		<content:encoded><![CDATA[<p>We always allow fully extendable classes. After 10 years of building software products with Java.</p>
<p>Times we had a bug from being open: 0<br />
Times project almost failed due to a library with private and final classes: 20+</p>
<p>Keep things open! Final and private code is like black box programming.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christof</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-317611</link>
		<dc:creator>Christof</dc:creator>
		<pubDate>Mon, 24 Nov 2008 09:29:28 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-317611</guid>
		<description>@Daniel: Python has something similar to access control but it is not enforced. So an attribute with a leading underscore is *meant* private but if you really want to you *can* use it (for e.g. tests). But if you use it in client code you know it may break in the future as it is *meant* private. So I guess it is something of both, ease of use and security for the API developer and user (if he wants it). (There are more strict possibilities to make an attribute something like private (two leading underscores or use of property() but the above should be sufficient for most needs.)

I guess for a language like Java using private the way defined above does make the most sense. Just to make something testable is not a sufficient reason as the trade-off is not good enough (as an API developer you are stuck with a non private field/method).</description>
		<content:encoded><![CDATA[<p>@Daniel: Python has something similar to access control but it is not enforced. So an attribute with a leading underscore is *meant* private but if you really want to you *can* use it (for e.g. tests). But if you use it in client code you know it may break in the future as it is *meant* private. So I guess it is something of both, ease of use and security for the API developer and user (if he wants it). (There are more strict possibilities to make an attribute something like private (two leading underscores or use of property() but the above should be sufficient for most needs.)</p>
<p>I guess for a language like Java using private the way defined above does make the most sense. Just to make something testable is not a sufficient reason as the trade-off is not good enough (as an API developer you are stuck with a non private field/method).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alexey</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-315346</link>
		<dc:creator>Alexey</dc:creator>
		<pubDate>Sun, 16 Nov 2008 15:06:07 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-315346</guid>
		<description>I meant
* Making classes and methods FINAL</description>
		<content:encoded><![CDATA[<p>I meant<br />
* Making classes and methods FINAL</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alexey</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-315325</link>
		<dc:creator>Alexey</dc:creator>
		<pubDate>Sun, 16 Nov 2008 14:31:54 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-315325</guid>
		<description>Making classes and methods is absolutely bad idea anyway. Programmers need classes to be mocked, proxied etc. You cannot to do it with final classes.</description>
		<content:encoded><![CDATA[<p>Making classes and methods is absolutely bad idea anyway. Programmers need classes to be mocked, proxied etc. You cannot to do it with final classes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SJS</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-314082</link>
		<dc:creator>SJS</dc:creator>
		<pubDate>Fri, 14 Nov 2008 13:03:03 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-314082</guid>
		<description>I&#039;m not so sure I agree. I&#039;ve worked with developers who aggressively finalize everything. Methods are package-private scope by default, only protected grudgingly, and public only when absolutely necessary.  This leads to a lot of assumptions in the code that leads to nice subtle logic errors when the time comes to actually extend the code, or to use it in unforeseen ways.

Making a field non-final can invalidate a lot of rather hidden assumptions elsewhere in the code, especially when one is working with maps, where the immutability mean that it&#039;s okay to use the object as a key rather than some aspect of the object.  Make a field non-final, and change it, and really odd bugs start showing up ... but since the class wasn&#039;t ever thought of as a suitable key (it was just convenient), that works-as-a-key behavior won&#039;t be documented or unit tested.  etc. etc.

Instead of adopting a knee-jerk &quot;policy&quot; of minimizing the API, one should instead sit down and think about the API. One should make a list of the &quot;minimal functional set&quot; (everything you need to do with the data in this class, even if it&#039;s a bit awkward), and then devise the &quot;minimal useful set&quot; (the minimal functional set with additional convenience
methods to avoid the awkward bits).  Expose the minimal useful set and spend some time documenting and testing it... and then, as need be, expand the API with the bits of common code that make sense.

For example, if you&#039;re defining an object that&#039;s a collection of things, you&#039;ll want a way to get at each of the things in the collection.  That&#039;s the minimal functional operation.  It&#039;s a good idea to be able to ask your object how many things it contains, but since you could figure that out by counting yourself, it&#039;s not part of the minimal functional set, but it is arguably part of the minimal useful set of functions.

If the object is not immutable, then you&#039;ll need a way to add new things, and where you add, you need to remove -- even if nowhere in your code at the moment you&#039;d need to remove anything.  You could do this with a clear method, or with a remove method, as one or the other *could* be implemented using the other (plus the get-every-object method).

If you remove as much of the API as possible right now, then it&#039;s likely that one developer will look at your class and say &quot;Oh, it&#039;s immutable, since there&#039;s no way to change it.&quot; and go off and write code that *depends* on that class being immutable.  Meanwhile, a different developer will look at the code and say &quot;This more-or-less does what I want, but it&#039;s missing mutator methods, so I&#039;ll just add those in.&quot;... and go off and write code that *depends* on that class being mutable.

The compiler won&#039;t complain. It&#039;s unlikely that any unit tests will expose this bug until the two sets of assumptions are well and firmly entrenched.

Been there, done that, had to clean up the mess.

The &quot;cult of extensibility&quot; is just as bad, but in the other direction.

Somewhere in the middle is about right, with the minimally useful set of public methods, and private data. And I like sean&#039;s point about interfaces... favor composition over subclassing.  In fact, my inheritance trees are really shallow these days -- I subclass for template methods, and very little else.</description>
		<content:encoded><![CDATA[<p>I&#8217;m not so sure I agree. I&#8217;ve worked with developers who aggressively finalize everything. Methods are package-private scope by default, only protected grudgingly, and public only when absolutely necessary.  This leads to a lot of assumptions in the code that leads to nice subtle logic errors when the time comes to actually extend the code, or to use it in unforeseen ways.</p>
<p>Making a field non-final can invalidate a lot of rather hidden assumptions elsewhere in the code, especially when one is working with maps, where the immutability mean that it&#8217;s okay to use the object as a key rather than some aspect of the object.  Make a field non-final, and change it, and really odd bugs start showing up &#8230; but since the class wasn&#8217;t ever thought of as a suitable key (it was just convenient), that works-as-a-key behavior won&#8217;t be documented or unit tested.  etc. etc.</p>
<p>Instead of adopting a knee-jerk &#8220;policy&#8221; of minimizing the API, one should instead sit down and think about the API. One should make a list of the &#8220;minimal functional set&#8221; (everything you need to do with the data in this class, even if it&#8217;s a bit awkward), and then devise the &#8220;minimal useful set&#8221; (the minimal functional set with additional convenience<br />
methods to avoid the awkward bits).  Expose the minimal useful set and spend some time documenting and testing it&#8230; and then, as need be, expand the API with the bits of common code that make sense.</p>
<p>For example, if you&#8217;re defining an object that&#8217;s a collection of things, you&#8217;ll want a way to get at each of the things in the collection.  That&#8217;s the minimal functional operation.  It&#8217;s a good idea to be able to ask your object how many things it contains, but since you could figure that out by counting yourself, it&#8217;s not part of the minimal functional set, but it is arguably part of the minimal useful set of functions.</p>
<p>If the object is not immutable, then you&#8217;ll need a way to add new things, and where you add, you need to remove &#8212; even if nowhere in your code at the moment you&#8217;d need to remove anything.  You could do this with a clear method, or with a remove method, as one or the other *could* be implemented using the other (plus the get-every-object method).</p>
<p>If you remove as much of the API as possible right now, then it&#8217;s likely that one developer will look at your class and say &#8220;Oh, it&#8217;s immutable, since there&#8217;s no way to change it.&#8221; and go off and write code that *depends* on that class being immutable.  Meanwhile, a different developer will look at the code and say &#8220;This more-or-less does what I want, but it&#8217;s missing mutator methods, so I&#8217;ll just add those in.&#8221;&#8230; and go off and write code that *depends* on that class being mutable.</p>
<p>The compiler won&#8217;t complain. It&#8217;s unlikely that any unit tests will expose this bug until the two sets of assumptions are well and firmly entrenched.</p>
<p>Been there, done that, had to clean up the mess.</p>
<p>The &#8220;cult of extensibility&#8221; is just as bad, but in the other direction.</p>
<p>Somewhere in the middle is about right, with the minimally useful set of public methods, and private data. And I like sean&#8217;s point about interfaces&#8230; favor composition over subclassing.  In fact, my inheritance trees are really shallow these days &#8212; I subclass for template methods, and very little else.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-313971</link>
		<dc:creator>Chad</dc:creator>
		<pubDate>Fri, 14 Nov 2008 08:39:38 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-313971</guid>
		<description>I wholeheartedly agree with this philosophy when the project is run with total code ownership and anyone can modify classes, particularly because I think limiting the interface to a class or module allows you to more-easily reason about it.  (Or maybe that&#039;s just me?)  For example, when I started working on a large Python codebase, everything was public, just as Daniel said.  However, far too many bits of code all over the system were modifying internal state.  This made refactoring needlessly difficult, so we&#039;ve been migrating fields to __private.  Now you can look at a class and have a pretty good assessment of how it&#039;s used.  If you need to use it somehow else, you can change it.

For public APIs, final and private should be used with caution.</description>
		<content:encoded><![CDATA[<p>I wholeheartedly agree with this philosophy when the project is run with total code ownership and anyone can modify classes, particularly because I think limiting the interface to a class or module allows you to more-easily reason about it.  (Or maybe that&#8217;s just me?)  For example, when I started working on a large Python codebase, everything was public, just as Daniel said.  However, far too many bits of code all over the system were modifying internal state.  This made refactoring needlessly difficult, so we&#8217;ve been migrating fields to __private.  Now you can look at a class and have a pretty good assessment of how it&#8217;s used.  If you need to use it somehow else, you can change it.</p>
<p>For public APIs, final and private should be used with caution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sean</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-313773</link>
		<dc:creator>sean</dc:creator>
		<pubDate>Fri, 14 Nov 2008 02:24:26 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-313773</guid>
		<description>I think most of the posters come from
a framework background and mindset. Harold is talking about a library model.  If you need extension points use interfaces. Subclassing correctly is hard, both from the Api creators standpoint and the consuming dev&#039;s usage of the api. 

If you do need to extend an api use composition and a facade

Pointing out large complex systems doesn&#039;t invalidate rusty&#039;s arguement. And his arguement applies to any language not just java</description>
		<content:encoded><![CDATA[<p>I think most of the posters come from<br />
a framework background and mindset. Harold is talking about a library model.  If you need extension points use interfaces. Subclassing correctly is hard, both from the Api creators standpoint and the consuming dev&#8217;s usage of the api. </p>
<p>If you do need to extend an api use composition and a facade</p>
<p>Pointing out large complex systems doesn&#8217;t invalidate rusty&#8217;s arguement. And his arguement applies to any language not just java</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: A</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-313449</link>
		<dc:creator>A</dc:creator>
		<pubDate>Thu, 13 Nov 2008 16:35:16 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-313449</guid>
		<description>I follow YAGNI only when it is not extra work

If it is extra work to mark methods as private (esp like in the example get/setters), without perceivable gain - I am not game for it.

Same goes for final too. If It does not hurt to keep it open then why close it? esp. if u r not Nostradamus.</description>
		<content:encoded><![CDATA[<p>I follow YAGNI only when it is not extra work</p>
<p>If it is extra work to mark methods as private (esp like in the example get/setters), without perceivable gain &#8211; I am not game for it.</p>
<p>Same goes for final too. If It does not hurt to keep it open then why close it? esp. if u r not Nostradamus.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paco</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-313412</link>
		<dc:creator>Paco</dc:creator>
		<pubDate>Thu, 13 Nov 2008 15:18:47 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-313412</guid>
		<description>When your making a public API, it might be very annoying for the consumer when everything is made final. You can&#039;t look in the mind of the programmers using your API. Over-finalization is bad. Keep the open-closed principle in mind: &quot;Open for extension, but close for modification&quot;. This doesn&#039;t violate YAGNI. 
Making things final when not needed but just to be sure does violate YAGNI.</description>
		<content:encoded><![CDATA[<p>When your making a public API, it might be very annoying for the consumer when everything is made final. You can&#8217;t look in the mind of the programmers using your API. Over-finalization is bad. Keep the open-closed principle in mind: &#8220;Open for extension, but close for modification&#8221;. This doesn&#8217;t violate YAGNI.<br />
Making things final when not needed but just to be sure does violate YAGNI.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Feathers</title>
		<link>http://cafe.elharo.com/programming/keep-your-methods-private-and-your-apis-minimal/comment-page-1/#comment-313395</link>
		<dc:creator>Michael Feathers</dc:creator>
		<pubDate>Thu, 13 Nov 2008 14:53:24 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/?p=323#comment-313395</guid>
		<description>Daniel, that echos my experience as well.  The problem with final is it&#039;s finality :-)  It does not admit the user might have a good reason to circumvent what the author (in his or her infinite foresight) was able to discern when writing the code.  That sort of perfect foresight doesn&#039;t exist and as a result there are many projects which are crippled by their inability to unit test code which uses those APIs.

People need the freedom to deal with local circumstances.  An example: Superficially, it might seem like it would be a good idea to control cars so that they can never go beyond the speed limit.  If you&#039;re taking someone with a serious injury to the hospital, however, it no longer seems like a good idea.</description>
		<content:encoded><![CDATA[<p>Daniel, that echos my experience as well.  The problem with final is it&#8217;s finality <img src='http://cafe.elharo.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   It does not admit the user might have a good reason to circumvent what the author (in his or her infinite foresight) was able to discern when writing the code.  That sort of perfect foresight doesn&#8217;t exist and as a result there are many projects which are crippled by their inability to unit test code which uses those APIs.</p>
<p>People need the freedom to deal with local circumstances.  An example: Superficially, it might seem like it would be a good idea to control cars so that they can never go beyond the speed limit.  If you&#8217;re taking someone with a serious injury to the hospital, however, it no longer seems like a good idea.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
