I am not very clear about the word boilerplate when it comes to programming. How is it different to other similar terms such as template and prototype? I would appreciate some examples that clearly explain the difference.
|
|
Well, this is the meaning of the three words you asked, in a programming context: Template: it is usually referred in web design or programming as a base you take to make your own fixes. But you do not usually use a template witouth your own fixes/changes. Note that in the general case, template is something you take from others (posted on the Internet, for example), while prototype or boilerplate is something you make on your own. |
|||||||||||||
|
|
In the context of programming, I do not believe that boilerplate is strictly a technical term like prototype and template. An actual boilerplate is something that was placed on a boiler. It is usually pre-printed with the exception of a serial number, boiler capacity, etc. In this respect, it is closer to a template. |
|||||
|
|
Boilerplate has a negative connotation and refers to repetitive code that does't really contribute to the logic of the program, but is required by the language or the framework. It indicates tedium and a violation of the "Don't Repeat Yourself" programming ethos. For example, in Java, it's common to provide getter and setter methods to access certain properties on an object. Say we want a class to represent brands of hot sauce:
This is a simple data object without any real internal logic. However, the standard practice is to provide "getter" and "setter" methods in case we want to add logic later or use the object with frameworks that make use of the getter and setter pattern:
These methods are boilerplate. They take up most of the file, yet they contain no real logic and do not tell us anything more about the HotSauce class. They are tedious to write, and are often automatically generated by the editor program. Boilerplate can also come from a buildup of declarations that normally save effort. For example, when using the Spring framework with Java, one must declare several things at the top of every unit test class:
The declarations at the top of the file convey important information: that we want these tests to be Spring-enabled, and to be initialized in the given context with the given active profile. The Spring framework uses those lines to do a great deal of initialization, yet they are still a bit verbose, and they must be repeated at top of every test class. Thus, even though they are useful, they become boilerplate. |
|||
|
|
|
Non-technically:
With respect to programming, a template can have a very technical meaning, which usually involves a class that has some type parameters, and boilerplate is just a piece of code that is inserted verbatim (both these uses respect the non-technical meanings). |
|||
|
|
