Reading various articles I sometimes come across such an expression:

Boilerplate code

In my team, clearly no one could explain this concept. What is it?

3 answers 3

In Wikipedia, everything is written

The concept boilerplate code or boilerplate refers to sections of code that should be written in many places with minimal changes. It is often used in relation to languages ​​in which a programmer must write a lot of code in order to perform a minimal task.

Many modern IDEs automatically generate a boilerplate code. There is also such a thing as a scaffold that allows you to generate a boilerplate (for example, in Ruby on Rails you can generate basic CRUD methods in controllers).

Examples:

html

<!DOCTYPE html> <title>Что такое Boilerplate code?</title> 

Boilerplate code, which we need to repeat in each pattern. ( Proof )

Java

 public class Pet { private String name; private Person owner; public Pet(String name, Person owner) { this.name = name; this.owner = owner; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Person getOwner() { return owner; } public void setOwner(Person owner) { this.owner = owner; } } 

PS: this is the main reason why I once abandoned programming in Java.

  • 2
    In the wiki in English, and I wanted to Russian. And in what language will you not need to write the boilerplate code for the above example? - koks_rs
  • 2
    For html5, this skin is optional. - Qwertiy
  • 2
    @Viktor, I will pass the validation. The standard clearly states which tags are not necessary to write and they will be added automatically. The minimum valid html document looks like this: <!doctype html><title>0</title> - title should not be empty. validator.w3.org/nu/#textarea - Qwertiy
  • one
    @Qwertiy thank you, did not know this - Viktor
  • one
    Boilerplate has been successfully overcome by libraries such as Lombok in Java. - naut92

Template code that should be written in many places with little or no changes. The programmer has to write this code every time, as a rule, taking up unnecessary space, cluttering up the program and diverting attention from business logic. This makes the program more complex, without any benefit.

  • one
    That is, is it a negative concept that needs to be avoided in code? - koks_rs
  • @koks_rs, this concept is used with negative coloring, of course, the less verbose the language, the better, but often the api libraries or frameworks are written in such a way that avoiding the boilerplate code will not work. - katso
  • four
    I would not say that it is always negative. - Qwertiy

Often there are projects on hitkhab in which a lot of things are done from what is often found in the average project, these projects were made to be copied and based on them to do their projects, not to bother with the same things when you start to make a project. This project is also called starter.

Here are examples: html5-boilerplate , react-boilerplate , electron-react-boilerplate , laravel-5-boilerplate , hackathon-starter .

Pros:

  • Many packages are already delivered and configured.
  • A lot of code is written for you.
  • ̶̶̶̶̶ ̶т̶а̶к̶ ̶m̶n̶o̶g̶o̶ ̶k̶o̶s̶t̶y̶l̶̶y̶ ̶v̶ ̶k̶̶̶̶̶̶ k̶a̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶

Minuses:

  • It happens in the starter includes tools that you do not like or do not want to use now, you have to cut out
  • Sometimes it is not clear thanks to what code something works.
  • It can be hard to remake the code in your own way.
  • No confidence in the code because I did not write it myself