DEV Community

CodingBlocks

The Pragmatic Programmer – How to Generate Code

We continue our dive into The Pragmatic Programmer and debate when is it text manipulation vs code generation as Joe can’t read his bill, Michael makes a painful recommendation, and Allen’s gaming lives up to Southern expectations.

In case you’re reading these show notes via your podcast player, you can find this episode’s full show notes at https://www.codingblocks.net/episode112 and join in on the conversation.

Sponsors

  • Clubhouse – The first project management platform for software development that brings everyone on every team together to build better products. Sign up for two free months of Clubhouse by visiting clubhouse.io/codingblocks.
  • Datadog.com/codingblocks – Sign up today for a free 14 day trial and get a free Datadog t-shirt after creating your first dashboard.

Survey Says …

Anonymous Vote
Sign in with Wordpress
What native language are you most interested in?
  • Rust, because safety first.
  • Go, because I wanna be fast and parallel.
  • C, the old ways are best.
  • C++, the good parts.
  • No thanks, I have deadlines.
  • You forgot mine, you expletives.

News

  • We really appreciate every review we get. Thank you for taking the time.
    • iTunes: AsIRoseOneMorn, MrBramme, MP7373, tbone189, BernieF1982, Davidwrpayne, mldennison
    • Stitcher: Ben T, moreginger, Tomski, Java Joe

Blurring the Text Manipulation Line

Text Manipulation

  • Programmers manipulate text the same way woodworkers shape wood.
    • Text manipulation tools are like routers: noisy, messy, brutish.
  • You can use text manipulation tools to trim the data into shape.
    • Once you master them, they can provide an impressive amount of finesse.
  • Alternative is to build a more polished tool, check it in, test it, etc.
Tip 28
  • Learn a Text Manipulation Language.

Code Generators

  • When you have a repetitive task, why not generate it?
  • The generated code takes away complexity and reduces errors.
  • And it’s reuse has little to no additional cost.
Tip 29
  • Write Code That Writes Code.

There are two types of code generators:

  1. Passive code generators are run once (scaffolding).
  2. Active code generators are used each time they are required.

Passive code generators save typing by automating…

  • New files from a template, i.e. the “File -> New” experience.
  • One off conversions (one language to another).
    • These don’t need to be completely perfect.
  • Producing lookup tables and other resources that are expensive to compute.
  • Full-fledged source file.

You get to pick how accurate you want the generators to be. Maybe it writes 80% of the code for you and you do the rest by hand.

Active code generators

  • Active code generators are necessary if you want to adhere to the DRY principle.
    • This form is not considered duplication because it’s generated as needed by taking a single representation and converting it to all of the forms you need.
  • Great for system boundaries (think databases or web services).
  • Great for keeping things in sync.
  • Recommend creating your own parser.

Why generate when you can just … program?

  • Scaffolding, so it’s a starting off point that you edit.
  • Performance.
  • System boundaries.
  • Some uses work best when built into your build pipeline.
    • Think about automatically generating code to match your DB at compile time, like a T4 generator for Entity Framework.
  • It’s often easier to express the code to be generated in a language neutral representation so that it can be output in multiple languages.
    • Something like System.CodeDom comes to mind.
  • These generators don’t need to be complex.
  • And the output doesn’t always need to be code. It could be XML, JSON, etc.

Resources We Like

Tip of the Week

  • Within Visual Studio Code, after you use CTRL+F to find some text in your current document, you can take it a step further by pressing ALT+ENTER to enter into block selection/edit mode.
  • Turn learning Vi into a game with VIM Adventures. (vim-adventures.com)
  • Never confuse forward Slash with back Slash again.

Episode source