Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is boilerplate code?

A coworker had never heard of this, and I couldn't provide a real definition. For me, it's always been an instance of 'I-know-it-when-I-see-it'.

Bonus question, who originated the term?

like image 659
Nate Parsons Avatar asked Oct 21 '10 21:10

Nate Parsons


People also ask

What does boilerplate mean code?

In computer programming, boilerplate code or boilerplate refers to sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.

What is boilerplate code Java?

A boilerplate code usually refers to a pile of code blocks with a fixed pattern that can be widely applied to various program modules.

Is boilerplate code bad?

The problem with boilerplate is that it violates DRY. In essence, when you write boilerplate, you're repeating the same code (or very similar code) across a number of classes. When that code needs to get changed, it's not at all certain that the developer will remember all of the places that code was repeated.


2 Answers

"boilerplate code" is any seemingly repetitive code that shows up again and again in order to get some result that seems like it ought to be much simpler.

It's a subjective definition.

The term comes from "boilerplate" in the newspaper industry: wiki

like image 161
Adam Vandenberg Avatar answered Sep 28 '22 17:09

Adam Vandenberg


Boilerplate code means a piece of code which can be used over and over again. On the other hand, anyone can say that it's a piece of reusable code.

The term actually came from the steel industries.

For a little bit of history, according to Wikipedia:

In the 1890s, boilerplate was actually cast or stamped in metal ready for the printing press and distributed to newspapers around the United States. Until the 1950s, thousands of newspapers received and used this kind of boilerplate from the nation's largest supplier, the Western Newspaper Union. Some companies also sent out press releases as boilerplate so that they had to be printed as written.

Now according to Wikipedia:

In object-oriented programs, 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:

public class Pet {     private PetName name;     private Person owner;      public Pet(PetName name, Person owner) {         this.name = name;         this.owner = owner;     }      public PetName getName() {         return name;     }      public void setName(PetName name) {         this.name = name;     }      public Person getOwner() {         return owner;     }      public void setOwner(Person owner) {         this.owner = owner;     } } 
like image 26
onCompletion Avatar answered Sep 28 '22 18:09

onCompletion