Subscribe to this blog!

Craftsman bio generator. A TDD practice.

Last week a friend of mine (one of the developers of Quares) told me an idea about an iPhone game. It was a rap battle game and he wanted my advise, due to my expertise in emceeing 😉 His first approach was to have a kazillion pre-recorded raps and then assign them weights, points, whatever… Soon we saw that it’d be better to generate rhymes but it seemed a tough job.

Challenge accepted!!

Choosing the TDD way

I knew how to develop this in a traditional way. A phrase generator can be build using context-free grammar but as I want to learn how to apply TDD I chose to try that way. I also want to master javascript as a new language this year and TDD practice is one of the best methods I know for that purpose.

What I found more difficult was to ask the right questions. It’s kind of funny to ask something when you know the answer but the objective was to build the data structure test by test. I based them on the existence of terminals (final text) and non-terminals (tokens that can be substituted for both terminals and non-terminals). From that point, it was easier to start.

You can find the project in GitHub. It’s written in Javascript and it uses Jasmine as a test framework.

My self exam

  •  As I said before, the key point is to ask good questions. There are many ways to skin a cat and bad tests can lead you in a wrong direction. I need to study other’s people katas to improve this skill.
  • Not enough refactoring. I think that I need to be more aggresive in refactoring. Sometimes I don’t feel like a refactor is needed right now, leaving a likely broken window. The same thought about katas applies here.
  • Not sure when it’s better to inject dependencies / use composition. In the end it works as this is a simple example but I have to keep an eye on this.
  • It’s difficult to create a good grammar. This is not a TDD or development issue but I assure you it’s quite hard.

Craftsman bio generator

After having all my test in a healthy green color and with the ability to substitute non-terminals I decided to have more fun. I’ve created a craftsman bio generator, in case you don’t know what to write in your twitter account.

Here are some examples:

  • Co-author of ‘The best practices for Haskell’ and ‘Clean Clojure’ (contributor). Scrum enthusiast, Ruby on Rails expert, father.
  • Working at NextLearning, Haskell expert, husband. Likes reading. Writing Ruby on Rails webs for CompuGlobalHyperMegaNet also.
  • I write code for NextCoding.com. Prize-Winning author of ‘Developing XP Haskell software’ and ‘Clean RoR’.

Craftsman bio generator

I’ve uploaded the generator to my GitHub. All the magic is in the grammar file (bioGrammar.js). You can study how the generation it’s done and if you use it for building your own generation let me know!

Improvements

  • More test cases. I guess there aren’t enough tests. You need one that traverses all the grammar to find errors in that structure prior to begin execution. I did tests only for the code but finding issues in the data is a plus.
  • Number / gender concordance. This is planned for the next iteration. Remember that the aim is to have a rhyme generator, but before, if I want to build sentences that make sense I have to add more complexity to the substitution process, taking care of the number and gender of the subjects.

Have fun!

 

Featured image Argyle Street Joinery (modified)
  • http://twitter.com/carlosble Carlos Ble

    Gran idea! Aun no he visto el codigo pero solo el hecho de que te hayas puesto a practicar y lo publiques me parece cojonudo 😀 enhorabuena tio

    • Anonymous

      Gracias Carlos.

      Tampoco le eches mucho vistazo al código a ver si me vas a sacar muchas faltas 😉

      • http://twitter.com/carlosble Carlos Ble

        Pues lo acabo de mirar 🙂 no veo los tests en github. Los metodos son muy largos. Que tal si intentas que no pasen de 5 lineas? Por practicar 🙂