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

Re: Re: Short Refactoring Tip: let the object do it for you

by Ovid (Cardinal)
on May 21, 2003 at 23:28 UTC ( #259939=note: print w/replies, xml ) Need Help??

in reply to Re: Short Refactoring Tip: let the object do it for you
in thread Short Refactoring Tip: let the object do it for you

That's a very interesting observation. Given what you've written, how would you handle this?

[% IF item.can_receive %] <input type="text" name="received" value="[% item.received %]" cla +ss="input2"/> [% ELSE; item.received; END %]

In the above case, I am querying whether or not an item can accept a value for given attribute (those are Template Toolkit directives). If so, an input box is presented to the end user, with the current value filled in. If not, they only see the value. As this data can be represented in many other ways (and not just on HTML forms), it doesn't seem to make sense to have the object present HTML code to the end user. I like the idea of moving the conditional logic into the object, but I don't see a clean way of doing that.


New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: Re: Short Refactoring Tip: let the object do it for you
by mvc (Scribe) on May 22, 2003 at 11:38 UTC

    Here we have a classic conflict of design forces:

    • Encapsulation
    • Seperation of concerns (cohesion)

    If you put the UI code in the object, you are encapsulated but your object is now doing its regular work + UI work (low cohesion). If you keep it as is, you get clean seperation of concerns, but break encapsulation.

    There are other solutions that could balance these forces better. The balance of forces you want depends on your requirements.

    You could, for example, have the template code look like this:

    <% item.getReceivedUI("<input ...>") %>

    The item object getReceivedUI() will check if receive is possible, if so interpolate the parameter as a TT template with item.received in the stash. If not, it returns item.received. Then you remove the UI code from the item object by delegating the actual interpolation to some strategy object, that may use TT or anything. Thus the item knows not about UI, just about presentation logic. The template is still in charge of the UI. Is this extra work worth it? Depends on your requirements.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://259939]
[atcroft]: .oO(It is a well-known, well-tried module that has probably encountered more corner-cases and oddities than the average mortal programmer can imagine when trying to roll their own...)
[atcroft]: .oO(*Blah* It, I meant....)
[james28909]: omg
[james28909]: im just finding out that july and august have 31 days?
[atcroft]: stevieb: Apparently it was contagious....
[atcroft]: james28909: Make both hands into fists, place them together (with thumbs concealed), and every knuckle is a month (starting with Jan.) has 31 days, every dip (between knuckles) does not....
[atcroft]: (And Feb. is the odd case, because it is 28, unless it is a year divisible by 4, or if it is divisible by both 100 and 400 (at which point it is 29).)
[james28909]: i know but scroll through your calendar on your computer.
[james28909]: i thiught you were going to say make both hands into a fist and puch yourself in the face
[atcroft]: .oO(Sorry, I probably should have said take two normal hands....)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2017-04-29 04:27 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.