Frameworks - 10 pounds of Design Patterns in a 5 pound bag (hopefully)
I was inspired to write this framework-related post when I read Dirk Merkel's "Practical Active Record in PHP" article from the latest issue of PHP Architect. Ironically, I was inspired because the PHP framework I'm most excited about (Zend Framework) has, AFAIK, chosen to use a Table Data Gateway pattern instead of an Active Record pattern, which I think is not quite as intuitive or easy to use. But a comparison of those two patterns is not only off-topic for this post, but well beyond my analytical qualifications.
Instead what I'd like to point out is the fact that all good frameworks should help programmers implement good design patterns. Programmers uncomfortable or unclear about frameworks might also be uncomfortable or unclear about design patterns, but design patterns are just ways of expressing common solutions to common problems, and frameworks can help teach good patterns. The web-proven ones that come to my mind first are MVC, Front-Controller, and Active Record, but there are many many more.
If you want to fully grok the Active Record pattern, you should of course read code & articles - like the article I mentioned above, or perhaps this article, or any other resource on the subject. But, since design patterns are common solutions to common problems, you don't want to re-write Active Record code every time you use a database in your applications. Instead, once you know how the Active Record pattern works, you can look for a framework which gives you an implementation of the pattern with an interface you like.
This is a much better approach to learning design patterns, IMO, than to learn design patterns outside of a framework. I find that resources covering design patterns without a framework tend to be overly abstract. Pattern implementations inside frameworks tend to constrain their context to the framework's intended use. I.e., you can learn much more about MVC theory by first diving into setting up an example Spring Web MVC application than you can by reading an abstract MVC blueprints paper.
When writing an application with a framework, you might get the feeling that you're dumbing yourself down - that you're relying on the framework too much and therefore becoming dependent on the way it works, or locking yourself into an architecture you don't fully grok and therefore can't fully debug. While that's entirely possible, it's more likely that, given the framework is reputable and has a solid architecture, you're actually relying upon good implementations of well-proven design patterns, and learning all about them in the process. This is not only good for your application, it's good for you as a programmer.
frameworks, licenses, etc.
I came across this interesting and relevant blurb from a Dave Rosenberg post on not using GPL for open source projects: </span>I'll take a stab at the fully-fleshed explanation. The reason Apache is for ubiquity is because, as a permissive license, it allows the licensee to do more with the software. Most relevant, it allows the licensee to build new software on top of the licensed software - a derivative work - but close-source the new software. This makes Apache-covered (or more generally, permissively-covered) software a suitable selection for a wider audience - those that won't (or can't) return their own contributions to the community AND those that will. The reason GPL can be said to be a more commercial-friendly license (from a project admin perspective) is because, as a viral license, it requires that any modifications made by anyone must be returned as open-source software as well. So a potential competitor cannot "steal" your code (it is free for use anyway), but even furthermore, they cannot improve upon your offering without returning those improvements to you as well - competitors will actually help you. Now, back to frameworks. Spring, Struts, Tapestry, Google Web Toolkit, Zend Framework, CakePHP, Symfony, Ruby on Rails - all of these use permissive licenses. The reason, of course, is that frameworks are intended to build other software. And that other software won't always be open-source. But it's important for any framework to have a strong and active community in order to enhance the framework itself, so you want ubiquity and popularity over "purity". If Spring, or any other framework, were licensed under the GPL, then all software built on top of the framework would also have to be GPL. I won't say that this never makes sense - it could be the framework author's intention that the framework only be used to create more free software. It wouldn't make sense if the author intends for the framework to be used in as many software projects as possible. However, most of the frameworks I mentioned are licensed under Apache License, Version 2.0 (or other similar licenses), and apparently incompatible with GNU GPL. This is just another GPL annoyance to me. These frameworks are clearly good free software, and have adopted a widely-used permissive license in order to foster popularity and community participation. I'm all for free software, but when I use any of these frameworks in a free software project, I'll be using a BSD license.
Spring as GPL wouldn't make a lot of sense, just like SugarCRM as Apache wouldn't make sense. (A very dumbed-down explanation being Apache is for ubiquity whereas GPL is for commercial.)</p>
On Frameworks
Sorry for the long hiatus. I've been busy learning and doing things I've never done before, and its those things which I'm going to blog about.
I've also recently been changing gears as a developer from a scripting-style PHP programmer, to a more OO programmer (Sadly, in Java most of the time). In this transition, I'm starting to warm up to the idea of frameworks. I think I've been around enough to know at least some of the complaints lots of people have with frameworks. Over the next little while on this blog, I might bring a couple of those points up and try to lay out additional perspective on them.
Today's point:
"Frameworks are just big ugly factory factory factories too abstract and cumbersome to do any good." (example)
"If you want to develop a simple website with five to ten pages, limited access to a database, and no obligations to ensuring its performance or providing documentation, then you should stick with PHP alone. You wouldn't gain much from a web application framework, and using object orientation or an MVC model would likely only slow down your development process." (src)
The above statement from the Symfony manual pretty much lays it out, especially since the example critical article states the scope of the hypothetical project as, "...build a spice rack." For simple projects, OO and Frameworks are over-kill.
But I think the better metric for determining if a framework is appropriate is not in number of pages (indeed, frameworks often at least double or triple your number of files), but rather in scope of features and depth of functionality.
In order to determine this, see if you can identify some useful objects in your project at all. Anti-OO heads might correctly argue that objects aren't *required* so there aren't any to identify, whereas OO-heads might correctly say there are objects in every project - which is why I added the "useful" qualifier.
A useful object, IMO, is an object that reasonably encapsulates properties and behavior - i.e., an object whose properties or behavior will be re-used in multiple areas of the project - the common examples are DB, logging, emailing classes etc.
Now, if there's only 1 or 2 useful objects (e.g., spice jar and spice rack) in the entire scope of the project, OO is applicable, but maybe unnecessary, and frameworks are counter-productive (they tend to introduce upwards of a dozen or more support objects with which you also have to deal).
On the other hand, if there are half a dozen useful objects, it is helpful to handle them all in a consistent way so that you can be sure of responsibility scope, standard interfaces, access levels, coding styles, blah blah on every object in the project. OO and frameworks are meant to force you to do this. I think the main point of pain most PHP programmers have with frameworks is that forcing bit, which is what we find cumbersome.
This doesn't have to be the case. A good framework should align with your own style, and help you adopt OOP quickly and easily, without getting in the way of your non-framework, non-OO code. The framework should be instructional, but unobstrusive. In short, if your experience with frameworks is that they are forcibly cumbersome, check around for a different framework - that's why there are so many options (Zend Framework, CakePHP, and Symfony seem to be the biggies for PHP). When looking at frameworks, read thru their documentation and tutorials - the objects and logic should make sense to you, not confuse you.
However, you will probably never find The Perfect Framework, unless you write it yourself (bad move). More than likely, you will have to climb the learning curve for some framework, letting it force you to adopt some new coding practices. I think PHP programmers are a bit spoiled in that PHP has such a shallow learning curve that a framework's learning curve seems daunting in comparison. (Contrast this to Java where learning by framework is probably the easiest way to learn Java?) But really, once you grasp the core "Aha!" features of a framework, the rest tends to fall into place.
Climbing this learning curve is usually the point at which a PHP programmer finds the framework to be too abstract. Why is the Controller class hierarchy 5 levels deep? Why is there an interface defined if it doesn't DO anything? I think the natural inclination of PHP programmers is to try to grok the framework by reading code. This isn't as straight-forward in OO frameworks. There's lots that goes on with OO frameworks which is implicit in the design of the objects (and the mechanisms of OOP) rather than explicit in the code. I would encourage PHP programmers to stick with it - it's necessary to brush up OO skills to fully grok any framework. But again, if you find a certain framework's code and design totally illogical, it's probably not the one for you.
All I've really done so far is explain WHY frameworks are cumbersome and abstract ... but do they do any good? It's easy to just point to Ruby on Rails as proof that frameworks can be awesome, but I'd also like to point out some of the specific reasons why this is so. (I may explore these in depth later)
Standardizing your code. This is a biggie, IMO. When you do script-by-script, ad-hoc programming, your mood, caffeine levels, circadian rhythms, or any number of other factors might change the way you write code from minute to minute. This is Very Bad for maintaining the code later on, when you need to change something and realize not only is it duplicated in dozens of locations, but each one has a slight variant in how the code is run, which means you can't just find-replace it all. Ouch.
Less "plumbing" code. Now this of course relies on the idea that your project NEEDS plumbing code in the first place. Again, if your project is small, it probably doesn't need plumbing code, and adding a framework full of plumbing code slows you down. An example of "plumbing" code is DB-access code. Any object in your project which accesses a database needs it, so it's nice to have a standardized method for doing it. And if you have a dozen objects of your own to write, why not wire them up with a framework's db-access plumbing code.
Productivity. This is not apparent while first learning the framework, but adopting a framework should boost your productivity. As stated above, you will be writing little (or no) plumbing code, instead focusing on YOUR code responsibilities, and relying on the framework for others.
Testability. Arguably not a feature of ALL frameworks, but good frameworks tend to employ good OO design, which forces YOU into better OO design, which makes your code testable via mocking objects and the like. This is another one of those things that probably turns PHP programmers off at first, but if they take the time to learn it and do it, the rewards more than make up for it. Writing tests first forces you to think about how other code will interact with the code you're about to write. And if you continue to write tests, you build up an entire battery of tests which help to keep you from inadvertently introducing bugs in the future. Tests do add some time onto your development, but they give you a great deal of quality, and confidence to modify code down the road.
That's about it for now. I've actually enjoyed the Spring framework a good deal in my Java work. It inspired me to go looking at ZF, Cake, and Symfony, which I think are also pretty good frameworks for PHP. If and when I need to build a sizeable PHP project of my own, I'm sure I'll be using one of them, or something similar.
Franchising, open source, and "methods of doing business"
After reading Asay's post on this subject, I think this is a great idea, and very pertinent to things we're doing now at SourceForge. I don't think there's an open-source company out there making this their primary business model. Dual-License, support, merchandising, patronage .... all of these seem to have at least 1 exemplary business - maybe MySQL, Red Hat, Firefox, and Eclipse respectively?
As he says, this is somewhat present in some of the various open-source certification programs out there, and the single comment on the post asks if franchising is "a mere 'Certification program' that entitles a company to provide professional suport/developemnt of a product/service with the support of other vendor/provider. Is my assumption correct?" I think this assumption is only partially correct.
Franchising is a little bit more - "a method of doing business wherein a franchisor licenses trademarks and tried and proven methods of doing business to a franchisee in exchange for a recurring payment, and usually a percentage piece of gross sales or gross profits as well as the annual fees" according to Wikipedia. (emphasis mine) Most (all?) existing open source certification programs are only about tried and proven technical skills. One of the biggest lessons the open source software industry is teaching is that "methods of doing business" are much harder, more important, and more valuable to get right, in terms of making money.
I do think there's room in the community for some big franchising companies, and I think they could really help the uptake and adoption of open source software in many markets. However, I don't think "franchising" is the only method which can inject those "tried and proven methods of doing business" into the open source software community.
IMHO, this - to provide tried and proven methods of doing business to the open source community - is and should be the mission of SourceForge Marketplace. In our case, we cannot (and should not) "license" the methods, but rather we should provide all the tools necessary for anyone, from a single person to an enterprise, to easily implement any methods they want. If we can do this, it will benefit me, SourceForge, and the open source community.
Reports of the death of the Long Tail have been greatly exxagerated.
People sure are making a stink about this post (with a questionable set of data, IMO) which purports to demonstrate that the Long Tail of web traffic is actually shrinking. Nick Carr has an excellent response explaining why the Long Tail is not shrinking, but rather, the economic value of the Long Tail is merely concentrating. This all falls within the scope of Anderson's previously asserted effects of the rise of the Long Tail - content aggregators will become (apparently ARE) the biggest winners in the phenomenon.
I'd like to expand just a bit on his explanation of how Myspace & Facebook are actually Long Tail websites - i.e., they aggregate the Long Tail of millions of personal mini-websites.
It should also be noted that Google, Yahoo, eBay, and craigslist are other Long Tail websites. Google and Yahoo are built on the long tail of search words (and subsequently related ads!), while eBay and craigslist aggregate the Long Tail of products for sale online.
So, of the top 9 sites (We all know pogo.com is not really in the top 10, right?), 6 are Long Tail sites? Well, even aol.com can be labeled as a Long Tail website, considering that their real list of top 10 searches are for those other Long Tail sites.
That leaves only msn.com and live.com as non-Long Tail sites in the top 10, right? Without the search data from those sites, it's hard to know for sure, but they could likely be similar to AOL - a mere entry point to the other Long Tail kings. Additionally, I've long suspected msn.com sneaks onto the tops of these lists merely for all the visits coming from IE browser users that haven't changed their default home page.
The Long Tail is NOT shrinking on the web. It's growing and the winners are exactly who was predicted - big-time aggregators.
