Skip to content

techtabor/Tennis-Refactoring-Kata

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

Tennis Refactoring Kata

kata: an individual training exercise in karate and other martial arts.

What is this about

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

What to do

The three most important files in this repository:

  • tennis-score/tennis-score.js: code for main program
  • tennis-score/tennis-score-test.js: code for testing main program
  • tennis-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).


Usage of TennisScore class to track a tennis game

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

How to add and use new functions

class TennisScore {
  nameOfYourNewMethod() {
    // ...
  }
  
  nameOfYourOtherNewMethod() {
    // call method in class
    this.nameOfYourNewMethod();
    // call function defined outside class
    standaloneFunctionName();
  }
} 

const standaloneFunctionName = function() {
  //
}

Refactoring

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.

Qualities of clean code

  • 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

Guidance for refactoring

  • 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

Summary of the rules of tennis (not necessary for refactoring)

  1. 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.
  2. 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.
  3. If at least 3 points have been scored by each player, and the scores are equal, the score is "Deuce".
  4. 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.

About

Starting code for a Refactoring Code Kata on the Tennis rules

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 92.4%
  • HTML 7.6%