I was thinking about how people are taught to program. It seems to me that most of the time, people are taught by quickly creating something. Its a "Hello World" app, or a quick Web page to display something. The idea is that you get to create something quick and easy and experience the thrill that comes along with that. From there you start digging into more complex concepts and with experience you learn how to do everything.
What if we changed that to writing a test first? What if we used the TDD process of red -> green -> refactor to teach? The code required to write a test would be very simple. A single line for setup, the assembly step of a test. A single line for the system to do work, the act step of the test. Then a single line to prove the work did what was expected, the assert step.
So the person learning would need to write 3 lines of code to create the test, and it would teach them a ton about how pieces of code interact with each other. They wouldn't be exposed to complex concepts yet, just a simple linear set of commands. They would then run the test, which would of course fail because the Target doesn't exist yet.
Next is where the coding "hook" happens. You help them write the system under test, at this stage it should be a simple 1 line of execution and spit back out the input. They run the test again and it passes. For me at least, this would provide the same sort of thrill as seeing the "Hello World" example because I just completed my first task. This would introduce them into "game" of TDD where you start with a quest, a failing test, and complete that quest by making the test pass.
From here you can introduce more complex logic, you continue the process and the student learns how to write code using TDD first.
What if we changed that to writing a test first? What if we used the TDD process of red -> green -> refactor to teach? The code required to write a test would be very simple. A single line for setup, the assembly step of a test. A single line for the system to do work, the act step of the test. Then a single line to prove the work did what was expected, the assert step.
[Test] function GivenInputOfHelloWorld_Always_ReturnOutputOfHelloWorld(){ var input = "Hello World"; var result = Target.Process(input); Assert.AreEqual("Hello World", result); }
So the person learning would need to write 3 lines of code to create the test, and it would teach them a ton about how pieces of code interact with each other. They wouldn't be exposed to complex concepts yet, just a simple linear set of commands. They would then run the test, which would of course fail because the Target doesn't exist yet.
Next is where the coding "hook" happens. You help them write the system under test, at this stage it should be a simple 1 line of execution and spit back out the input. They run the test again and it passes. For me at least, this would provide the same sort of thrill as seeing the "Hello World" example because I just completed my first task. This would introduce them into "game" of TDD where you start with a quest, a failing test, and complete that quest by making the test pass.
From here you can introduce more complex logic, you continue the process and the student learns how to write code using TDD first.
Comments
Post a Comment