In computer programming, boilerplate code or boilerplate refers to a section of code that should be included in multiple places with little or no change. This is often used when referring to a language that is considered verbose , ie the programmer must write lots of code to do the minimal work.
The need for boilerplate can be reduced through high-level mechanisms such as metaprogramming (which has the computer automatically writing the required boilerplate code or entering it at compile time), the overconfiguration convention (which provides a good default value, reduces the need to specify program details in each project) and model-driven engineering (which uses model-to-model models and generators, eliminating the need for boilerplate manual code).
Video Boilerplate code
Origin
This term emerged from the newspaper business. Columns and other pieces distributed by a print syndicate are sent to a subscription newspaper in the form of a prepared platen. Because of their resemblance to the metal plate used in boiler making, they are known as "boiler plates", and the resulting text - "boilerplate text". Since the stories distributed by way of prepared dishes are usually "fillers" and not "serious" news, the term becomes synonymous with unoriginal and repetitive text.
Related terms are accounting code , referring to code that is not part of the business logic but inserted with it to keep the data structure updated or handle the secondary aspects of the program.
Maps Boilerplate code
Preamble
One form of boilerplate consists of declarations which, while not part of the program logic or language essential syntax, are added to the start of the source file as a custom problem. The following Perl example shows boilerplate:
The first line is a shebang, which identifies files as Perl scripts that can be executed directly on the command line (on Unix/Linux systems.) The other two are pragma-enabled warnings and strict modes, mandated by Perl-style programming.
The following example is the C/C language boilerplate language, guard #include
.
It organizes, and checks, global flags to tell the compiler whether the myinterface.h file has been entered. Since many interconnected files can be involved in compiling modules, it avoids processing the same header multiple times (which will cause errors due to multiple definitions with the same name).
In object-oriented programming
In object-oriented programs, classes are often given methods for obtaining and managing instance variables. The definition of this method can often be considered a boilerplate. Although the code will vary from one class to another, it is quite stereotyped in structures that would be better generated automatically than written by hand. For example, in the following Java classes representing pets, almost all code is boilerplate except for declarations Pet , name and owner :
Most of the boilerplate in this example exists to provide encapsulation. If the name and owners are declared as public, then accessor and mutator methods are not required.
To reduce the number of boilers, many frameworks have been developed, eg. Lombok for Java. The same code as above is generated automatically by Lombok using Java annotation, which is a metaprogramming form:
In some other programming languages ââit is possible to achieve the same thing with fewer boilers, when the language has built-in support for such general constructs. For example, the equivalent Java code above can be expressed in Scala using only one line of code:
Or in C # use Auto Properties with the backing fields created by the compiler:
HTML
In HTML, the following boilerplate is used as a basic blank template and exists on most web pages:
The WHATWG HTML Living Standard defines that the & lt; html & gt;
, & lt; head & gt;
and & lt; body & gt;
can be safely removed in most circumstances. Tags & lt; meta charset = "UTF-8" & gt;
can also be omitted if the Web server is configured correctly to send character encoding along with content type. The HTML/CSS style guide Google recommends that all optional tags be omitted, resulting in a much more compact boilerplate:
See also
References
Source of the article : Wikipedia