• Post Reply Bookmark Topic Watch Topic
  • New Topic

boilerplate code good or bad  RSS feed

 
Ian Cabigon
Greenhorn
Posts: 25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys,

I have a design question. I've heard of the term "boilerplate code" and tried to search for its meaning. I found out that generally its just code that is repeatedly written. I mean, this is a good idea right? In my point of view, its making use of re-usability. I thought this is something of an advantage.

My question is why do most of programmers/designers consider this a design issue? I keep on seeing methods trying to eliminate or reduce boilerplate code.

Please share some thoughts.

Ian
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boilerplate code points to a block of code that you have to write everytime, usually taking unnecessary space in the main logic. A good example is with JDBC. For every JDBC calls, you'll have to wrap your code in try/catch blocks and explicitly close resources... Everytime.


This is boilerplate code. It takes a lot of space. It's annoying. There are ways to remove that kind of boilerplate code. For example, by wrapping it in a new method.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"code reusability" doesn't mean cut-n-pasting the same chunk of code into dozens of places. What happens if you discover a bug in your 'boilerplate'? How are you going to track it down everywhere?

Code reusability means you write code that you can use in many places, then you call it or link to it. Think of something like the String class. It's a very handy class that is used all the time. You wouldn't want to cut-n-paste it into every application you write, then compile it every time, would you?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Christophe explained, boilerplate code is repetitive code that an API requires you to write, which clutters your program and takes your attention away from the business logic of your program. It makes your program longer and more complex without any benefit. The less boilerplate code an API requires you to write, the better.
 
Ian Cabigon
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

I now see its importance.

This forum rocks!!!

Ian
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boilerplate is evil and wrong. Java has a high boilerplate/actualProblem ratio, although it's getting somewhat better over time. But when compared to more expressive languages, it doesn't fare too well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!