582334 TDD-ohjelmointimenetelmä ja koodin suunnittelu (5 op), syksy 2009
582334 TDD programming technique and designing code (5 cr), autumn 2009

Kurssiesittely Luennot Harjoitukset

Exercise 1: Tetris #1 (warmup). Detecting duplication.

Introduction to the Tetris project work

Pair programming is a good way to learn from other programmers and experiences show that it works well for learning TDD also. For example a more experienced TDD practitioner writes one test case and his pair writes the code needed to pass the test case. The keyboard is moved between the two developers (like in ping pong) and the flow stays on. The more experienced one can instruct his pair to refactor and to write good tests. When both pairs are equally skilled, together they can support each other and write better code than alone.

Unfortunately this course's teachers do not have enough many personalities to be able to pair program with all of you. That's why we have prepared a Tetris-themed project work, where the test cases have been provided (but commented out). Your assignment is to write code to pass the provided test cases one at a time. When the first test passes, uncomment the next test case and write code to pass it (but no more than is required for the tests to pass - remember the three rules of TDD). Improve the code by refactoring always when the test pass, so that making the next changes would be easier.

The Tetris project work's test cases for this and the coming exercises can be downloaded here: tdd-tetris-tutorial.zip

Or if you use Git, you may clone it from GitHub: orfjackal/tdd-tetris-tutorial, "tutorial" branch.

The stages of the Tetris project work have been numbered "Tetris #1", "Tetris #2" etc. By this we mean that the project work has been implemented so far that the test cases in the following list pass until the particular number:

  1. FallingBlocksTest
  2. RotatingPiecesOfBlocksTest
  3. RotatingTetrominoesTest
  4. FallingPiecesTest
  5. MovingAFallingPieceTest
  6. RotatingAFallingPieceTest
  7. Still more functionality - will be specified in later exercises.

The code to pass the tests has been meant to be written in the above order, because the later tests are based on the behaviour defined by the preceding tests.


max 6 points

  1. Make sure that your development environment works in the faculty's computer class. Installation instructions have been provided here (in Finnish - use Google Translate). You must be able to open the above mentioned Tetris project work in your development environment and run its JUnit tests. Recommendable IDEs are NetBeans, Eclipse and IntelliJ IDEA (commercial). You must install Maven and make sure that you can run the Tetris project work's tests with the command mvn test and build it with the command mvn clean package. If some tests do not pass, you can anyways build it with the command mvn clean package -DskipTests=true. This will create in the target directory a ZIP file, which contains the source code of the project.

    1 point: You are able to run the Tetris project work's tests with Maven in your exercise group's computer class.

  2. Detect code duplication in this function and think about how to improve it. Print the function on paper and mark with a pen all code which you detect to repeat itself inside this function. Think about how you could reduce this duplication. If some duplication can be replaced with a new function call, what would be the name of this function? (In this assignment you do not need to modify the code.)

    P.S. This function comes from the Gun-Tactyx AI programming game and is one of the functions that the robots' AI scripts can call (the whole file is here).

    1 point: Found one example of duplicated code.
    2 points: Found two or more examples of duplicated code.

  3. Tetris #1: Implement code to pass FallingBlocksTest using TDD. The test cases have been provided, so you need to just remove their comments one at a time, as you manage to write production code to pass the tests.

    It's worth reading the following articles about TDD, so that you would know TDD's idea and were able to do this exercise. The same topics are covered during the first lecture, but if you want to start doing the exercises in good time or are not able to make it to the lecture, with these articles you can get started. Otherwise you can review the lecture's topics from these.

    http://www.butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata (a link to the PPT presentation is at the beginning of the page)
    http://www.agiledata.org/essays/tdd.html (you may skip chapters 4 and 5)

    1 point: Out of FallingBlocksTest's 10 tests 3 pass.
    2 points: Out of FallingBlocksTest's 10 tests 6 pass.
    3 points: Out of FallingBlocksTest's 10 tests 10 pass.

In the exercise group we will make sure that everybody's development environment works, discuss about code duplication and pair program starting from Tetris #2.

Viimeksi muokattu 01.12.2010 21:04 Copyright © 2009 Esko Luontola