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.
- #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 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).
- 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 functions. Now see the following 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 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.
- 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 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.