My privates are not public, they are protected

This week there was an interesting discussion on twitter between several people from the PHP community on the use of access modifiers, and why things should be public, protected or private, or why not. The thing that triggered this was the fact that the new Symfony2 Coding Style disallows the usage of private methods. This discussion earlier on triggered Lukas Smith to post his opinion. I commented there but the comment became thus long that I decided to write a blogpost about it myself.

In general, I agree with Lukas. I dislike the way private methods block any possible extension. I much more prefer being able to extend something if a piece of code does not support the use case I have. I agree with pro-private people that it is important to have a good API design and to use that to protect less experienced developers from making mistakes, however one should never assume that the developers using your libraries, especially Open Source libraries, are less than yourself. You as developer of a library (especially Open Source), will never think of every use case possible. People may use your code in ways you have never thought of, and they may still be using it in a very valid use case. So they may also run into problems if you make certain functionality private: this will block them from extending your library and in some situations that may block them from actually using your library in valid use cases!

I definitely am not in favor of simply opening up the complete library to every developer though. By making a clear decision on which methods are public and which methods are protected you will ensure that people simply implementing your library will use the API that you have taken the time designing. And in most cases, this will be enough for those developers, and they will be happy users of your code. However, those that wish to extend your functionality are still able to do so. And yes, this will also open up your library to completely invalid use cases for extending, but this is simply not your problem. You have taken the time and put in the effort to design the API to work in the most common use cases and you have kept the option of extending it for other valid use cases, but there will always be developers that abuse the power you've given them.

I see places though where private (and also final) are valid: When you're working on a specific implementation for a customer, you may want to use private to hide specific functionality for that project, or final to block people from overriding the method even if they may call it from subclasses. In this situation, it actually makes a lot of sense, because you will often have architects or seniors building the basis of a project, and have mediors and juniors finishing the project. And in this case, it *is* the responsibility of the senior/architect when power offered by code is abused by less experienced developers. So unless the senior or architect conciously makes the decision to open up this power, it is smarter to not offer it to the implementing developers.

I want to close down with the only reason I can see at this point where private would make sense in Open Source projects. And this is not really from a technical point of view, but more from the community side of things: When you make methods private or final, people who have an alternate use case will have to contribute their patch to the project if (s)he wants it to be changed in the main library (which in most situations would be preferable for those developers I guess). But I don't think the community point of view should rule above the technical reasons for not using private.


Add comment

Php5_zce_logo

Tags

1337 2008 2010 2011 4developers access modifiers accessibility AdaLovelaceDay09 advent agavi agile alfred amsterdam amsterdamphp apache api apple article articles atk atkMetaNode audioscrobbler autoloading automation azure backwards compatibility barcelona barcodes bash bbc bbq beatstad belgium best practices bittorrent blogging blogs boards of canada book books bughuntday bundle caching cake cal evans calendar career cat cerf certificate cfp cilex clear cms cologne common sense communities community components composer conference conferences contest continuous integration contribute contribution crisis css curl custom d-day data migration datetime DbFinderPlugin decorator decorators deployment deps devdays development directoryindex directoryiterator docblox doctrine doctrine2 documentation download dpc dpc09 dpc10 dpc11 DPC2008 dreamhost drupal dv7 eclipse ed editors efficiency enterprise errors event events expertise ezcomponents facebook filter-branch filteriterator finland flickr fork framework frameworks free ticket freelance freeze frontend fun game games geoip germany getting real git github globiterator gnome-do google google calendar googletalk graceful degradation hack hackers hidden gem hiphop howto hp HR html http i386 ibuildings icann ide ideasofmarch idm imovie inclusivity indy ingewikkeld integration international php conference internet interview ipad IPC ipc ipc08 ipc10 ipc11se iterators iterm2 javascript jenkins jenkins-php job job openings jobeet john peel joomla joomladays kiva kubuntu launcher launchy left on the web libcurl libraries library lighttpd lime linktuesday linux live london loudblog m2ts mac magazines malware mambo manchester marjolein mediterra meeting meme meta methodology micro-financing microframework microsoft migration movie music mysql namespace namespaces netbeans netherlands newsfire nllgg northeastphp nos odmarco open source opinion ORM osx paradiso paris partnership pavilion pear pecl performance personal pfc10 pfc11 pfcongres pfcongrez pfz pfz.nl photo php PHP php5.3 phpabstract phpazure phpBB phpbb phpbelgium phpbenelux phpbnl10 phpday phpdoc phpdocumentor phpgg phpitalia phpnw phpnw08 phpnw11 phpnw12 phpstorm phptek phptek09 phpuk2009 phpUnderControl phpunit php|architect php|tek podcast politics portability postcrossing presentation presentations private projects protected prototype PSR-0 public python qa qr codes re2c recruiting refactoring review rewrite ruby on rails san francisco schedule scifi script security sensio seven things sexism sfdaycgn sflive2011 shell scripting silex simplexml slides smfony software sogeti solar sound speakers spl ssh standard standards star trek static steer strings stylesheets subversion symfony symfony live symfony2 Symfony2 symfonycamp symfonyday symfonylive symfonyUnderControlPlugin talk talks tech techademy technology techportal tek09 telecommuting terratec terrorism testfest testing textmate textpattern the right tool timeout tips tld todo tomas tools training twig uncon unet usability usergroup validation vhost video vim vinyl virus warp webinar weblogging webservices wiki windows winphp women wordpress work workshop world world of warcraft wpi writing wunderlist xml xpath xsd yara year youtube zc11 ZCE zemanta zend zend framework zend server zend studio zendcon Zend_Form zite
© 2004 - 2013 Stefan Koopmanschap + Powered by Symfony, photos powered by Flickr, links powered by Delicious, Shanghai smilies by Iconbuffet. Feeds: rss / atom. Left on the Web v4.4.0.1