What Boilerplate Code Is

Introduction

I was doing some online research to fix a bug in my code and I just came across this term Boilerplate Code. I thought for a while what boilerplate code is. This term was new for me so I just used Google to find what this term actually means.

I will be sharing that with all of you.

Questions

What does Boilerplate Code mean in computer programming?

Boilerplate Code

The term boilerplate originates from the printing industry in the early 1900s. Boilerplate Code is any seemingly repetitive code that shows up again and again to get some result that seems like it ought to be much simpler. It's the code that can be used by many applications/contexts with little or no change.

Eventually, boilerplate code refers to the repetitive code that can be avoided by optimizing code. Boilerplate is lack of implementing programing logic in a more optimized way.

You can see below an example of boilerplate code. I created a program in C++ to add two numbers.

  1. #include<iostream.h>  
  2. #include<conio.h>  
  3. void main()  
  4. {  
  5.     clrscr();  
  6.     int a, b, sum;  
  7.     cout<<"Enter the values of A and B: ";  
  8.     cin>>a>>b;  
  9.     sum = a + b;  
  10.     cout<<"Sum is: "<<sum;  
  11.     cout<<endl;  
  12.     cout<<"Enter the values of A and B: ";  
  13.     cin>>a>>b;  
  14.     sum = a + b;  
  15.     cout<<"Sum is: "<<sum;  
  16.     cout<<endl;  
  17.     cout<<"Enter the values of A and B: ";  
  18.     cin>>a>>b;  
  19.     sum = a + b;  
  20.     cout<<"Sum is: "<<sum;  
  21.     cout<<endl;  
  22.     cout<<"Enter the values of A and B: ";  
  23.     cin>>a>>b;  
  24.     sum = a + b;  
  25.     cout<<"Sum is: "<<sum;  
  26.     cout<<endl;  
  27.    
  28.     getch();  

If you see in the preceding code, I'm repeatedly calling this code, syntax/logic whatever you call it. Here I'm fetching two values from the user, adding them and printing the result. Assuming I want to do this task around 10-20 times, I need to repeat this set of code about 10-20 times. Correct?

Now when the logic is the same, repeating the code 10-20 times makes no sense. This is what is referred to as Boilerplate Code (lack of implementing logic or repeating code).

  1. cout<<"Enter the values of A and B: ";  
  2. cin>>a>>b;  
  3. sum = a + b;  
  4. cout<<"Sum is: "<<sum;  
  5. cout<<endl; 

We have a very special feature in programming languages know as functions. Now see the following code:

  1. #include<iostream.h>  
  2. #include<conio.h>  
  3. #include<stdio.h>  
  4. void add();  
  5. void main()  
  6. {  
  7.    clrscr();  
  8.    char ch='y';  
  9.    while(ch=='Y' || ch=='y')  
  10.    {  
  11.       add();  
  12.       cout<<"Do you want to continue? ";  
  13.       cin>>ch;  
  14.       cout<<endl;  
  15.    }  
  16.    fflush(stdin);  
  17.    getch();  
  18. }  
  19. void add()  
  20. {  
  21.    int a, b, sum;  
  22.    cout<<"Enter two numbers:";  
  23.    cin>>a>>b;  
  24.    sum=a+b;  
  25.    cout<<endl;  
  26.    cout<<"Sum of two numbers is: "<<sum<<endl;  

Now if you see in the preceding code, I have optimized it by putting that addition logic in the function named add.

Now what I did is just called the function add() wherever and whenever I require. In this code I used a while loop that will execute until the value of ch remains "y" or "Y" . The moment the value of ch changes the program will terminate, in other words when I execute this program it will always ask "Do you want to continue?" and it depends on me how much I want this program to run.

What I want to convey here is that, the following piece of code (in other words the main logic of this program), is placed inside the function that prevents the repetition of code.

  1. void add()  
  2. {  
  3.    int a, b, sum;  
  4.    cout<<"Enter two numbers:";  
  5.    cin>>a>>b;  
  6.    sum=a+b;  
  7.    cout<<endl;  
  8.    cout<<"Sum of two numbers is: "<<sum<<endl;  

Problem encountered with Boilerplate Code

The boilerplate code indicates tedium and a violation of the “Don't Repeat Yourself” principle. When we write boilerplate code, we are actually repeating the same code again and again across the project. When that code needs to be changed, it's very difficult to remember all of the places where the code was written.

In simple language we can say that boilerplate code is not a limitation of the language but it is a limitation of logic.

Up Next
    Ebook Download
    View all
    Learn
    View all