kata: an individual training exercise in karate and other martial arts.
You find a correctly working code in tennis-score/tennis-score.js
. For a tennis game, the TennisScore
class can track the game progress and has a method (scoreToDisplay
) which returns the current status of a tennis game according to its rules.
The goal is to make the code cleaner (more readable, more elegant) while keeping the same functionality. You can check this by passing all existing tests. This process is called refactoring. For more information, see below
The three most important files in this repository:
tennis-score/tennis-score.js
: code for main programtennis-score/tennis-score-test.js
: code for testing main programtennis-score/tennis-score-test.html
: visualizing test runs for main program
First step: Download repository as ZIP and extract, or if you have git installed,
git clone https://github.com/techtabor/Tennis-Refactoring-Kata.git
Then:
- open
tennis-score/tennis-score-test.html
in your browser - modify
tennis-score/tennis-score.js
- refresh your browser after every modification (
Ctrl + r
in Chrome on Windows) to check that the code is still working as intended
Mainly you should modify the internals of the scoreToDisplay
function. You can also add new functions or modify other methods in the TennisScore
class, but do NOT modify the tests and keep the names and arguments for the already existing functions.
Work in pairs using one laptop (or work alone if you really prefer that).
const game = new TennisScore('Timea Babos', 'Serena Williams');
game.increasePointsOfPlayer1();
console.log(game.scoreToDisplay());
game.increasePointsOfPlayer1();
console.log(game.scoreToDisplay());
game.increasePointsOfPlayer2();
console.log(game.scoreToDisplay());
// etc
class TennisScore {
nameOfYourNewMethod() {
// ...
}
nameOfYourOtherNewMethod() {
// call method in class
this.nameOfYourNewMethod();
// call function defined outside class
standaloneFunctionName();
}
}
const standaloneFunctionName = function() {
//
}
Programmers spend much more time reading code than writing code, so it is really important that the code is understandable by reading it. For example, you read a lot of code during debugging, taking over a project, learning a new technology, building on top of someone else's solution, revisiting your own code from two months ago, etc.
- easy to understand: (even without comments)
- easy to modify (it is natural that needs, requirements are changing)
- easy to extend (code will usually evolve, support more requirements)
- difficult to accidentally mess up
- name variables and functions with names close to how you would speak about it in natural language
- each piece of business logic should be represented by exactly one piece of code
- each function should do one thing (easier to name if this is true!)
- simplest working and understandable solution
- clear entry point, gradually more details inside functions
- shortest is not always the simplest or most understandable
- you do not have to understand every detail before starting to refactor
- refactor in small steps, never rewrite from scratch
- A game is won by the first player to have won at least 4 points in total and at least 2 points more than the opponent.
- The running score of each game is described in a manner peculiar to tennis: scores from 0 to 3 points are described as "Love", "Fifteen", "Thirty", and "Forty" respectively.
- If at least 3 points have been scored by each player, and the scores are equal, the score is "Deuce".
- If at least 3 points have been scored by each side and a player has one more point than his opponent, the score of the game is "Advantage" for the player in the lead.
Forked from emilybache/Tennis-Refactoring-Kata
.