ArticleslgStudy

computer science

Boilerplate code

Boilerplate code is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Boilerplate code rather than just read about it. In short: 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.

Key takeaways

  • Boilerplate code belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Boilerplate code to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Boilerplate code from memory before moving on to harder problems.

Reference excerpt

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 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:

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.

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 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:

Java

Most of the boilerplate in this example exists to fulfill requirements of JavaBeans. If the variables 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. 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:

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:

C / C++ An include guard:

C# Or in C# using automatic properties with compiler generated backing fields:

Starting with C# 9.0 there is an opportunity to use Records which generate classes with Properties automatically:

HTML In HTML, the following boilerplate is used as a basic empty template and is present in most web pages:

The WHATWG HTML Living Standard defines that the <html>, <head> and <body> tags may be safely omitted under most circumstances. The <meta charset="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. Google'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 <title> must not be empty:

See also

References

Worked examples

Example 1 — a first encounter with Boilerplate code

Start with the simplest possible case. Write down what Boilerplate code claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Boilerplate code before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Boilerplate code ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Boilerplate code

In research
Boilerplate code appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Boilerplate code in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Boilerplate code is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer programming folklore, Software engineering folklore, Source code, so understanding it makes those chapters shorter.
In everyday life
Look for Boilerplate code outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Boilerplate code” →

How to study Boilerplate code in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Boilerplate code means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Boilerplate code out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Boilerplate code in simple terms?

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.

Why does Boilerplate code matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Boilerplate code?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Boilerplate code.

Tags

  • Computer programming folklore
  • Software engineering folklore
  • Source code

Keep exploring