In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered ''verbose'', the programmer must write a lot of boilerplate code to accomplish only minor functionality.
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 manual boilerplate code).
It is also possible to move boilerplate code to an ''
abstract class'' so that it can be inherited by any number of ''
concrete classes''. Another option would be to move it into a subroutine so that it can be called instead of being duplicated.
Origin
The term arose from the
newspaper
A newspaper is a Periodical literature, periodical publication containing written News, information about current events and is often typed in black ink with a white or gray background. Newspapers can cover a wide variety of fields such as poli ...
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, repeated 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:
#!/usr/bin/env 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 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.
#ifndef MYINTERFACE_H
#define MYINTERFACE_H
...
#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, (which would lead to errors due to multiple definitions with the same name).
In Java and similar platforms
In
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
programs,
DTO 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
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
class representing a pet, almost all the code is boilerplate except for the
declarations of ''Pet'', ''name'', and ''owner'':
Java
public class Pet
Most of the boilerplate in this example exists to fulfill requirements of
JavaBeans. If the variable's name and owner were declared as
public
In public relations and communication science, publics are groups of individual people, and the public (a.k.a. the general public) is the totality of such groupings. This is a different concept to the sociology, sociological concept of the ''Öf ...
, the
accessor and mutator methods would not be needed.
In Java 14, record classes were added to fight with this issue.
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
Scala
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(var name: String, var owner: Person)
C#
Or in
C# using automatic
properties with compiler generated backing fields:
public class Pet
Starting with C# 9.0 there is an opportunity to use Records which generate classes with Properties automatically:
public record Pet(string Name, Person Owner);
Method boilerplate
In addition to declarations,
methods in OOP languages also contribute to the amount of boilerplate. A 2015 study on popular Java projects shows that 60% of methods can be uniquely identified by the occurrence of 4.6% of its tokens, making the remaining 95.4% boilerplate irrelevant to logic. The researchers believe this result would translate to
subroutines in procedural languages in general.
HTML
In
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
, the following boilerplate is used as a basic empty template and is present in most web pages:
Test
The
WHATWG HTML Living Standard defines that the , and tags may be safely omitted under most circumstances. The tag is technically redundant when coming directly from a web server configured to send the character encoding in an HTTP header, though it becomes useful when the HTML response is saved in an
.html
file, cache, or web archive.
Google
Google LLC (, ) is an American multinational corporation and technology company focusing on online advertising, search engine technology, cloud computing, computer software, quantum computing, e-commerce, consumer electronics, and artificial ...
's HTML/CSS style guide recommends that all optional tags be omitted, resulting in much less boilerplate. The
World Wide Web Consortium states that the element must not be empty:
Test
Python
In
Python, the following boilerplate code can be used to modify if code can only be executed in or out of a module context.
if __name__ '__main__':
# Anything placed here will never be executed in a module context.
pass
if __name__ != '__main__':
# Anything placed here will only be executed in a module context.
pass
See also
*
*
*
*
*
*
*
*
*
*
References
{{Reflist
Source code
Computer programming folklore
Software engineering folklore
Articles with example Java code