The Problems with Boilerplate Code

Copy. Paste. Modify. Repeat. Writing and maintaining boilerplate code is a necessary evil in VBA.

The Problems with Boilerplate Code

Being an older programming language, VBA lacks some modern features, such as generic objects and first-class functions.

The fact that these features are missing from the language does not make VBA any less capable than other languages.  VBA is Turing-complete, after all.

Rather, these "missing" features often manifest themselves in the form of additional boilerplate code that may not otherwise be necessary in other languages.

There's nothing wrong with boilerplate code per se.  However, it does have some drawbacks:

  • Writing boilerplate code is tedious
  • More code equals more opportunities for bugs
  • Paradoxically, you're more likely to make silly mistakes when performing mindless tasks (like copy-pasting-modifying boilerplate code) than when your mind is fully engaged in a complex programming challenge
  • Refactoring boilerplate code is a fraught endeavor, as the refactoring exercise suffers from all three of the above disadvantages but does so at scale

If only there were a way to produce boilerplate code that did not suffer from these downsides...

Referenced articles

Writing Boilerplate Code by Hand is for Suckers
Writing repetitive code may be a necessary evil in VBA, but it’s a lot less tedious if you generate most of it in VBA itself.

Image by Bruno /Germany from Pixabay

UPDATE [2022-10-17]: Add links to article "Writing Boilerplate Code by Hand is for Suckers."

All original code samples by Mike Wolfe are licensed under CC BY 4.0