http://qs321.pair.com?node_id=825664

One of the tenets of object oriented programming is that you keep the most accessed data in the object instead of passing it as parameters to the methods. Another recently popular rule is to make your objects immutable - that is you store the data in them at the creation time and never change it during the objects lifetime, you only read it. I think both rules are useful. The first one is rather vague and cannot be more than a rough guideline, but I think most people would have similar intuitions about what is the data most important to a given object. The area where they come into conflict is when you have an object that repeatedly performs some work - like for example a web server. Let's say you have a web handler object serving a page - then I would say that the most important data such object operates on is the request, but if we stick with immutability then request cannot be the objects internal data (one of it's attributes) because it needs to change with each new request. To keep the handler immutable we can either pass the request as a parameter to the handler methods. But then we would need to pass it to virtually all of them because the whole work a web handler does is dependent on the request params. Or we can make the handler a temporary object created per request. Most Perl web frameworks uses the first solution - the only one, that I am a aware of, that uses the second one is Tatsumaki, I don't know much about other languages - but, as much as I understand their architecture, both Rails and Django use the second solution. When most of the code that the users of your framework write is about serving the request, i.e. is mostly processing the parameters and other request data - then keeping that data at hand is the right way.

The temporary object doing the repeatable work (serving the web page) does not need to do everything by it's own. It can have access to the application and other more persistent objects - and it can read data from them and also call methods on them. So using such a temporary object does not mean recreating some heavy to build and not changing objects with each request.

All of this can also be described in terms of scope - a web application has at least two scopes - the application scope where you can keep data persistent between requests - and the request scope where the request data is processed. The immutability principle requires that objects in a wider scope do not keep links to objects in narrower scope.