This session will be fun. That's the primary objective, have fun and code. That's it! In this session we will talk for a little bit about why XP practices are important and then we can write some code practicing two of the most fundamental ones.
The code will be super easy, this is not about writing complicated code, quite the opposite. We will get pairs of you to build really simple test driven code. It will make you rethink how you write code. For those of you after more of a challenge, we have included some optional Object Oriented (calisthenics) rules to apply.
Come in pairs or make new friends on the day. Please all bring your own laptop with your favourite IDE installed.
Two key things to learn / practice in this Kata:
- Test Driven Development
- Pair programming
-
Clone the repository with the following command.
git clone https://github.com/xp-dojo/tdd-bank-account-python
If you have problems with SSL, you can try the following.
git clone -c http.sslVerify=false https://github.com/xp-dojo/tdd-bank-account-python
If you have problems with a proxy, you can
unset http_proxy
andunset https_proxy
(or equivalent for your OS). -
Install python dependencies:
pip install -r requirements.txt
-
Open the project from your IDE, and you will find tdd_bank_dojo source folder with tests and account.py. Instructions for setting up VS Code can be found here.
-
Run your tests:
python -m pytest tdd_bank_dojo/tests
-
Implement the following user requirements in a TDD fashion. Work in pairs and read the guidelines and background information below before starting.
- I can Deposit money to accounts
- I can Withdraw money from accounts
- I can Transfer amounts between accounts (if I have the funds)
- I can print out an Account balance slip (date, time, balance)
- I can print a statement of account activity (statement)
- I can apply Statement filters (include just deposits, withdrawal, date)
Test driven development is based on the principles of test-first development (where you write the test first) but goes an extra step to actually driving the code using the IDE. The basic cycle follows the Red -> Green -> Refactor model:
- Red: write a failing test. Write a test that describes (think documentation) what the function you are writing actually does. Likely this will not even compile (this is fine, not compiling IS a failing test).
- Green: now write enough of an implementation to make the test pass. You should write the simplest code possible to make the code pass and resist the urge to write more than is actually needed. Consider the YAGNI principle.
- Refactor: now we re-read the code and make sure that this is good enough to push to the world at large. You should ask yourself at this point how the next person that reads this code will experience it.
As a walkthrough consider applying TDD as a two stage process, the first phase writes the API in the test as it should be (write the code you would like someone else to have written for you). In this case the compiler IS the failing test, you rewrite it until you are happy and then to make it go green you use the IDE to create the classes and methods as per the test (dont type them, let the IDE do the work). The next phase of the cycle implements the methods to get the unit tests passing, followed by the refactoring to complete the RGR cycle described above.
There are many different ways to do pair programming, the most common model is the Driver-Navigator model. For this kata, try and follow as below for simplicity. There are two roles in this model (you should switch often to keep it interesting):
- The Driver is the person wiring the code (test driven) and implementing. The Driver should be explaining what they are doing in a running monologue so the Navigator understands the direction taken and can assess it (also it keeps the Navigator engaged).
- The Navigator is the person observing and thinking about the big picture. The best Navigators are those that ask why? often to check that we build the right thing, and build the thing right. For this kata, the Navigator is checking that they are really test driving (using the IDE) and that the code does what it says and says what it does. When applying object calisthenics the Navigator should be checking that the Nine rules are not being broken. Correction and learning is the key here.
Object calisthenics are a set of rules or constraints designed to challenge and stretch yourself when applied to OO coding. For an extra challenge when implementing the user requirements, consider applying rules below.
- Don’t use the
else
keyword - Wrap all primitives and strings (in an object)
- No properties
- First class collections
- Use only one dot per line
- Don’t abbreviate
- Keep all entities small (50 lines)
- No classes with more than two instance variables
- Reduce boilerplate code in your tests by using fixtures
See the links below for more details.
- Project based on the original from Sandro Mancuso and the LSCC
- Original idea for the kata: How Object-Oriented Are You Feeling Today? - Krzysztof Jelski (Session on the Software Craftsmanship UK 2011 conference)
- Object Calisthenics pdf
- Object Calisthenics (full book), Jeff Bay in: The ThoughtWorks Anthology. Pragmatic Bookshelf 2008
The content of this project (educational material, slides and alike) are licensed under the Creative Commons Attribution Share Alike 4.0 International (CC-BY-SA) license, whilst the underlying source code used to support the educational material is licensed under the Apache 2.0 license.