What is boilerplate code?

27

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

    
asked by Geison Santos 26.03.2014 в 06:03
source

3 answers

40

"Boilerplate" is a term often used to refer to excerpts of documents (eg legal) that are always the same, from document to document, so that they do not add much but can not be omitted. You should 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 it already knows by heart what is written, but the document would not be complete without it.

When you apply this term to the code, it may have an "innocent" meaning (as shown in the AndersonBS response a>, referring to a ready pattern, a skeleton, from which you do the rest), but also a pejorative sense: it is that code you need to write every time - because the syntax of language requires - but also not adds a lot, since it is the same in every program. In general, when it is said that a language or platform has a lot of boilerplate, it is saying that it is little expressive .

As an example, see the "hello world" program in Python:

print("Olá, Mundo!")

The same Java program:

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 in the output. All the rest is boilerplate. Code that 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 snippet that's taking up space, taking compile time and distracting your attention from what the code does for usefulness.

Note: the presence of some boilerplate does not automatically make the language bad; it's just a clue that probably this language forces you to write more than necessary to perform the same function, compared to others with less boilerplate.

    
answered by 26.03.2014 / 08:54
source
23

The term derives from the manufacture of steel, where boilerplate is steel rolled into large plates for use in steam boilers.

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

    
answered by 26.03.2014 в 06:17
0

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

    
answered by 19.10.2018 в 16:48