Test-Driven Teaching!


0. This overview article

1. Hello World

2. Initial Box Game

3. Object Oriented Programming – Refactoring!

4. User Interface – Swing!

5. More Game Features


The idea of test-driven development (TDD) is not a new one and also the idea of test-driven teaching is not new (see also at springer or here).

The main idea is to write working code examples based on TDD for beginners. The students should then be able to code (unit) tests and to read them from the very beginning. Most people learned TDD some years after learning ‘programming itself’, although I think TDD is already part of every coding. Most programmers ‘code’ + ‘test’ + ‘bug fix’ + ‘test’ + etc. But with TDD you have the benefit that your tests can be executed later automatically and very fast, which ensures that early requirements are valid

  • even if the same programmer forgot them or
  • if several programmers working on the same project at the same time or also
  • if a programmer must resume the work of another one.

I won’t give a lot details of TDD here, but the main advantages of TDD are:

  • If applied correctly (=early!) you can often code faster with TDD
  • Later refactorings are much more faster and less error prone (!!)
  • Testable code is often ‘better’ code; in terms of readability and re-useability
  • You indirectly document the functionality
  • The (unit) tests can be used as feature request or bug reports

The ‘real’ development cycle for TDD is outlined at wikipedia. My subjective suggestion to you my fellow reader is:

  1. think and initial code
  2. think …
  3. test a little (write test)
  4. code a little
  5. test passed?
    • yes => repeat with 2. (if all other tests passed too)
    • no  => find the mistake and repeat with 4. or 3.

Within all steps refactoring could be involved.

Unit Test ‘Add-on’ to Normal Teaching

Additionally to the explanation what different primitive types in Java are, there will unit tests like the following:

public void testLongAndInteger() {
 // an important difference of a long value to an integer value is the range:
 int  expectedInt  = 2147483647;
 // L indicates a long value
 long expectedLong = 9223372036854775807L;
 assertEquals( expectedInt, Integer.MAX_VALUE);
 assertEquals(expectedLong, Long.MAX_VALUE);

 // here is the problem what happens to an integer variable which
 // exceeds its domain: increasing the maximal value will cause the
 // variable to be negative!
 int intVal = Integer.MAX_VALUE + 1;
 assertTrue(intVal < 0);

This simple tutorial will not replace a good old Java textbook e.g. the JavaInsel (German) or other, but could be used in parallel. It is a pity that even those big and great books do not teach such important hand toolings (TDD).

Newbies will learn more …

Other important learning steps should be involved while teaching a programming language:

  • Version control (subversion, bazaar, mercury, git, …)
  • IDE handling (run, debug, test, refactor, format, … )
  • Using external libraries or link to resource lists (e.g. this one)

So, how should we structure such a beginner course where test-driven development is involved?

As a beginner I would prefer a ‘real life’ project. We should develop an idea and implement this while teaching (with refactoring!). It should be an idea which does not require a lot of set-up or external resources (such as an internet connection, web app etc). And it should be either useful (sth. like a xml conversion tool), a game or sth. that looks nice.

In this tutorial we will implement a simple game with Swing as the graphical user interface (GUI) to make it visually more appealing than an ordinary console output. The Swing coding chapters should and can be skipped at the first time of reading. Although user interface code can be tested (e.g. with FEST) they won’t, because of the lack of time of myself. The game itself should look like this but actually won’t (again lack of time).

BTW: I’m the author of GraphHopper, a fast open source route planner


7 thoughts on “Test-Driven Teaching!

  1. Pingback: Test-Driven Teaching – 1. Hello World « Find Time for the Karussell

  2. Pingback: Test-Driven Teaching – 2. Initial Box Game « Find Time for the Karussell

  3. Pingback: Test-Driven Teaching – 3. Object Oriented Programming « Find Time for the Karussell

  4. Pingback: Blog harvest, January 2010 « Schneide Blog

  5. Pingback: Test-Driven Teaching – 4. User Interface / 5. More Game Features « Find Time for the Karussell

  6. Finally someone gets it. Test driven teaching is the best way to learn a language. I hope this is applied to Python. Python is supposed to be an easy language to learn. Regarding most(not all) of Python the syntax and use are easy. What is difficult is the general expectation that learning coding is to be accomplished via the command line and /or using the horrible IDLE editor that onnly supports pasting a single line of code! How is a noob supposed to learn?
    I’ve learned C# and Python by stumbling around, trial and error. What a waste of time! When learning C#, non of the books nor websites including MSDN even mentioned source control. Teach how to use Visual Studio? Never. I lost allot of work as a noob because of this even though I did learn how to create an array.

    I could continue to ramble.
    Thank you very much, I fully support your efforts.

  7. Thanks a lot for your comment! It is great to here that not only me is thinking this way 🙂 Especially this phrase “Teach how to use Visual Studio? Never.” is really good. Tools in informatics come and go (even great projects for beginners like BlueJ).

    And only concepts will stay (or only evolve)… thanks again for mental support 😉

Comments are closed.