Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Immutabillity and scope and temporary objects by zby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2021-12-07 23:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (34 votes). Check out past polls.

    Notices?