Why templates are evil




















We start out with some building blocks, or "supplies," for a site, but we can still design a unique experience for our clients. We can still develop a custom site architecture, wireframes, and map out each page type. In essence, we use a framework to build a custom template for each client. Clients like this method because while their visitors get a great user experience, they are also able to plug in their own text and content without any programming knowledge.

Every now and then, I get pushback from prospects who believe that a website should be built from scratch. They think using templates or frameworks is cheating. This line of thinking makes me think about a book I read a couple of years ago, Smartcuts , by Shane Snow excellent read; I highly recommend it.

In the book, Snow poses the question: Is using a calculator instead of manually doing high level math cheating? Some would say yes, but respected theoretical physicist Freeman Dyson disagrees. He argues that when students correctly learn how to use a calculator, they are able to have a better understanding of math. When you have the tools in place to eliminate repetitive tasks and rote memorization, you can focus on learning the things that excite you.

From a personal standpoint, I completely agree. I took a class or two on programming and web design in college, but the knowledge didn't stick until I started building sites in Wordpress seven years ago. Starting out with templates or themes, as they are called in Wordpress gave me a head start. Eventually, curiosity got the best of me, and I started learning how the "parts" of a theme fit together and how to customize them. You could say I learned what I know about web development backwards, but I had a lot of fun in the process, and it became a passion.

Imagine you want to add a new page to your website. Without a templated structure, you would have to build every page from scratch. Templates allow you to setup uniform page structures for different types of content. You might have a template for an interior page, a blog post, a landing page, etc. This makes adding a new page a breeze for a non-technical user.

What about those special features you want in your site? A carousel slider, booking calendar, or eCommerce shopping cart, for example. That makes creating a template tricky because you cannot be sure you didn't accidentally rely on a feature property of the type that you didn't document the type needed. Yes, Casey is compiling meta-programming generator and running it on every build.

But this is not requirement for meta-programming. This is just they way how Casey is doing "build-system". It is completely orthogonal to his meta-programming.

So build will recompile exe file only if. Same for output of preprocessor. On the plus side, one major advantage that templates have over code generation programs, like the one Casey uses, is a standard implementation that is already built into the language.

I remember Casey once mentioning on a video something to the effect of if he didn't use C he would roll his own tools for working directly with Assembly, and it seems like Lisp would be a perfect fit for that, and it even comes with meta-programming out of the box. I don't understand this argument about lisp and similar languages. How they are better at generating assembly than any other C like language. I know lisp in some level, and I understand how it is better at manipulating code as data and is good for creating DSL.

But I totally don't understand using it as machine code generator. If all you want from your language is to go fast without worrying about assembly or the underlying architecture or putting much effort into custom tools, C is more than good enough and its simple and old enough that multiple developers can work on the same codebase without issue.

You can write your assembler in any language you want of course, but the reason I think Lisp is an especially good choice is because of the powerful abstraction mechanisms available to it that you don't see in most modern programming languages. You did stuff in lines that would have otherwise taken You grinded your way through incomprehensible error messages to get to a point where if you needed to extend the code to a new case, you would know the exact 3-line template function to overload.

Your maintainers, of course, would have to invest infinitely more to achieve the same. Templates vastly increase the viable design space, which is not necessarily a bad thing, but it does make them that much harder to use well. In general, templates make the language much more complicated and difficult to implement correctly! Templates were not intentionally designed to be Turing-complete, but they are anyway -- thus, even though they can do just about anything, using them may turn out to be more trouble than it's worth.

Better negative arguments would go towards the fact that the STL has a lot of "gotchas", and using templates for purposes the STL already covers is reinventing the wheel. Templates also increase link time, which can be a concern for some projects, and have a lot of idiosyncrasies in their syntax that can be arcane to people. But the positives with generic code reuse, type traits, reflection, smart pointers, and even metaprograms often outweigh the negatives. The thing you have to be sure of is that templates are always used carefully and sparingly.

They're not the best solution in every case, and often not even the second or third best solution. You need people with enough experience writing them that they can avoid all the pitfalls and have a good radar for when the templates will complicate things more than helping. One of the disadvantages I haven't seen mentioned yet is the subtle semantic differences between regular classes and instantiations of class templates.

I can think of:. To me templates are like a chainsaw with an integrated flame thrower that can also launch grenades. One time in my life I may have a specific need of that. But most of the time, I'm using a regular hammer and a simple saw to build things and I'm doing a pretty good job that way.

How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 12 years, 8 months ago. Active 2 years, 5 months ago. Viewed 34k times. Improve this question. Add a comment. Active Oldest Votes.



0コメント

  • 1000 / 1000