<?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/"
	xmlns:series="http://organizeseries.com/"
		>
<channel>
	<title>Comments on: In Further Defense Of Avoiding Private Methods</title>
	<atom:link href="http://www.brandonsavage.net/in-further-defense-of-avoiding-private-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brandonsavage.net/in-further-defense-of-avoiding-private-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=in-further-defense-of-avoiding-private-methods</link>
	<description>The personal blog of Brandon Savage. Contains entries of a personal and professional nature focusing on PHP, Apple, LAMP, MySQL and Washington, DC.</description>
	<lastBuildDate>Wed, 15 May 2013 14:54:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2-alpha</generator>
	<item>
		<title>By: Heikki Naski</title>
		<link>http://www.brandonsavage.net/in-further-defense-of-avoiding-private-methods/#comment-6238</link>
		<dc:creator>Heikki Naski</dc:creator>
		<pubDate>Sun, 16 Dec 2012 11:22:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=1684#comment-6238</guid>
		<description><![CDATA[Some cons of overusing protected visibility are reduced information hiding, increased scope of attributes, accusing visibility for bad design of class, adding dependencies within the class and adding to the public API.

---------

&quot;However, the developer of ExtendedTestObject does not have the option to avoid using setNewString() if they want to set a string&quot;

This is exactly one of the most important benefits of making attributes private. When we hide the information where and how the data is obtained from, we also create single point of control where we can assert that all access to the data is uniform and constrained in similar ways. Think invariants.

Setting attributes private also reduces the scope of them to only that class, which reduces the potential for errors.

If you&#039;re tempted to declare a method protected, it could be that the class isn&#039;t designed properly. Could there be a way to factor the class better, and expose only the meaningful attributes?

Note that protected attributes should be regarded in the same way as the public interface because any method using an overridable attribute can be affected by inheritance. Therefore, making attributes protected can actually make it harder to inherit from the class. Thus, overridable attributes need documentation of all uses of them in other methods, meaning that you have to document more how a method works, instead of what it does.

And of course, you can never take anything out of your public API (which includes protected attributes), unless you really want to annoy users of the class who have already extended it.

In my opinion, it&#039;s shortsighted to refrain from using private attributes. In the long run that will cause problems and make development harder. Using protected more will not make inheritance easier but it will make more inheritance possible. And the cost of that is potential for more errors.]]></description>
		<content:encoded><![CDATA[<p>Some cons of overusing protected visibility are reduced information hiding, increased scope of attributes, accusing visibility for bad design of class, adding dependencies within the class and adding to the public API.</p>
<p>&#8212;&#8212;&#8212;</p>
<p>&#8220;However, the developer of ExtendedTestObject does not have the option to avoid using setNewString() if they want to set a string&#8221;</p>
<p>This is exactly one of the most important benefits of making attributes private. When we hide the information where and how the data is obtained from, we also create single point of control where we can assert that all access to the data is uniform and constrained in similar ways. Think invariants.</p>
<p>Setting attributes private also reduces the scope of them to only that class, which reduces the potential for errors.</p>
<p>If you&#8217;re tempted to declare a method protected, it could be that the class isn&#8217;t designed properly. Could there be a way to factor the class better, and expose only the meaningful attributes?</p>
<p>Note that protected attributes should be regarded in the same way as the public interface because any method using an overridable attribute can be affected by inheritance. Therefore, making attributes protected can actually make it harder to inherit from the class. Thus, overridable attributes need documentation of all uses of them in other methods, meaning that you have to document more how a method works, instead of what it does.</p>
<p>And of course, you can never take anything out of your public API (which includes protected attributes), unless you really want to annoy users of the class who have already extended it.</p>
<p>In my opinion, it&#8217;s shortsighted to refrain from using private attributes. In the long run that will cause problems and make development harder. Using protected more will not make inheritance easier but it will make more inheritance possible. And the cost of that is potential for more errors.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sven Rautenberg</title>
		<link>http://www.brandonsavage.net/in-further-defense-of-avoiding-private-methods/#comment-6213</link>
		<dc:creator>Sven Rautenberg</dc:creator>
		<pubDate>Tue, 11 Dec 2012 19:57:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=1684#comment-6213</guid>
		<description><![CDATA[I look at your example, and I see good design. The TestObject class actually does it all right. Let&#039;s assume for a moment that besides offering to set and get the internal string, it MIGHT actually do something with it. A string object class, just like ArrayObject.

Guess what: The authors of ArrayObject intentionally did not expose anything of the inner workings of this object. All you get is the API consisting of only public methods, and you have to deal with it. It does not prevent you from extending it, though. So you are able to actually create a better version of all the public methods, and you can add functionality, but you cannot interfere with the inner workings.

Back to your TestObject. Making &quot;_setString&quot; protected does not help at all, because you also have to make the private property protected.

In fact, the right way to deal with your class is to use the API that is intended for this. Use parent::setNewString() to actually set the modified string. Or go another route and modify getString(). It is actually a good thing to do this, it is called decorator pattern.

But let&#039;s assume for a moment that both classes are owned by a single entity. This voids the &quot;cannot fix the bug otherwise&quot; argument from your other posting. You can do everything you want to your classes.

But what if you do? You might want to refactor TestObject completely, all internal workings will be changed. If you exposed internals via protected methods and properties, you also have to check, more likely also change all classes that extend TestObject, because they might stop working.

A protected method is a STOP-sign: &quot;No changes allowed here, because otherwise you will break things in other classes.&quot; A protected method is part of the public API of this class, as I said.

Now if you write a library that anyone is intended to use, and you have no control over any extending class, the more protected methods and properties you offer, the less you can change anything in the library. Protected methods disable your ability to change things.

But of course they ENABLE the users of the class to change the class through inheritance. The question is: Is this the only pattern that can be used to change classes? I believe &quot;no&quot;.]]></description>
		<content:encoded><![CDATA[<p>I look at your example, and I see good design. The TestObject class actually does it all right. Let&#8217;s assume for a moment that besides offering to set and get the internal string, it MIGHT actually do something with it. A string object class, just like ArrayObject.</p>
<p>Guess what: The authors of ArrayObject intentionally did not expose anything of the inner workings of this object. All you get is the API consisting of only public methods, and you have to deal with it. It does not prevent you from extending it, though. So you are able to actually create a better version of all the public methods, and you can add functionality, but you cannot interfere with the inner workings.</p>
<p>Back to your TestObject. Making &#8220;_setString&#8221; protected does not help at all, because you also have to make the private property protected.</p>
<p>In fact, the right way to deal with your class is to use the API that is intended for this. Use parent::setNewString() to actually set the modified string. Or go another route and modify getString(). It is actually a good thing to do this, it is called decorator pattern.</p>
<p>But let&#8217;s assume for a moment that both classes are owned by a single entity. This voids the &#8220;cannot fix the bug otherwise&#8221; argument from your other posting. You can do everything you want to your classes.</p>
<p>But what if you do? You might want to refactor TestObject completely, all internal workings will be changed. If you exposed internals via protected methods and properties, you also have to check, more likely also change all classes that extend TestObject, because they might stop working.</p>
<p>A protected method is a STOP-sign: &#8220;No changes allowed here, because otherwise you will break things in other classes.&#8221; A protected method is part of the public API of this class, as I said.</p>
<p>Now if you write a library that anyone is intended to use, and you have no control over any extending class, the more protected methods and properties you offer, the less you can change anything in the library. Protected methods disable your ability to change things.</p>
<p>But of course they ENABLE the users of the class to change the class through inheritance. The question is: Is this the only pattern that can be used to change classes? I believe &#8220;no&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bradley Holt</title>
		<link>http://www.brandonsavage.net/in-further-defense-of-avoiding-private-methods/#comment-6211</link>
		<dc:creator>Bradley Holt</dc:creator>
		<pubDate>Tue, 11 Dec 2012 17:52:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=1684#comment-6211</guid>
		<description><![CDATA[This is a much more balanced post than your original &quot;Private Methods Considered Harmful&quot; post. In that post you took an absolutist position. The title alone implied that there were no good uses of private methods, ever. You also said, &quot;Many developers believe that the internal methods of their objects should be private. While this thought process is understandable, it’s also wrong.&quot; Thank you for this more reasoned update.

I think one point of confusion for folks is the concept of extensibility. This confusion, I believe, stems from the use of the keyword &quot;extends&quot; to indicated that one class inherits from another. I think people mistakenly conclude from this that inheritance is the only form of extension. Others who know better might mistakenly conclude that inheritance is the preferred approach to make code extensible.

In fact, one should favor composition over inheritance. Inheritance is one way to provide extensibility, but I would argue it is actually the least preferable approach. There are many design patterns that you can use to provide extensibility without resorting to simply using inheritance.

In my comments on your previous post, I mentioned the strategy pattern and the template method pattern. Those are two examples of design patterns that will allow you to provide extensibility without just naively using inheritance.]]></description>
		<content:encoded><![CDATA[<p>This is a much more balanced post than your original &#8220;Private Methods Considered Harmful&#8221; post. In that post you took an absolutist position. The title alone implied that there were no good uses of private methods, ever. You also said, &#8220;Many developers believe that the internal methods of their objects should be private. While this thought process is understandable, it’s also wrong.&#8221; Thank you for this more reasoned update.</p>
<p>I think one point of confusion for folks is the concept of extensibility. This confusion, I believe, stems from the use of the keyword &#8220;extends&#8221; to indicated that one class inherits from another. I think people mistakenly conclude from this that inheritance is the only form of extension. Others who know better might mistakenly conclude that inheritance is the preferred approach to make code extensible.</p>
<p>In fact, one should favor composition over inheritance. Inheritance is one way to provide extensibility, but I would argue it is actually the least preferable approach. There are many design patterns that you can use to provide extensibility without resorting to simply using inheritance.</p>
<p>In my comments on your previous post, I mentioned the strategy pattern and the template method pattern. Those are two examples of design patterns that will allow you to provide extensibility without just naively using inheritance.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced (User agent is rejected)
Object Caching 510/528 objects using apc
Content Delivery Network via Amazon Web Services: S3: brandonsavage-net-files.s3.amazonaws.com

 Served from: www.brandonsavage.net @ 2013-05-21 13:38:42 by W3 Total Cache -->