Boilerplate code


In computer programming, boilerplate code or just boilerplate are sections of code that have to be included in many places with little or no alteration. When using languages that are considered verbose, the programmer must write much code to accomplish only minor functionality. Such code is called boilerplate.
The need for boilerplate can be reduced through high-level mechanisms such as metaprogramming, convention over configuration and model-driven engineering.

Origin

The term arose from the newspaper business. Columns and other pieces that were distributed by print syndicates were sent to subscribing newspapers in the form of prepared printing plates. Because of their resemblance to the metal plates used in the making of boilers, they became known as "boiler plates", and their resulting text - "boilerplate text". As the stories that were distributed by boiler plates 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 keep data structures updated or handle secondary aspects of the program.

Preamble

One form of boilerplate consists of declarations which, while not part of the program logic or the language's essential syntax, are added to the start of a source file as a matter of custom. The following Perl example demonstrates boilerplate:

  1. !/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 on the command line The other two are pragmas turning on warnings and strict mode, which are mandated by fashionable Perl programming style.
This next example is a C/C++ programming language boilerplate, #include guard.

  1. ifndef MYINTERFACE_H
  2. define MYINTERFACE_H
...
  1. endif

This checks, and sets up, a global flag to tell the compiler whether the file myinterface.h has already been included. As many interdepending files may be involved in the compilation of a module, this avoids processing the same header multiple times.

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 frequently be regarded as boilerplate. Although the code will vary from one class to another, it is sufficiently stereotypical in structure that it would be better generated automatically than written by hand. For example, in the following Java class representing a pet, almost all the code is boilerplate except for the declarations of Pet, name and owner:

public class Pet

Most of the boilerplate in this example exists to provide encapsulation. If the variables name and owner were declared as public, the accessor and mutator methods would not be needed.
To reduce the amount of boilerplate, many frameworks have been developed, e.g. Lombok for Java. The same code as above is auto-generated by Lombok using Java annotations, which is a form of metaprogramming:

@AllArgsConstructor
@Getter
@Setter
public class Pet

In some other programming languages it may be possible to achieve the same thing with less boilerplate, when the language has built-in support for such common constructs. For example, the equivalent of the above Java code can be expressed in Scala using just one line of code:

case class Pet

Or in C# using Automatic Properties with compiler generated backing fields:

public class Pet

HTML

In HTML, the following boilerplate is used as a basic empty template and is present in most web pages:











The WHATWG HTML Living Standard defines that the , and tags may be safely omitted under most circumstances. The tag may also be omitted if the Web server is properly configured to send the character encoding along with the content type. Google's HTML/CSS style guide recommends that all optional tags be omitted, resulting in a much more compact boilerplate: