Boilerplate code
In computer programming, boilerplate code is sections of code
that are repeated in several places with virtually no changes.
When using languages that are considered verbose, a programmer
has to write a lot of code to accomplish only minor functionality.
Such code is called boilerplate.
The need for boilerplate code can be reduced with high-level
mechanisms such as:
- metaprogramming (in which the computer automatically writes the required boilerplate code or inserts it during compilation)
- convention over configuration (which provides good defaults, reducing the need to specify program details in every project)
- model-driven engineering (which uses models and generators from models to code, eliminating the need for manual boilerplate code).
Origin
Further information: Boilerplate - Template (text)
The term originated in the newspaper business. Columns and other material distributed by print syndicates were sent to subscribing newspapers as ready-made printing plates. Because of their similarity to the metal plates used in making boilers (boiler - a boiler), they became known as "boiler plates", and the resulting text as "boilerplate text". Since the stories that the boilers distributed were usually "fillers" rather than "serious" news, the term became synonymous with unoriginal, repetitive text.
A related term is bookkeeping code, referring to code that is not part of the business logic but is interleaved with it in order to update data structures or handle secondary aspects of the program.
Preamble
One form of boilerplate code consists of declarations that, although not part of the program logic or the core syntax of the language, are added to the top of the source file as a custom. The following Perl example demonstrates a boilerplate template:
#!/usr/bin/perl
use warnings;
use strict;
The first line is a shebang, which identifies the file as a Perl script
that can be executed directly from the command line
(on Unix/Linux.)
The other two are pragmas (directives) that enable warnings and strict mode,
which are prescribed by the fashionable Perl programming style.
The next example is a boilerplate template for the programming language
C/C++, #include guard.
#ifndef MYINTERFACE_H
#define MYINTERFACE_H
...
#endif
This checks and sets a global flag to
tell the compiler whether the file
myinterface.h has already been included.
Since the compilation of a module may involve many interchangeable files, this avoids processing the same header multiple times (which would lead to errors from multiple definitions with the same name).
In object-oriented programming
In object-oriented programs, classes are often provided with methods
for getting and setting instance variables.
The definitions of these methods can often be regarded as boilerplate.
Although the code will vary from one class to another, it
is stereotyped enough in structure that it is better generated automatically
than written by hand.
For example, in the following class in
Java
, representing a pet, almost all of the code is boilerplate,
except for the Pet, name and owner declarations:
public class Pet {
private String name;
private Person owner;
public Pet(String name, Person owner) {
this.name = name;
this.owner = owner;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getOwner() {
return owner;
}
public void setOwner(Person owner) {
this.owner = owner;
}
}
Most of the boilerplate in this example exists to provide encapsulation.
If the name and owner variables were declared public, the accessor
and mutator methods would not be needed.
To reduce the amount of boilerplate, many
frameworks have been developed, for example Lombok for Java.
The same code as above is generated automatically by the Lombok framework using
Java annotations, which is a form of metaprogramming:
@AllArgsConstructor
@Getter
@Setter
public class Pet {
private String name;
private Person owner;
}
In some other programming languages the same can be achieved with less
boilerplate, when the language has built-in support for such common constructs.
For example, the equivalent of the Java code above can be expressed in Scala,
using only a single line of code:
case class Pet(var name: String, var owner: Person)
Or in
C#
using automatic properties with compiler-generated backing fields:
public class Pet
{
public string Name { get; set; }
public Person Owner { get; set; }
}
Boilerplate method
In addition to declarations, methods in OOP languages also contribute
to the amount of boilerplate.
A 2015 study of popular Java projects shows
that 60% of methods can be uniquely identified by the appearance of
4.6% of its tokens, which makes the remaining 95.4% boilerplate irrelevant to the logic.
The researchers believe that this result will carry over to subroutines in
procedural languages in general.
HTML
In HTML the following boilerplate template is used as a basic empty template and is present on most web pages:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
</body>
</html>
The Web Hypertext Application Technology Working Group
(WHATWG) in its HTML Living Standard specifies
that the <html>, <head> and <body>
tags can safely be omitted in most cases.
The <meta charset="UTF-8"> tag
can also be omitted if the web server is properly configured to send
the character encoding along with the content type.
The Google HTML/CSS Style Guide recommends omitting all optional
tags, which results in a much more compact template:
<!DOCTYPE html>
<title<>/title>
Article author: Andrei Olegovich