4

I noticed the following in the introduction for StringTemplate:

StringTemplate interprets o.p by looking for property p within object o. The lookup rules differ slightly between language ports, but in general they follow the old JavaBeans naming convention. StringTemplate looks for methods getP(), isP(), hasP() first. If it fails to find one of those methods, it looks for a field called p.

This doesn't seem to jive with this paper: http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf

Doesn't this open the door for violations of model/view separation, by essentially allowing the model to pull data by calling a method? A bad programmer could write a method getP() that causes side effects. How does ST "strictly" enforce the separation of concerns here?

Kricket
  • 4,049
  • 8
  • 33
  • 46

1 Answers1

6

Every single templating language out there does exactly that, Velocity, FreeMarker, StringTemplate and JSP/JSF Expression Language.

The separation of concerns is something the programmer should care about, not the view. People are expected to write side-effect free get/is/has methods so that anyone can call them without having to care about this. That's why these methods are supposed to be accessors and there are methods usually called with setSomething that are supposed to be mutators.

If someone writes their own classes and decides to define a getSomething method that has a side effect, they are going against the common belief and the tools are not supposed to take every single assumption when dealing with objects, they just hope people will be intelligent and will respect the common sense and write code as everyone else writes.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
  • Exactly. Speaking with no real facts to back this up, but I'd guess that determining if a method has side effects is equivalent to the halting problem. – I82Much Aug 04 '11 at 06:05
  • 2
    Good enough. But it's too bad that ST has to lie about strictly enforcing something that it's unable to enforce. The cute academic ideal of the paper is far outdated with respect to the stuff ST does in its current version. – Kricket Aug 26 '11 at 15:11
  • Actually even you strictly limit it to output fields only, it in the end comes to `XType.toString()`, and a mal programmer can write any logic in `XType.toString()`, even `System.exit(1)`. I am not in fond of so called "Strict MVC" at all. That's an academic stuff and doesn't make any sense to put it into real practice – Gelin Luo Jan 24 '13 at 22:17