Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Boilerplate code

From Wikipedia, the free encyclopedia
Code that has to be included in many places with little or no alteration
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Boilerplate code" – news ·newspapers ·books ·scholar ·JSTOR
(May 2017) (Learn how and when to remove this message)

In computer programming,boilerplate code, or simplyboilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are consideredverbose, 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 asmetaprogramming (which has the computer automatically write the needed boilerplate code or insert it atcompile time),convention over configuration (which provides good default values, reducing the need to specify program details in every project) andmodel-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 anabstract class so that it can be inherited by any number ofconcrete classes. Another option would be to move it into a subroutine so that it can be called instead of being duplicated.

Origin

[edit]
Further information:Boilerplate (text)

The term arose from thenewspaper business. Columns and other pieces that were distributed byprint syndicates were sent to subscribing newspapers in the form of preparedprinting plates. Because of their resemblance to the metal plates used in the making ofboilers, 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 isbookkeeping code, referring to code that is not part of thebusiness logic but is interleaved with it in order to keep data structures updated or handle secondaryaspects of the program.

Preamble

[edit]

One form of boilerplate consists of declarations which, while not part of the program logic or the language's essentialsyntax, are added to the start of a source file as a matter of custom. The followingPerl example demonstrates boilerplate:

#!/usr/bin/env perlusewarnings;usestrict;

The first line is ashebang, which identifies the file as a Perl script that can be executed directly on the command line on Unix/Linux systems. The other two arepragmas turning on warnings and strict mode, which are mandated by fashionable Perlprogramming 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

[edit]

InJava programs,DTO classes are often provided with methods forgetting 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 followingJava class representing a pet, almost all the code is boilerplate except for thedeclarations ofPet,name, andowner:

Java

[edit]
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 ofJavaBeans. If the variablesname andowner were declared aspublic, theaccessor 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 usingJava annotations, which is a form ofmetaprogramming:

@AllArgsConstructor@Getter@SetterpublicclassPet{privateStringname;privatePersonowner;}

Scala

[edit]

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 inScala using just one line of code:

caseclassPet(varname:String,varowner:Person)

C#

[edit]

Or inC# using automaticproperties 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);

HTML

[edit]

InHTML, 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>

TheWHATWG HTML Living Standard defines that the<html>,<head> and<body> tags may be safely omitted under most circumstances.[8] 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.[9]Google's HTML/CSS style guide recommends that all optional tags be omitted,[10] resulting in much less boilerplate. TheWorld Wide Web Consortium states that the element<title> must not be empty:[11]

<!DOCTYPE html><title>Test</title>

See also

[edit]

References

[edit]
  1. ^Lämmel, Ralf; Jones, Simon Peyton (2003)."Scrap your boilerplate: a practical design pattern for generic programming".Proceedings of the 2003 ACM SIGPLAN International Workshop on Types in Languages Design and Implementation. TLDI '03. New York:ACM. pp. 26–37.doi:10.1145/604174.604179.ISBN 9781581136494.S2CID 9472305.
  2. ^"Boilerplate".Dictionary.com. Retrieved2018-01-27.
  3. ^"Boilerplate".Merriam-Webster. Retrieved2018-01-27.
  4. ^"Record Classes".docs.oracle.com.
  5. ^"JEP 395: Record".openjdk.org.
  6. ^Evans, Ben (2020-11-01)."Records Come to Java".blogs.oracle.com.
  7. ^Frankel, Nicolas (2009-12-07)."Lombok reduces your boilerplate code".DZone.com. Retrieved2017-08-02.
  8. ^"HTML Standard - The HTML syntax - Optional tags".WHATWG. 2017-05-05. Retrieved2017-05-05.
  9. ^"Is the charset meta tag required with HTML5?".stackoverflow.com. Retrieved2017-05-05.
  10. ^"Google HTML/CSS Style Guide".google.github.io. Retrieved2017-05-05.
  11. ^"HTML page has non-empty title".www.w3.org. Retrieved22 July 2021.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Boilerplate_code&oldid=1335261906"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp