What Does Boilerplate Code Means ?

Title

What does Boilerplate Code means ?

Introduction

I was doing some online research in-order to fix some bug in my code, and I just came across this term Boilerplate Code. I thought for a while what is this boilerplate code, this term was new for me so I just Google it to find what does this term actually means.
I’ll be sharing the same with all of you.

Questions

What does Boilerplate Code means in computer programming?

Boilerplate Code

The term boilerplate originates from the printing industry is the early 1900s. 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 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 more optimized way.

Below you can see the example of boilerplate code. I created a program in C++ to add two numbers.

 

 


#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int a, b, sum;
    cout<<"Enter the values of A and B: ";
    cin>>a>>b;
    sum = a + b;
    cout<<"Sum is: "<<sum;
    cout<<endl;
    cout<<"Enter the values of A and B: ";
    cin>>a>>b;
    sum = a + b;
    cout<<"Sum is: "<<sum;
    cout<<endl;
    cout<<"Enter the values of A and B: ";
    cin>>a>>b;
    sum = a + b;
    cout<<"Sum is: "<<sum;
    cout<<endl;
    cout<<"Enter the values of A and B: ";
    cin>>a>>b;
    sum = a + b;
    cout<<"Sum is: "<<sum;
    cout<<endl;

    getch();
}

 

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

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

cout<<"Enter the values of A and B: ";
cin>>a>>b;
sum = a + b;
cout<<"Sum is: "<<sum;
cout<<endl;

 

We have a very special feature in programming languages know as function. Now see the below code…


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void add();
void main()
{
clrscr();
char ch='y';
while(ch=='Y' || ch=='y')
{
add();
cout<<"Do you want to continue? ";
cin>>ch;
cout<<endl;
}
fflush(stdin);
getch();
}
void add()
{
int a, b, sum;
cout<<"Enter two numbers:";
cin>>a>>b;
sum=a+b;
cout<<endl;
cout<<"Sum of two numbers is: "<<sum<<endl;
}

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

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

 

What I want to convey here is that, below piece of code (i.e. the main logic of this program), is placed inside the function which prevents the repetition of code.


void add()
{
int a, b, sum;
cout<<"Enter two numbers:";
cin>>a>>b;
sum=a+b;
cout<<endl;
cout<<"Sum of two numbers is: "<<sum<<endl;
}

Problem faced 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 limitation of language but it is the limitation of a logic.

 

Hope this article was helpful, if you feel that this post require some modification, feel free to contact.


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar