maia comments on Open Thread, July 1-15, 2012 - Less Wrong

2 Post author: OpenThreadGuy 01 July 2012 10:45PM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (150)

You are viewing a single comment's thread. Show more comments above.

Comment author: maia 02 July 2012 12:49:14PM 0 points [-]

Why is it an antipattern?

Comment author: Viliam_Bur 04 July 2012 08:04:40AM 4 points [-]

Designing a HTML page can be complicated, especially if client requires complicated things, animations and other effects, pixel precision, and compatibility with older browsers. Also the page may contain non-trivial JavaScript code. Mix it up with PHP code (or Java code), and you get something very complicated. If you do some change in design, it may break the code. If you do some change in code, it may break the design. In a larger team you can have one person who is HTML expert but not a programmer (or a very bad programmer), and a coder who is not a HTML expert. If you make them both edit the same page, it is unpleasant for both of them.

Unfortunately, things like this are done very often. I often see it in Java web development. In theory, you have all the tools you need, so you don't have to put any Java code in the JSP files. (There are special tags for "if then else" and "for each", which is all you need in most templates.) But most people do it anyway, just because they can and it's the fastest way to write it, but then it is horrible to maintain.

You get clean design by separating different aspects of the work. If one file is reading from the database, second file processes the data, and third file formats the data in nice HTML, that's good. If lines 1-46, 67-78, 89-123, 150-176, 189-235 format the data in HTML, the lines 46-65, 124-128 read from database, and the lines 66, 79-88, 129-149 process the data, that's bad, because it's difficult to read. And if something is difficult to read, many problems follow automatically; it's difficult to find bugs, it's difficult to make changes.

Even if I write a simple PHP file, I always put code in the first part and design in the second part, clearly separated. With a larger project, it must be two different files.

Comment author: dbaupp 02 July 2012 01:38:19PM 1 point [-]

There are few reasons, e.g. these questions. But as an incomplete summary, it means your templates are language independent and it enforces a style of coding and application design that is easier to test and maintain.

(This isn't to say that logicful templates are never appropriate: for quick hacks and prototyping they are perfect!)