Last Updated: February 25, 2016
·
1.046K
· flemon

Three rules when refactoring

1. A method only does one thing

scenario: you are trying to explain what a method does, you say "this method is doing this...," as soon as you put 'and that' to your explanation, you need to think about extracting 'that' into another method

2. A file only cover one subject

scenario: you are trying to explain what a file is, you say "this file covers this subject...," as soon as you introduce another subject into your explanation, you need to think about extracting another subject to another file

3. A block of code only get used once (not always)

scenario: you are trying to write a piece of code to do something, you write "..." as soon as you find yourself repeating(or copy pasting) what you had written already in another place of the same project, you need to think about rewriting this code to be reusable. (usually apply to a block of code)

P.S

rule 3 may in some sense feel like the same as rule 1, but not exactly the same...feel free to help me refine the wordings. arguments are more than welcome! thank you!