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.

  • Comment on Immutabillity and scope and temporary objects

Replies are listed 'Best First'.
Re: Immutabillity and scope and temporary objects
by Xiong (Hermit) on Feb 27, 2010 at 20:51 UTC

    -- Blog post; wankerish:

    ...there is usually not more than one good way to do something...

    I see that and start to lose interest in anything that follows. This is a One True rant (in this case, supporting functional programming) and I immediately discount by half anything else the author has got to say.

    One True people are slaves to their One True Thing, be it the One True God, the One True Politico-Socioeconomic Model, or the One True Programming Paradigm.

    ...(with pure being the key word, not functional)...

    This raises my absolutism flag. Compared to the average postmodern deconstructuralist, I'm a pretty absolute guy; but I still say the solution has to fit the exercise, not the reverse.

    There are no absolute rules in programming; there is no One True Paradigm.

    That said, there is much merit in splitting work into scopes. I agree that some data is per-request and should be stored in a temporary object. Assuming you're running a persistent environment like mod_perl, then yes, there will be considerable data that never changes and might as well be stored in an immutable object.

    But then, I say there may be plenty of data that changes slowly and yet needs to be stored: per-session data, perhaps. A mutable object is reasonable here. Just don't get sucked into the One True trap.

      I agree: Absolutism is always terrible. Relativism is the one true way.

      All kidding aside, it's amazing how many people are still ignorant of Brooks' observations in No Silver Bullet. It seems that there are always many ready to drink the latest Kool-Aid.

      TGI says moo

        Absolutism is always terrible. Relativism is the one true way.
        Hmm - isn't that a self contradicting statement? Or was that on purpose and ironic?
      Let me put that quote in a longer context:
      But TIMTOWDI is a mixed bag, there is usually not more than one good way to do something, and which one it is depends on the context.
      I guess it is the criticism of TIMTOWTDI that caused that knee jerk reaction - but please read that sentence up the the period. This is not an absolute statement - it says that in a given context there is usually one good way to do something - the meaning of context here is not defined so it can be as broad as you can. To characterise the whole article as a 'One True rant' is undeserved given that the author writes:
      One thing to keep in mind is that just because you use immutability to your benefit it doesn't mean you need to use it all the time.
      The more so that I don't see any remark of heated emotions that are the characteristic part of a rant. This is so contrasting with your own style of writing presented above.