sexta-feira, 22 de julho de 2011

TDD Language Proposal

Introduction

Yesterday  I saw the inspiring The Last Programming Language lecture of Uncle Bob and extracted the following concepts:

  • Languages are always restricting our way to write code.
  • The last big thing in programming was TDD.
Proposal

joining the two concepts, I wondered if we could have a programming language in which we restrict the programmer to do TDD. But how could this language be structured? how could we inject TDD on the language desing?

Doing TDD is doing tests firsts and write only the code necessary for it to pass. What if we could, on the same file, write a unit test and the line that makes it pass? Obviously we would gain a great amount of time from shifting from test file to production file as we do today, but what else?

The main concept of this proposal is to value the test and its connection to the production line of code, in such a way that we would loose the direct control of the final code, the language will compile it for us.

Ok, a bit crazy I know, but it is possible to have such a language? lets wander:

[METHOD] AddTest [TEST_STATEMENT], [PRODUCTION_LINE]

Example:

Buy AddTest "throws if cart is empty", 
             throw if cart.size == 0

ok, simple enough, but where does cart come from? well, it would be inferred by compiler and possibly added  as a parameter of the method Buy.You might ask what about a more complex example, in which different tests are testing the same line? or if we are testing things inside a loop?

This is where Homoiconicity comes along and instead of repeating lines you contextualize the line of code in order to know where it will be inserted on the final code, example:

Buy AddTest "throws if item is not in stock", 
            [Buy.Iteration.cart] throw if item.NotInStock

Writing this line makes the compiler add an "for" on the Buy method that iterates the cart parameter and add the line that checks if its in stock or not.

Conclusion

If we preach "we should only write production code if its to pass an unit test", why not make it a language restriction and say "The only way to write production code is writing its test", and then we would take TDD really seriously and forget frameworks, IDEs tools and metrics that we have build to accommodate a language dysfunction, its the same thing as implementing polymorphism on C.

2 comentários:

  1. TDD stands for Test Driven Development - http://pt.wikipedia.org/wiki/Test_Driven_Development is that your question?

    ResponderExcluir