Boilerplate code
Encyclopedia
In computer programming, boilerplate is the term used to describe sections of code that have to be included in many places with little or no alteration. It is more often used when referring to languages which are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs. The need for boilerplate can be reduced through high-level mechanisms such as metaprogramming
(which has the computer automatically write the needed boilerplate text), 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).
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.
example demonstrates boilerplate:
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 pragma
s turning on warnings and strict mode, which are mandated by good Perl programming style
.
Preambles often include imports as 'headings boilerplate', that is, declarations of which libraries are being used. For example the following lines are very frequently included in C
programs:
This tells the compiler to include the files stdio.h and stdlib.h, which contain declarations of the types of commonly used library functions, making them available for use in the rest of the source files.
, 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 declaration
s of Pet, name and owner:
Note that in this specific case, if the variables name and owner were declared as public, these 4 methods would not be needed, but the code would break encapsulation
.
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:
Or in C-Sharp (C#) using properties
with compiler generated backing fields:
, the following boilerplate is used as a basic empty template and is present in most websites.
Metaprogramming
Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at compile time that would otherwise be done at runtime...
(which has the computer automatically write the needed boilerplate text), convention over configuration
Convention over Configuration
Convention over configuration is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility....
(which provides good default values, reducing the need to specify program details in every project) and model-driven engineering
Model-driven engineering
Model-driven engineering is a software development methodology which focuses on creating and exploiting domain models , rather than on the computing concepts...
(which uses models and model-to-code generators, eliminating the need for boilerplate manual code).
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
Aspect (computer science)
In computer science, an aspect of a program is a feature linked to many other parts of the program, but which is not related to the program's primary function. An aspect crosscuts the program's core concerns, therefore violating its separation of concerns that tries to encapsulate unrelated functions...
of the program.
Preambles
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 PerlPerl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
example demonstrates boilerplate:
The first line is a shebang
Shebang (Unix)
In computing, a shebang is the character sequence consisting of the characters number sign and exclamation point , when it occurs as the first two characters on the first line of a text file...
, 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 pragma
Directive (programming)
In computer programming, the term directive is applied in a variety of ways that are similar to the term command. It is also used to describe some programming language constructs ....
s turning on warnings and strict mode, which are mandated by good Perl programming style
Programming style
Programming style is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers to read and understand source code conforming to the style, and help to avoid introducing errors.A...
.
Preambles often include imports as 'headings boilerplate', that is, declarations of which libraries are being used. For example the following lines are very frequently included in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
programs:
This tells the compiler to include the files stdio.h and stdlib.h, which contain declarations of the types of commonly used library functions, making them available for use in the rest of the source files.
In object-oriented programming
In object-oriented programsObject-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, classes are often provided with methods for getting and setting
Mutator method
In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation...
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 (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
class representing a pet, almost all the code is boilerplate except for the declaration
Declaration (computer science)
In programming languages, a declaration specifies the identifier, type, and other aspects of language elements such as variables and functions. It is used to announce the existence of the element to the compiler; this is important in many strongly-typed languages that require variables and their...
s of Pet, name and owner:
Note that in this specific case, if the variables name and owner were declared as public, these 4 methods would not be needed, but the code would break encapsulation
Encapsulation (object-oriented programming)
In a programming language encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:* A language mechanism for restricting access to some of the object's components....
.
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:
Or in C-Sharp (C#) using properties
Property (programming)
A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field and a method. Properties are read and written like fields, but property reads and writes are translated to get and set method calls...
with compiler generated backing fields:
HTML
In HTMLHTML
HyperText Markup Language is the predominant markup language for web pages. HTML elements are the basic building-blocks of webpages....
, the following boilerplate is used as a basic empty template and is present in most websites.