Private vs Protected 34
Someone on comp.object recently asked why anyone would make a field private since privacy ruins extensibility.
I recently read an article on comp.object that asked the following question:
While I can see that the ‘private’ modifier has its uses, I’m puzzled as to why it’s advocated so much given that one of the strong points of OO is extensibility.
I responded with:
The Open-Closed Principle of OOD (See article) 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.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 only 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.
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’t use the variable in an unintended way.
For example, given a variable
v
used by a modulem
, such thatv
should never be negative. If you makev
public
orprotected
someone could set it to a negative number breaking the code inm
and possibly forcing mofidication tom
. However, ifv
is private but is accessible throughgetV
andsetV
methods; and if thesetV
method throws an exception if you pass it a negative number, thenm
is safe, and extenders are forced to follow the rules thatm
expects.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 TDD, the cost/benefit ratio changes, and you may find that you can soften access to some variables.
“When the code enjoys 90+% unit test coverage those tests will uncover and prevent variable misuse.”
...providing, of course, that you’re not shipping your code as an API, in which case you can’t predict what the users of the API will do.
Funny thing then that Eiffel doesn’t have a ‘private’ 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.
Eiffel doesn’t have a “private” modifier because all Eiffel fields are “private”: http://en.wikipedia.org/wiki/Eiffel_(programming_language)#Scoping
At the very first, I’d choose to give thanks to you for this informative article. Second, I had prefer to doubt wherever I can find lot more information related to your article. I arrived here via Bing & can’t distinguish any linked up web websites along this matter. How do I subscribe for your web blog? I’d prefer to stick to your updates as they come along! I’d a query to interrogate but I forgotten what it absolutely was… anyways, thanx. Author of how to cook beef tenderloin
ul, but also very bizarre. If you aren’t careful you can get algorithms that work
Then the researchers measured the way muscle groups came down with and even done getting a dynamometer, a tool in which actions pressure, twisting, energy and additionally speed. Shortened staple fibers indicate a good high-heel wearer’s achilles tendon would certainly generate less force compared to a flat-shoe individual wearing, nevertheless muscle tissue conducted similarly both in groupings.
The uk’s The city of manchester Metropolitan School also that Higher educatoin institutions for Vienna. This may get the reason why numerous ladies fully feel rigidity in their calf muscles should they get rid of their pumps. The particular conclusions are posted inside July issue of the Academic journal for Experimental Biology.
I’m in the process of learning oop and classes in c++ and one thing I’m trying to grasp is when you write a class when it Protected preferred over using private? Can someone explain this to me, I just cannot understand why one would be better than the other, and why not just use private?
Thanks for any info.
when you write a class when it Protected preferred over using private? Can someone explain this to me, I just cannot understand why one would be better than the other, and why not just use private virtuemart templates
I used to be on the lookout for essential information on this subject.
I built a castle in the swamp and it sunk. I built a second castle and it sunk too. I built a third castle and it burned down and then sunk.
When I come to here, I think I am in the right place. the web gives me a lot of infomation, it is very informative. I think lots of people can learn much here. I will come to here again. Thanks.
Nice blog ! I like your article and i will definitely look again…....................................... regards: http://www.viaam.com
Intertech Machinery Inc. provides the most precise Plastic Injection Mold and Rubber Molds from Taiwan. With applying excellent unscrewing device in molds, Intertech is also very professional for making flip top Cap Molds in the world.
Ferriero said.Why Would Anyone ???? ?? Anyone Want to Kill Kate?20/20 Investigation: Scandal hermes kelly bag blueprint Scandal Inside the Peace Corps By BRIAN hermes h bracelet chalk BRIAN ROSS and ANNA SCHECTERJan. 10, 2011 hermes wallet 2011 — Kate Puzey was a beloved replica hermes scarves beloved Peace Corps volunteer in the African knockoff hermes scarves African country of Benin when she was grace kelly hermes bag was brutally murdered.Her parents and the people hermes h bracelet black people in the village where she served hermes leather bracelet served still ask,
It is to say that if you use TDD, the cost/benefit ratio changes, and you may find that you can soften access to some variables.
I must tell you that your post is totally cool I fantastically like your theme! Right now I have a lot of spare time to read your website because of which I added it to my bookmarks.
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.
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 only reason to make the variable public or protected.
We are the professional t-shirts manufacturer. t-shirts supplier. t-shirts factory, custom t-shirts.
Follow ipad bag the detail tips below, you can increase the laptop battery life of a year or more. Game Controllers first thing you should care about the USB Gadgets END!
Another informative post. I am impressed by it. Thanks. roof venice
truereligionjeans.com
jeans for sale
“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’t use the variable in an unintended way “
i agree with it …
Dünyan?n en büyük online okey oyunu bu sitede sizleri bekliyor. Gerçek ki?ilerle sohbet ederek Okey Oyunu Oyna ve internette online oyun oynaman?n zevkini ç?kar
I’ll just comment here.
Your essay is good except for the spelling.
I found that his foot odors never bring us to death.I never regret buying these beats by dr dre studio for him. These beats by dr dre solo are just the same as selling in the franchise store.Or even better.
hmm ,i’m not sure if this is what i’m looking for but anyway this is interresting and could be useful some day,thanks for taking time to write such cool stuff
http://www.alijewelry.com/burberry-earring-c-10.html"> Burberry Earring ,
http://www.alijewelry.com/burberry-bangle-c-11.html"> Burberry Bangle ,
http://www.alijewelry.com/bvlgari-earring-c-12.html"> Bvlgari Earring ,
provide extenders access to certain variables, and 2) ensure that the extenders don’t use the variable in an unintended way cheap beats by dre beats by dre store
General Pickett, finding the battle broken while the enemy was still reinforcing, called the troops off.
Nice post keep up next times
The professional design make you foot more comfortable. Even more tantalizing,this pattern make your legs look as long as you can,it will make you looked more attractive.Moveover,it has reasonable price.If you are a popular woman,do not miss it.
Technical details of Christian Louboutin Velours Scrunch Suede Boots Coffee:
Fashion, delicate, luxurious Christian louboutins shoes on sale, one of its series is Christian Louboutin Tall Boots, is urbanism collocation. This Christian louboutins shoes design makes people new and refreshing. Red soles shoes is personality, your charm will be wonderful performance.
Private vs Protected 33 hoo,good article!!I like the post!5