Code katas are small coding exercises to sharpen your programming skills. In this post, I’ll show you my favourite code kata. The core idea is to resolve a relatively simple programming problem, following an exaggerated version of coding best practices.
- All methods have a maximum of three lines of code.
- All classes have a maximum of two fields (excluding data classes and POJOs).
- All classes have a maximum of ten lines of code (excluding imports).
These rules have Java and Kotlin in mind; you may want to tweak them to suit your programming language.
Using your preferred programming language, design a loyalty program for a coffee shop. Customers collect one stamp per coffee; when they reach six stamps, they have one free coffee. The application exposes the following public operations:
- Create a customer.
- Create a stamp for a customer.
- List all stamps and free coffees for a customer.
- Redeem a free coffee.
If the customer reaches six stamps upon adding a new stamp, the application should create a free reward coffee.
Feel free to ask any clarification questions, give feedback or share solutions on Twitter @code_exercises. Have a look at my other code exercises.
- Make you think about your coding practices.
- Consider the trade-offs involved when performing everyday programming tasks like splitting a class or a method.
- The app may hold state (customers, stamps, free coffees) in memory.
- Make reasonable assumptions for things like error handling (trying to add stamps to non-exiting customers, for example), that part is not that important.
- To ease testing, you can pre-load your application with customers, stamps and free coffees.