Search code examples
c#boilerplate

What's the motivation behind differentiating any regular code and boilerplate code?


From the information available online it isn't difficult to understand what boilerplate code is. What I don't understand is why was there any need to name a repeated section of code as boilerplate code? I would be surprised if someone coined the term "boilerplate code" just for the sake of it. There must be some reason which I am unable to get my head around at the moment.

Does being aware that a piece of code is boilerplate help a developer in any way? Is there something the dev needs to be careful about when writing boilerplate code?

In the examples of boilerplate code that I have seen, it seems that this code is unavoidable. If the code is unavoidable and is existing because of the mere fact that one is coding, what's the motivation behind emphasizing on it?


PS: Answers with examples in C# would be very beneficial.


Solution

  • In one line, the motivation is reusability and automation.

    Example: In ORM, the attributes in a database table will become the fields of a class. Therefore, instead of writing getters and setters (which will be boilerplate code) manually, one can write code to generate boilerplate code and automate this process. Now, when some time later the table is added/subtracted columns, the corresponding getters and setters will be then automatically added/removed.



    Longer version, addressing each point separately:

    What I don't understand is why was there any need to name a repeated section of code as boilerplate code?

    First thing is to understand that this "repeated" section of code does not mean identical. Repeated here would mean the kind of code which can be generated by some logic. One of the best examples is already mentioned above - getters and setters for each field of class where the class maps to a database table.

    After one understands that, the need to differentiate between regular code and boilerplate code becomes obvious - Reusing as well as automating the generation of boilerplate code.


    Does being aware that a piece of code is boilerplate help a developer in any way?

    Yes, of course. The developer can choose to automate it. A great example of such is Template Metaprogramming. Don't be afraid by that term - it concerns generating source code automatically. Pretty common in projects with ORM.


    Is there something the dev needs to be careful about when writing boilerplate code?

    Yes. Don't write it. Instead write code for generating it automatically wherever possible.


    In the examples of boilerplate code that I have seen, it seems that this code is unavoidable. If the code is unavoidable and is existing because of the mere fact that one is coding, what's the motivation behind emphasizing on it?

    This question is because you misunderstood what boilerplate code really was. Now, I hope, your understanding has improved and so you probably can guess that the answer to this question is the very first line of this answer (aka reusability and automation).



    Also, I'd suggest you to read the comments by @JerryJeremiah and @Marged on your question. With improved understanding of what all sections of code in a solution/project/class can be factored out as boilerplate code, you will find their comments pretty useful now.