Do you code what you don’t know?
Do you code with MANY F****OR loop?
How to throwout the tag of a NEWBIE?
THESE CAN BE SOLVED！！
When I was a child of learning program, I didn’t know how to write BEAUTIFUL code.
We think the ONLY WONDERFUL THING is writing a program which can work!
I wrote lots of junk in program AND I never think that was junk.
HOW to become a junior Back-End? Refactoring is important skill for improvement.
All content are according to the book “Refactoring: Improving The Design of Existing Code”
What is Refactoring?
What is Refactoring?
According Wiki, “Refactoring” is make code more readable by clean the code. The input/output of program will not be affect.
We can say that refactoring like cleaning house, we don’t destroy the interior furnitures and items, but arranging all of the items.
You may feel happy because writing a CORRECT (can work) program. BUT if you don't want punch by your teammate. I suggest you learn "Refactoring" to make a beautiful world.
I SHOW the code I write before:
In that moment, I don't think it is stupid because it can work.
However, my teammate say that he almost faint because see the code.
What problem that you can’t work with code?
Unreadable code, hard to rewrite.
Duplicated logic, hard to rewrite.
Add new feature, but have to fix exist code, hard to rewrite.
Complex conditional logic in program, hard to rewrite.
Thus, we hope code can:
All logic is specified only in one location
New feature will not change exist code
Express conditional logic as simply as possible
Just like clean the house, you put same function together and separating the different. If there are bugs you can easy to find it.
Improve programming design.
Make program more Readable.
Help you find BUGS
Help you arising the speed of coding
Remember “The Rule Of Three”
Refactoring when Add New feature
Refactoring when Fix Bugs
Refactoring when you review the code
Bad Smell 程式碼的壞味道
What is Bad Smell?
Bad Smell is what kind of code should be refactor?
I will introduce three Bad Smell.
if you like content, you can see the book.
Duplicate Code (重複的程式碼)
If you see the duplicate code, it represent you can combine the code, make program more simple.
SEE the SQL:
The solution is I make fake data, using variable, format method to put params.
Long Method (過長函式)
Let’s see the code I write last week
It’s a transfer tool of Excel to Json.
you can see I put all feature into a function.
This will confuse people, hard to understand what code want to do.
This also hard to find bugs.
SO, I divide the feature to three parts:
Read > Change > Write
Three parts, three function.
Pull Up Method (函式上拉)
That see the code:
This is another example:
Class <strong>Felidae(object): def legs(): .... def hair(): .... def color(): ....</strong> Class <strong>Felidae(object): @abstractmethod def criteria(): pass @abstractmethod def rule(): pass @abstractmethod def must_have(): pass</strong> Class <strong>Cat(object): def criteria(): .... def rule(): .... def must_have(): ....</strong> Class <strong>Lion(object): def criteria(): .... def rule(): .... def must_have(): ....</strong>
After you practice with bad smell several times, next time, when you code, you will stop and think.
Refactoring + Unit Test + Design Pattern
Finally, I would like to say that if you learn Refactor, you also need to learn Unit Test.
Refactoring + Unit Test can find bugs more quickly.
You need to understand Design Pattern, this can help you more when you design code.