This article needs additional citations for verification .(May 2017) |
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. [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 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.
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, repeated text. [2] [3]
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:
#!/usr/bin/perlusewarnings;usestrict;
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 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 class representing a pet, almost all the code is boilerplate except for the declarations of Pet, name, and owner:
publicclassPet{privateStringname;privatePersonowner;publicPet(Stringname,Personowner){this.name=name;this.owner=owner;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicPersongetOwner(){returnowner;}publicvoidsetOwner(Personowner){this.owner=owner;}}
Most of the boilerplate in this example exists to fulfill requirements of JavaBeans. If the variable's name and owner were declared as public, the accessor and mutator methods would not be needed.
In Java 14, record classes were added to fight with this issue. [4] [5] [6]
To reduce the amount of boilerplate, many frameworks have been developed, e.g. Lombok for Java. [7] The same code as above is auto-generated by Lombok using Java annotations, which is a form of metaprogramming:
@AllArgsConstructor@Getter@SetterpublicclassPet{privateStringname;privatePersonowner;}
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:
caseclassPet(varname:String,varowner:Person)
Or in C# using automatic properties with compiler generated backing fields:
publicclassPet{publicstringName{get;set;}publicPersonOwner{get;set;}}
Starting with C# 9.0 there is an opportunity to use Records which generate classes with Properties automatically:
publicrecordPet(stringName,PersonOwner);
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. [8]
In HTML, the following boilerplate is used as a basic empty template and is present in most web pages:
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"/><title>Test</title></head><body></body></html>
The WHATWG HTML Living Standard defines that the <html>
, <head>
and <body>
tags may be safely omitted under most circumstances. [9] The <metacharset="UTF-8">
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. [10] Google's HTML/CSS style guide recommends that all optional tags be omitted, [11] resulting in much less boilerplate. The World Wide Web Consortium states that the element <title>
must not be empty: [12]
<!DOCTYPE html><title>Test</title>
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.passif__name__!='__main__':# Anything placed here will only be executed in a module context.pass
A "Hello, World!" program is generally a simple computer program which outputs to the screen a message similar to "Hello, World!" while ignoring any user input. A small piece of code in most general-purpose programming languages, this program is used to illustrate a language's basic syntax. A "Hello, World!" program is often the first written by a student of a new programming language, but such a program can also be used as a sanity check to ensure that the computer software intended to compile or run source code is correctly installed, and that its operator understands how to use it.
A quine is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".
In computing, serialization is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.
Generic programming is a style of computer programming in which algorithms are written in terms of data types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by the ML programming language in 1973, permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplicate code.
Code or text folding, or less commonly holophrasting, is a feature of some graphical user interfaces that allows the user to selectively hide ("fold") or display ("unfold") parts of a document. This allows the user to manage large amounts of text while viewing only those subsections that are currently of interest. It is typically used with documents which have a natural tree structure consisting of nested elements. Other names for these features include expand and collapse, code hiding, and outlining. In Microsoft Word, the feature is called "collapsible outlining".
In class-based, object-oriented programming, a constructor is a special type of function called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
Raku is a member of the Perl family of programming languages. Formerly known as Perl 6, it was renamed in October 2019. Raku introduces elements of many modern and historical languages. Compatibility with Perl was not a goal, though a compatibility mode is part of the specification. The design process for Raku began in 2000.
A webform, web form or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields. For example, forms can be used to enter shipping or credit card data to order a product, or can be used to retrieve search results from a search engine.
Embedded Ruby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP and JSP, and PHP and other server-side scripting languages. The templating system of eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to maintain.
In computer programming, a trait is a concept used in programming languages which represents a set of methods that can be used to extend the functionality of a class.
In computing, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such. For clarity, attributes should more correctly be considered metadata. An attribute is frequently and generally a property of a property. However, in actual usage, the term attribute can and is often treated as equivalent to a property depending on the technology being discussed. An attribute of an object usually consists of a name and a value. For an element these can be a type and class name, while for a file these can be a name and an extension, respectively.
This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.
In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters. The syntax of comments in various programming languages varies considerably.
Haml is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Haml gives you the flexibility to have some dynamic content in HTML. Similar to other template systems like eRuby, Haml also embeds some code that gets executed during runtime and generates HTML code in order to provide some dynamic content. In order to run Haml code, files need to have a .haml extension. These files are similar to .erb or .eRuby files, which also help embed Ruby code while developing a web application.
In computing, Facelets is an open-source Web template system under the Apache license and the default view handler technology for Jakarta Faces. The language requires valid input XML documents to work. Facelets supports all of the JSF UI components and focuses completely on building the JSF component tree, reflecting the view for a JSF application.
A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.
The following outline is provided as an overview of and topical guide to the Perl programming language:
Yesod is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based, high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.
Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice", made possible by the Elm compiler's static type checking.
Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level systems programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.