Writing testable code - A functional programming point of view — talk by Alvaro Videla.asdasd

“The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. [...] unit testing by definition only tests the functionality of the units themselves. Therefore, it will not catch integration errors or broader system-level errors. “ — wikipedia

“I get paid for code that works, not for tests, so my philosophy is to test as little as possible to reach a given level of confidence” — Kent Beck on stackoverflow

Why is it so hard to write tests?

What if I - as a first timer in Budapest - design a tourist tour about Budapest. Would you trust my tour quality? - says Alvaro.
I wouldn’t, you have to familiarise yourself with the city, possibly by living in Budapest for a while to know the hidden places, the secret passages and the nice parks to hang out. Otherwise your tour would be bad or it will be just another one in the big pile of same tours. Same applies to testing. To write good tests we have to learn how to write good programs.

Advertisement

Alvaro then shows some tangled php code and shows us how to untangle it, how to turn bad code into good code. (Nice language choice to present bad code btw)

Some general tips

  • Separate pure code from impure or stateful.
  • First write pure code.
  • Then add impure code if needed.
  • Write composable code.
  • Proper use of Types.
  • Write functions with just one responsibility.

From these general tips you can see why the functional programming part is crossed over in the title. “Writing unit tests is reinventing functional programming in non-functional languages

Advertisement

Additional food for thought

Advertisement


Advertisement


Advertisement


Slides from the talk: