The Smarty Trap

Recently I went to Luke Welling’s presentation entitled “PHP in the 21½th Century” at OSCON. He mentioned something interesting that got me thinking, something he calls “The Smarty Trap”. This trap occurs when someone decides to write a template engine with “a simple syntax that designers can understand” – it starts with some includes and printing a variable here and there. All goes well until he realizes he needs looping, so he builds loops into the language. And so it continues until the template engine is as complicated as any other programming language.

This is probably the most common argument against template engines. People often say “but PHP IS a template engine”. Proponents of template engines will then counter argue that the syntax is simpler, but in some cases this is just not true…

We use Smarty in one of our products and in retrospect, we would most likely not have chosen to use it if we had to do it all again. However, based on our requirements and design decisions at the time, it was probably the right choice for then. When we started out, we opted for a MVC architecture where controllers pull data from models and push the data as arrays to the view. The advantage of this approach is that it keeps the templates simple because you’re only ever printing the contents of variables and occasionally looping over a dataset.

As our product evolved and became more complicated, this process became tedious and inefficient. Objects had to be converted to arrays and passed to the view. Sometimes this included a hierarchy of child objects. Then we started implementing lazy loading and iterators, the benefit of which quickly evaporated when we converted all of our objects to arrays.

We decided to instead pass either the model objects to the view or to use view helpers. The view then calls methods on the model or loops over the iterator, preserving the original objects and any lazy loading. All was well again, but of course the views started getting more and more complicated. To refer back to the argument, our templates have now become as complicated as embedded PHP code in HTML would have been. Add of course the additional overhead of the template engine and the cost of learning yet another language and suddenly a template engine doesn’t make an awful lot of sense anymore.

The problem is that requirements, design decisions and technologies all shift and evolve over time. It is not possible to predict where your product will be in 2 years, 5 years or even a few months from now. As software engineers, we try to accommodate change as much as possible by building safeguards into our code. However, at the end of the day, we cannot predict the future so we have to make peace with the fact that continuous re-factoring is as much a fact of life as change itself.

ZendCon 2009 PHP Conference – Review

October 2009 saw my return to San Jose for one of the top conferences on the PHP calendar – ZendCon. Other than taking place a month later and the conference venue shifting locations slightly, much of the format of this year’s ZendCon was the same as last year’s.

Taking the organizational reigns this time around, Eli White takes over from Cal Evans who recently moved from Zend to iBuildings. Organization was mostly smooth, with a few minor hiccups – day 1 had a serious lack of power supply, leaving most attendees with dying laptops before lunch. Compounding the problem, no notepads or pens were provided on registration. Attendees who had registered early and received free netbooks reported that the netbooks came with European plugs instead of US ones. However, most of these issues were sorted out by day 2.

The speakers at this year’s ZendCon included some familiar stalwarts – Sebastian Bergmann was back and presented on topics including testing, quality assurance and continuous integration. Accompanying him, was Stefan Priebsch and Arne Blankerts, who together with Sebastian Bergmann makes up the newly formed PHP consulting company, Cal Evans was present in the capacity as a speaker and delivered a good presentation on design patterns. Of course, Elizabeth Marie Smith was back and presented on PHP for the desktop using php GTk as well as a talk on SPL

Of all the speakers, two that stood out was Ilia Alshanetsky and Stefan Priebsch. I attended two of Ilia sessions, the first about premature optimization and the second on popular caching tools, APC and Memcache. His rapid delivery style and excellent knowledge crammed an almost overwhelming amount of content into a single one hour slot. Stefan Priebsch joined in on a code review tutorial session with Sebastian Bergmann and Arne Blankerts, but it was only when he presented on OOP best practices when he came into his own. Stefan is an engaging speaker and his OOP knowledge demands a lot of respect.

As usual, Twitter and the ZendCon IRC channel was abuzz with attendees tweeting during the conference and was a great way to keep a finger on the conference’s pulse.

The conference, however, wasn’t without negatives – drinks (soft drinks and coffee) were only served after every 2 talks and physically removed after the break, even though these weren’t refrigerated to begin with. In some instances, I had to leave the conference venue to purchase my own drinks.

This year there was no party or any afterhours activities arranged by the conference or sponsors other than the usual reception – this year sponsored by Adobe.

Regarding the format of the conference, I do feel that there are too many talks and the talks are too short – it is simply not possible (unless your name is Ilia Alshanetsky) to fit any meaningful amount of information into a one hour slot , especially the more advanced topics (which are of course the more interesting ones).

ZendCon 09 ended with a framework shootout – a representative of each of the more popular PHP frameworks were invited to appear in a panel discussion and the audience grilled them with questions. Of course, to liven things up, each panel member was provided with a toy gun. Although not particularly useful, it was highly entertaining and a great way to end the conference with.

For a list of conference speakers, talks, ratings and slides of this year’s ZendCon, check out the ZendCon page.

Simpletest and brittle test cases

I’ve been thoroughly annoyed the last 2 days with Simpletest.

The mock objects have some unexpected behaviour when setting return values for their methods:

$someMock->setReturnValue('get', 123);
$someMock->setReturnValue('get', 234);

Which value does it use? 123. It doesn’t overwrite the first set value. How do we get around this problem? Like this:

$someMock->setReturnValueAt(0, 'get', 123);
$someMock->setReturnValueAt(1, 'get', 234);

setReturnValueAt() allows you to specifiy return values for any additional calls to that method. This is all dandy, but its making my test cases very brittle by creating a tight coupling between my test case and the code being tested.

What I want to be able to do is:

1) set the return value for the mock object
2) run the test that calls this method, not caring how many times the method is called (for argument sake, lets say this is a simple getter method).
3) set the return value for the mock object to something else
4) run another test

I don’t see any way of doing this other that creating a new instance of the mock object. O well.