This chapter will discuss coding kata, basic terminology, overcoming difficulty in TDD, and how to improve your TDD skills over time. We will look at the distinction between intent and implementation and its role in coming up with elegant solutions. We will briefly discuss the basics of Test Driven Development (TDD) and how problem-solving skills fit into TDD. We will also cover designing test cases, assertion, Canonical Test Structure, and how to avoid common mistakes.
Terminology
This section will introduce you to the basic terminology required to grasp the material in this book. We will look at basic terms such as kata , coding kata , domain , problem domain , and solution domain .
Kata
Kata is a Japanese word meaning form . In martial arts, it describes a choreographed pattern of movements used to train yourself to the level of muscle memory. The focus is on making small improvements during practice. Figure shows a choreographed pattern of movements in martial arts.
Figure 1-1.
A choreographed pattern of movements
Coding Kata
A coding kata is a short exercise that is thirty minutes to an hour long. It can be coded in many different ways. It is likely that coding katas have many solutions. The focus is on learning when you work through them. The goal is to practice in order to improve your skills, not to achieve perfection.
Domain
What comes to mind when you hear terms such as equity , debt , gross margin , and net income ? How about contour , contrast , opacity , and form ? Heres another example: parallel , ordinate , arc , and angle . A domain is defined as a specific sphere of activity or knowledge. The first example corresponds to finance. The second example corresponds to visual communication, and the third corresponds to geometry. Figure shows three different domainsfinance, visual communication, and mathconsisting of abstractions found in those domains.
Figure 1-2.
Examples of domains
Problem Domain
The problem domain refers to real-world things and concepts related to a problem.
Solution Domain
The solution domain refers to real-world things and concepts related to a solution.
Domain Expert
A domain expert is someone with special knowledge or skill in a particular domain. For instance, an accountant is an expert in the accounting domain. The development of accounting software requires knowledge in two different domains, namely accounting and software.
Domain Knowledge
Domain knowledge is expertise in a particular problem domain. It is a critical ingredient in coming up with abstractions that create elegant solutions.
Defining the Problem Domain and the Solution Domain
When you read a problem statement in a textual form, you will find concepts in the problem statement. You can list the concepts you find and group them to come up with the problem domain name. Figure illustrates the process of coming up with the problem domain name from a given problem statement.
Figure 1-3.
Process of finding the domain name
Lets now see an example of problem domain and solution domain. Lets say you have leaking sink problem in your kitchen. You search on the Internet for the term leaking sink . The phrase leaking sink is found in the problem domain. Once you read about the solution to this problem, you learn about things like: clevis screw, stopper rod, clevis, retaining nut and so on. These terms belong to the solution domain. You watch a video and find out that you need to buy a retaining nut to fix the leak. You now start using this term found in the solution domain, retaining nut , to find the nearest store carrying this item. So, the term: retaining nut belongs to the solution domain.
Learning TDD
Learning by Coding Kata
Why coding Kata? Test Driven Development (TDD) is a difficult but learnable skill. So, in order to answer this question, we need to look at why TDD is difficult.
TDD is not a testing technique. Its an analysis technique, a design technique, really a technique for all activities of development.
Kent Beck, Test Driven Development by Example
By using the small but precise nature of the coding kata to practice these skills separately, you can move past this difficulty and hone your TDD skills. So, coding kata is the best way to learn TDD. How do you practice a coding kata? You will work through a coding kata by following the five steps of TDD, which will be discussed in an upcoming section.
Learning Retrospective
After you complete a coding kata using TDD, reflect on the TDD practice session. Ask yourself the following questions:
What went well during the TDD session?
What went wrong during the TDD session?
What can I do differently next time to improve?
The answers to these questions will tell you where you need to focus your efforts in your next practice session so as to improve your skills. Its a good idea to keep a journal that records the coding kata name and the answers to these questions for each practice session.
Intent vs. Implementation
In this section, we will discuss the intent and implementation, or the specification and implementation. We will look at examples to illustrate the differences between intent and implementation, why we need to separate them, and how to separate them in the code.
Intent
The dictionary definition of intent is determined to do something . If you want to travel in your car, your intent is to drive your car. You dont reach into the transmission and pull levers to drive. You can drive without knowing the details of the car engine. You use the visible parts of the car, such as the steering wheel, gas pedal, brake, and gears, to drive. You can express your intent by using the public interface of the car. In programming terms, the intent-revealing drive() method is used. The public interface would consist of things you can do to a car, such as start, stop, drive, turn, and so on.
Implementation
The things under the hood of the car make up the implementation. Only your car mechanic knows about the details of the car engine. You may be aware of the 3.0 liter V-6 engine, but you have no idea of how it works. There could be methods that are internal to the class, such as burn_fuel() , that are not part of the public interface of the car. This means the user does not directly invoke any private methods of the car.
Intent and Implementation
We will now see three examples of intent and implementation, the difference between them, and why we need to separate them.