Boilerplate code

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer programming, boilerplate code or boilerplate refers to sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.[1]

The need for boilerplate can be reduced through high-level mechanisms such as metaprogramming (which has the computer automatically write the needed boilerplate code or insert it at compile time), convention over configuration (which provides good default values, reducing the need to specify program details in every project) and model-driven engineering (which uses models and model-to-code generators, eliminating the need for boilerplate manual code).


The term arose from the newspaper business. Columns and other pieces that were syndicated were sent out to subscribing newspapers in the form of a mat (i.e. a matrix). Once received, boiling lead was poured into this mat to create the plate used to print the piece, hence the name boilerplate. As the article printed on a boilerplate could not be altered, the term came to be used by attorneys to refer to the portions of a contract which did not change through repeated uses in different applications, and finally to language in general which did not change in any document that was used repeatedly for different occasions.[citation needed]

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.


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:

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 (on Unix/Linux systems.) 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.




This sets up, and checks, 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 (which would lead to errors due to multiple definitions with the same name).

In object-oriented programming[edit]

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 {
    private String name;
    private Person owner;

    public Pet(String name, Person owner) { = name;
        this.owner = owner;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Person getOwner() {
        return owner;

    public void setOwner(Person owner) {
        this.owner = owner;

Note that 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.[2] The same code as above is auto-generated by Lombok using Java annotations, which is a form of metaprogramming:

public class Pet {
    private String name;
    private Person owner;

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:

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; }


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

<!DOCTYPE html>
  <meta charset="UTF-8"/>


The WHATWG HTML Living Standard defines that the <html>, <head> and <body> tags may be safely omitted under most circumstances.[3] The <meta charset="UTF-8"> tag may also be omitted if the Web server is properly configured to send the character encoding along with the content type.[4] Google's HTML/CSS style guide recommends that all optional tags be omitted,[5] resulting in a much more compact boilerplate:

<!DOCTYPE html>

See also[edit]


  1. ^ Lämmel, Ralf; Jones, Simon Peyton (2003). "Scrap your boilerplate: a practical design pattern for generic programming" (PDF). Proceedings of the 2003 ACM SIGPLAN International Workshop on Types in Languages Design and Implementation. TLDI '03. New York: ACM. pp. 26–37. ISBN 9781581136494. doi:10.1145/604174.604179. 
  2. ^ Frankel, Nicolas (2009-12-07). "Lombok reduces your boilerplate code". Retrieved 2017-08-02. 
  3. ^ "HTML Standard - The HTML syntax - Optional tags". WHATWG. 2017-05-05. Retrieved 2017-05-05. 
  4. ^ "Is the charset meta tag required with HTML5?". Retrieved 2017-05-05. 
  5. ^ "Google HTML/CSS Style Guide". Retrieved 2017-05-05.