Hey Folks! It’s a new site. If you see bad links or odd behavior, please contact me right away. Thanks! – GeePaw
 Home  Home  hom  Meet with GeePaw  Meet with GeePaw  mee  Work with GeePaw  Work with GeePaw  wor  Who's GeePaw  Who's GeePaw  who  Blog  Blog  blo  Contact  Contact  con

Parsing Expressions, TDD, and the Big Why

 22. August 2018

so, for those following along, the other day i had to decide whether to roll my own parser for boolean expressions involving tags, like “(stinky & doofus) | (ugly)“. very standard stuff. in the end, i rolled my own, and i have to tell you, i just had a really wonderful time.

a parser for strings is an absolutely awesome place to do TDD. they’re just strings. no clever fakes, no extract & override, nothing. just strings. i hadn’t done a recursive descent parser in years, so first i ripped some internet code and mucked around in it until i grasped it all again. then i tossed that, and TDD’d based on what i’d learned. rdp’s are nice, cuz you basically start at the bottom and add wrapping function at each step, but the call you make to fire up the parsing is the same every time, the wrapping is all internal.

anyway, now i have a lovely parser. it’s smart about errors, and hooked into a live editing box, so the user can type the expression. as they type, the text is red if i can’t parse it, green if i can, and i update the applied filter every time it’s green.

there were plenty of screwups along the way, and the TDD was just sitting there doing it’s TDD magic perfectly. i swear to god if i’d had to find all the little regressions i created as i was adding new function, it would have taken at least an order of magnitude more time. i’ve found three bugs since it went live, and i true-blue TDD’d them: added a new red test before i fixed the problem, got it red, then got it green. it was really a very pure process.

one of the reasons i decided to take up the mantle of teaching TDD: situations as clean & pure & delicious as these are actually quite rare in the workaday world of geekery.

and it seemed to me a lot of folks out there were suggesting that TDD is red-green-refactor all day, an endless spiral of rainbow-farting unicorns and never ever ever a judgment call or a frustration or an impure thought or act.

a working geek using TDD knows that we don’t normally do greenfield work in toy codebases with only leaf dependencies that we wrote ourselves. hardly ever do we get the chance to just let the tests flow like wine. so then why do i persist in applying TDD in situations in which it’s not pure and easy? why do i teach TDD if it’s not normally universally straightforward and learnable in cute little soundbites?

well here’s the thing. when i do get those simple pure situations, i code like a god among geeks. i am faster, stronger, smarter, and what i ship stays shipped. it’s not just a little better, it’s fantastically better.

and somewhere in my second year of trying TDD everywhere, i began to see situations that were almost simple and pure. one tiny change i could make, and they would be pure TDD situations. and i kept going, and that trend kept going. ever larger impure situations i became able to see how to change into pure ones. and it was very often very worth the effort to make those changes.

two duffers are at the billiard parlor watching the local hero get his ass whipped by some out of town star. the one guy says, “man, that stranger is good.” the other one says, “oh, idunno. he never takes a hard shot.”

this is the heart of the steering premise. we don’t force TDD to take hard shots. in fact, that forcing is what makes TDD duffers decide they hate it, that we’re all mouth-foaming religious fanatics, and that TDD can never work in real life.

baby TDD is red green refactor. grownup TDD is taking the shots that look hard at first blush, then rearranging things until they’re not hard, then baby-TDDing them.

and i teach this because it has made me a vastly stronger geek. and i can easily imagine duffers looking on: “oh, idunno, he never takes a hard shot.”

the steering premise says “tests & testability are first-class participants in design”. it means, in the simplest telling, “don’t take hard shots. when you can’t test this design in an easy shot, change it until you can.”