What is Boilerplate code?

Asked

Viewed 15,467 times

40

I have noticed the frequent use of the term in some forums and I was wondering what its meaning and where it came from.

3 answers

59


"Boilerplate" is a term often used to refer to excerpts of documents (for example, legal documents) that are always the same, from document to document, so that they do not add much but still cannot be omitted. You may have already seen in software licenses, for example, the part that says "THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN..." etc. Everyone already knows, nobody even reads because they already know by heart what is written, but the document would not be complete without it.

When applying this term to the code, it may have an "innocent" meaning (as shown in reply by @Andersonbs, referring to a ready-made pattern, a skeleton, from which you do the rest), but also a pejorative sense: it’s that code you need to write every time - because the syntax of the language requires it - but it also doesn’t add much, since it’s the same in every program. In general, when it is said that a language or platform has much Boilerplate if it is saying that it is understated ("talks too much to say too little").

For example, see the program "hello world" in Python:

print("Olá, Mundo!")

The same program in Java:

public class OlaMundo {
    public static void main(String[] args) {
        System.out.println("Olá, Mundo!");
    }
}

And the same program in C#:

using System;

namespace Teste
{
    class OlaMundo
    {
        static void Main()
        {
            Console.WriteLine("Olá, Mundo!");
        }
    }
}

Source: Wikipedia

In all three cases there is only one "useful line": the one that prints the string on the output. Everything else is Boilerplate. Code you will have to write again and again and again every time you create a program. Even if your IDE (development environment) creates the skeleton for you, it’s still a useless stretch that’s sitting there taking up space, taking time from the build and distracting your attention from what the code does useful.

Note: the presence of some Boilerplate does not automatically make the language bad; it is only one clue that probably this language requires you to write more than necessary to perform the same function, compared to others with less Boilerplate.

  • Very good explanation!! Thank you

28

The term derives from steel manufacturing, where Boilerplate is steel rolled into large plates for use in steam boilers.

In information technology, "Boilerplate code" is a chunk of code that can be reused several times without any or with little change in its consistency. It is assumed that this code piece has been tested numerous times and is as consistent as steel.

1

In English the expression Boilerplate means a template, that is, a standard way of writing something that can be copied, used mainly in legal contracts and documents. Those who deal with heaters and boilers (boilers) know that these equipment have operational risks and very strict operating limits. There is also specific legislation for its use and, one of the most important rules and that must be fixed in the equipment a plate (Plate) containing important data of its operation, as temperature and maximum pressure of operation, used fuel type, volume, etc... This board has to contain a minimum of information and be written in a certain way. But almost no one reads this information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.