LAME Question of the Day: What is Boilerplate Code?

Title

What is the meaning of Boilerplate Code in computer programming?

Introduction

I was reading about ASP.NET Scaffolding in a book and it describes the scaffolding as "Scaffolding is the process of generating boilerplate code based on your model classes." I thought for a while what this boilerplate code means. I have heard the term 'boilerplate code' several times but didn't know the actual meaning of it.



So, I did a little Googling and tried to search for its meaning. I'm writing this article to share the same with all of you.

Question of the Day

Our lame question of the day is: What is the meaning of Boilerplate Code in computer programming?

Boilerplate Code

The term "boilerplate" originates from the printing industry in the early 1900s. It refers to a unit of text that can be re used again and again without any alteration. It is commonly used in product manuals, disclaimers, copyright statements, end-user license agreements and so on.

Eventually, the idea applied to programming as in "boilerplate code". Boilerplate code is defined as the sections of code that must be included in many places with little or no alteration. The boilerplate code refers to the repetitive code that doesn't really contribute to the logic of the program, but is required by the language or framework.

The most common example is the empty structure of an HTML page. It is present in nearly every web page and is considered boilerplate.

  1. <!DOCTYPE html>   
  2. <html>   
  3.    <head>   
  4.       <title></title>   
  5.    </head>   
  6.    <body> </body>   
  7. </html>  
Criticism over 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 C#, we use to declare normal properties in a class like this:
  1. class Student   
  2. {   
  3.   private string _studentName;   
  4.   public string StudentName   
  5.   {   
  6.      get {return _studentName;}   
  7.      set {_studentName = value;}   
  8.   }   
  9. }  
If we don't have any specific encapsulation logic for the get and set accessors, they are regarded as boilerplate. They take the entire space, yet didn't contain any specific logic about the Student class. They are tedious to write and it would be better generated automatically. With the introduction of auto-implemented properties in C#, we can write the same code as:
  1. class Student   
  2. {   
  3.    public string StudentName{ getset; }   
  4. }   

Conclusion
 
I hope this article helps you to understand the meaning of boilerplate code. Your feedback and constructive criticism is always appreciated, keep it coming. Until then, try to put a ding in the universe! 

Up Next
    Ebook Download
    View all
    Learn
    View all