English (en) 中文 (正體) (zh-tw)
minayu416

3 minute read

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!

SO

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.


That’s beginning~

重構 Refactoring

All content are according to the book “Refactoring: Improving The Design of Existing Code”

螢幕快照 2018-12-19 上午10.27.12

  • What is Refactoring?

  • Why Refactoring?

  • When Refactoring?

  • Bad smell

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:

螢幕快照 2019-01-04 下午2.24.03

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?

  1. Unreadable code, hard to rewrite.

  2. Duplicated logic, hard to rewrite.

  3. Add new feature, but have to fix exist code, hard to rewrite. 

  4. Complex conditional logic in program, hard to rewrite. 

Thus, we hope code can:

  1. Readable

All logic is specified only in one location

  1. New feature will not change exist code

  2. Express conditional logic as simply as possible

Why Refactoring?

Just like clean the house, 
you put same function together and separating the different. 
If there are bugs you can easy to find it.
  1. Improve programming design.

  2. Make program more Readable.

  3. Help you find BUGS

  4. Help you arising the speed of coding


When Refactoring?

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:

螢幕快照 2018-12-29 下午12.51.49

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.

螢幕快照 2018-12-29 下午12.46.17

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.

螢幕快照 2018-12-29 下午1.15.05


Pull Up Method (函式上拉)

Class-diagram-for-exemplary-Pull-Up-Method-refactoring

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.


comments powered by Disqus