APPENDIX 4

image

Coding Katas

Coding katas have become increasingly popular since their invention in 1999. If you want to improve your programming chops, coding katas are like a sharpening block you can use to hone your skills. This appendix will explain what coding katas are and how to get the most out of them. There are some links to coding kata collections that you can use to practice your TypeScript programming.

A coding kata is method of practicing programming techniques and many katas are designed to exercise your design skills and give you mastery of your tools and workflow. The instructions for a kata describe a problem, gradually increasing the complexity throughout the kata. It is possible to repeat a kata by adding constraints, for example, limiting the language features that can be used.

Because a coding kata is just practice, you can try out ideas safely because you can always delete the code and start from scratch. Something you may opt to do several times even if your first attempt goes well. You can even use a kata to try out a new programming language or a new integrated development environment.

You can perform a coding kata individually, but there is even more value in pairing up with another programmer to work on a kata. Not only will you be working on your programming skills, you’ll be sharing and learning with your partner. Group kata workshops supply opportunities to pair with plenty of other programmers. The more diverse you make your practice, the more experience you bring to your real work. Some of the benefits of coding katas are

  • Learning features of your development environment, such as keyboard shortcuts.
  • Practicing test-first development.
  • Designing object-oriented solutions to the kata problem.
  • Refactoring and design pattern practice.

You can make katas even more interesting by changing the way you approach them. One valuable technique you can employ when undertaking a coding kata with a partner is to play ping-pong. You write a failing test to represent a single behavior and your partner must write the simplest code possible to make the test pass. Then your partner writes a test and you in turn must make it pass in the simplest way. You pass the problem back and forth, alternately writing a test or making it pass. You may even find that this practice makes its way into your daily working methods.

Another challenge you can set yourself during a coding kata is to use only the keyboard to complete the task. By discarding all other input devices, you will find interesting and helpful keyboard shortcuts to perform the operations you normally undertake using point, click, and touch.

Image Note  The term coding kata was coined by “Pragmatic” Dave Thomas. He also maintains a library of kata ideas at http://codekata.com/.

Performing a Kata

All you need to perform a coding kata is the following:

  • A development environment
  • A unit testing framework or library (see Chapter 9)
  • The kata instructions

A coding kata often relies on creating an increasingly complex problem, which is designed to cause your first design to break. You are forced to rework your code just as you must when writing a real program. Because of this, you’ll get more from a kata if you resist the temptation to read ahead. (Likewise, if you run a coding kata workshop, you should reveal the problem gradually to make the practice more effective.)

The rules of the kata should reflect the aspirations of the team; this usually means following practices such as test-first programming, pairing, and refactoring. With TypeScript in particular, you should aim to exercise the object-oriented programming practices described in Chapter 3.

The Fizz Buzz Kata

This coding kata originated as a job interview test created by Imran Ghory (http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/, Using FizzBuzz to Find Developers who Grok Coding, 2007) based on the children’s game Fizz Buzz. The original game can be summed up in just a few simple rules, but as the program increases in size to satisfy the rules it should be just complex enough to give rise to one of many possible design solutions.

Remember, try not to read ahead, implement just one requirement in its entirety before reading the next one. This makes the practice as life like as possible.

Requirement 1

Write a program that outputs numbers from 1 to 100.

Requirement 2

For any number that is divisible by three, replace the number in the output with the word Fizz.

Requirement 3

For any number that is divisible by five, replace the number in the output with the word Buzz.

Requirement 4

For any number that is divisible by both three and five, replace the number in the output with the words Fizz Buzz.

Requirement 5

For any number that is divisible by seven, replace the number in the output with the word Bazz. If the number is divisible by three and seven, the output should be Fizz Bazz. If the number is divisible by five and seven, the output should be Buzz Bazz. If the number is divisible by three, five, and seven, the output should be Fizz Buzz Bazz.

Fizz Buzz Summary

These requirements are typically enough to stretch the first design for the program. Requirement 5 is actually based on a variation of the original Fizz Buzz game—there is nothing wrong with adapting this kata based on some of the variations of the game or by adding a new rule of your own invention. Known alternative versions of Fizz Buzz are based on whether the number contains a digit, rather than being divisible by a number.

Summary

Once you have practiced the Fizz Buzz kata, there are many more available online (for example, http://codekata.com). Don’t be limited by existing lists of coding katas though; invent your own based on games or problems that you have encountered. The original coding kata was actually an experiment with different solutions to a real problem in a production system; so if you are about to add a feature to your program, run a kata to generate and evaluate potential solutions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161