In a Drupal programming guide, I noticed this sentence:
The theme hook receives the total number of votes and the number of votes for just that item, but the template wants to display a percentage. That kind of work shouldn’t be done in a template; instead, the math is performed here.
The math necessary to calculate a percentage from a total and a number is (number/total)*100
. Is this application of two basic arithmetic operators within a presentation layer already too much? Is the maintenance of the entire system severely compromised by this amount of mathematics?
The WPF (Windows Presentation Framework) and its UI mark-up language, XAML, seem to go to similar extremes. If you try to so much as add two numbers in the View (the presentation layer), you have committed a cardinal sin. Consequently, XAML has no operators for any arithmetic whatsoever.
Is this ultra-strict separation really the holy grail of programming? What are the significant gains to be had from taking the separation to such extremes?
I see nothing wrong with documentation being a tad pedantic, especially considering the typical target audience, even more so when it comes to things like templates, given that it’s not exactly rare for non developers (graphic artists, for example) to dabble with them. Prior to your quote, the wording suggests that, although a bit silly, the example is used to illustrate the concept, I don’t think the authors really meant to suggest that you should blindly follow their suggestion, but more to make a point.
Furthermore when it comes to documentation that targets PHP developers (and non developers), extreme pedantry is almost required, PHP has a notoriously low entry barrier and consequently the ecosystem is filled with crap code. I haven’t looked at Drupal’s codebase in years, but the last time I remember doing so, it was mostly super glue and duct tape, not WordPress bad mind you, but still I was amazed it even worked. I’ll venture a guess and say that the pedantry in the documentation is probably a direct consequence of the… not ideal state of the project’s codebase.
At the same time though, you make a fair point, and the quote you isolated feels a bit dogmatic. Separating logic from presentation is an abstract principle, and thus open to interpretation and affected by various constraints. Similarly, MVC, the main expression of the principle, at least in web development, is an architectural pattern, or perhaps more accurately a tiered architectural model with three main components and without a canonical, or “pure”, implementation. There are no “holy grails” in this, as there are no “holy grails” in anything in software development, our profession is one of trade-offs.
It’s nearly impossible to say how much the application of two basic arithmetic operators within a presentation layer will actually affect your application, unless we know exactly what you’re building and what exactly will depend on that math. Will the math need to change at a later time, for example? And if so, will you remember that this small, but perhaps crucial piece of code is not where it’s supposed to be, two years after you’ve written it? I’m assuming here that you practice separation of program logic and presentation in general, and because of that you’ve trained yourself, perhaps unwittingly, to expect things to be in a certain place.
If you are confident that you are just doing a simple operation, that will probably never change and nothing important depends on it, I’d say feel free to put it wherever you want. Sure, purists will bark, some of them will probably feint, but who cares? I’ve said it before and I’ll say it again, perfectionism is not a goal in itself, keep it simple, stupid!
1