Skip to content

Latest commit

 

History

History
151 lines (121 loc) · 9.34 KB

clean-code.md

File metadata and controls

151 lines (121 loc) · 9.34 KB

Clean Code

martin fowler quote

Poorly factored code is a problem because it's hard to understand. Code that's hard to understand is hard to modify, whether to add new features or to debug - Martin Fowler

The idea of writing clean code is to ensure that the software is: readable, maintainable and extensible. Clean code enables businesses to iterate quickly and create value without tearing too much hair out.

Tips on Writing Clean Code

5S Principles (from Clean Code)

  • Seiri, or organization (think “sort” in English). Knowing where things are—using approaches such as suitable naming—is crucial. You think naming identifiers isn’t important?
  • Seiton, or tidiness (think “systematize” in English). There is an old American saying: A place for everything, and everything in its place. A piece of code should be where you expect to find it—and, if not, you should re-factor to get it there.
  • Seiso, or cleaning (think “shine” in English): Keep the workplace free of hanging wires, grease, scraps, and waste. What do the authors here say about littering your code with comments and commented-out code lines that capture history or wishes for the future? Get rid of them.
  • Seiketsu, or standardization: The group agrees about how to keep the workplace clean. Do you think this book says anything about having a consistent coding style and set of practices within the group? Where do those standards come from? Read on.
  • Shutsuke, or discipline (self-discipline). This means having the discipline to follow the practices and to frequently reflect on one’s work and be willing to change.

Tips on Doing Refactoring Safely

  • Make sure you have automated tests in place to cover the system you are going to change. If there is no test cases yet, you need to write Characterization Test or generate Golden Master Test first.
  • Don't change system behavior (e.g. fixing bugs, adding new features, etc) during refactoring
  • Make small changes, step by step. If you need to do a big refactor it is better to do multiple small changes
  • Commit frequently. You can make a commit after each small change, when all the tests are still passing.
  • Make good use of IDE support on refactoring. For VS Code, there are some good plugins like JavaScript Booster, glean and Surround which helps you to automate some of the code changes.
  • Track the issues in a todo list. If you notice some issues and you cannot work on it right away, you need to track it in the issue system.
  • Add unit tests along the way. If you only have Golden Master Test and no Unit Tests, you should start adding Unit Tests while you do refactoring.

Learning Resources

Labs

1. Refactoring Kata

Instructions:

  • Pair up and refactor each other's code
  • Jot down code smells (see list of code smells here) and apply the refactoring techniques that you've learnt to make it better
  • If you have any doubts, clarify it with your pair

Steps:

  • Add your pair to your repo
  • Clone your pair's repo
  • set up
    • Install dependencies

      npm install

    • check test coverage first and write tests first if necessary:npm test -- --coverage

  • Read each other's code and list down code smells
  • Start refactoring!
    • Pair up, and refactor one repo at a time
    • Keep tests (watch mode) in sight
    • Commit often
    • Communicate - ask questions when in doubt

2. Refactoring video store

Fork the Refactoring a JavaScript video store and refactor the codes by applying the principals learned in this session.

3. Refactoring the-ugly-trivia-game

Fork the Refactoring the ugly trivia game and refactoring the codes.

Note that in the repository there is a golden_master folder which contains Golden Master Test for the game.

Golden Master Test (a.k.a Characterization Test) is a technique used to record the existing behavior of the system under test and make sure your modifications do not cause behavior changes.

For more information on this technique, check out these two blog posts:

3. Code Retreat