Skip to main content

What is Boilerplate code? Concept summary and examples

What is Boilerplate?

  • Boilerplate refers to the metal plate used by newspaper companies.
  • Newspapers print the same content repeatedly using templates already made on metal plates.

Boilerplate code concept

  • Boilerplate code refers to code that must be written repeatedly every time you start a new project. Boilerplate code is code that is repeated in a similar fashion in several places.
  • This includes, for example, creating the structure of the project, initializing settings, loading libraries, etc. Alternatively, it refers to code that must be written repeatedly when implementing a specific function, such as a Getter & Setter Method.

Boilerplate code example

  • Boilerplate code can occur in various languages.
  • An example of code used as a boilerplate in Java is as follows:
    • Getter & Setter Method
    • equals() and hashCode() Method
    • toString() Method
    • Constructor Method
  • The above codes are codes that must be written repeatedly every time a new class is created.
// 1. Getter & Setter Method
public class Person {
private String name;
private int age;

// Getter Method
public String getName() {
return name;
}

// Setter Method
public void setName(String name) {
this.name = name;
}

// Getter Method
public int getAge() {
return age;
}

// Setter Method
public void setAge(int age) {
this.age = age;
}
}

// 2. equals() and hashCode() Method
public class Person {
private String name;
private int age;

// equals() Method
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}

// hashCode() Method
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}

// 3. toString() Method
public class Person {
private String name;
private int age;

// toString() Method
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

// 4. Constructor Method
public class Person {
private String name;
private int age;

// Constructor Method
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}

How to reduce boilerplate code

  • Boilerplate code is code that must be written repeatedly, so as the amount of code increases, the readability of the code may decrease.
  • In Java, you can use the following methods to reduce boilerplate code.
    • Use IDE (Integrated Development Environment): Boilerplate code can be automatically generated using functions provided by the IDE.
    • Use the Lombok library: You can use the Lombok library to automatically generate Getter & Setter Method, equals() and hashCode() Method, toString() Method, Constructor Method, etc.
    • Use Annotation Processor: Boilerplate code can be automatically generated using the Annotation Processor.
    • Use Template Engine: Boilerplate code can be automatically generated using Template Engine.
  • In Java, you can use the Lombok library to reduce the Boilerplate code as shown below.
    • Using @Data Annotation, you can automatically create Getter & Setter Method, equals() and hashCode() Method, toString() Method, and Constructor Method.
    • You can reduce code writing just by using @Data Annotation as shown below.
import lombok.Data;

@Data
public class Person {
private String name;
private int age;
}
  • Depending on the language or framework, methods to reduce boilerplate code may vary. Find ways to reduce boilerplate code by using features provided by the language or framework you're using.