Monday, May 6, 2019

Notes about Refactoring, at very beginning

https://refactoring.guru/refactoring/what-is-refactoring

Why Refactoring ?
Fight technical debt, clean code

And because you are not Chuck Norris




What's clean code looks like ?


  • Good to read ─ For your psycho team whose work is to debug your code.
  • No duplicate ─ Workload for changes and bugs also duplicate.
  • Less code ─ KISS Keep It Simple and Stupid
  • Passes tests ─ all of them, 100% 

What's technical debt ?

Unclean code, of course, but take it more literally, it's something you left in your code, done or undone, in order to make your code work right away, that something won't hurt now, but you'll definitely pay for it someday.

There have been some real-life examples, including even one of the biggest social media website

What cause technical debt ?

  • Your boss, who wants to sell the product ASAP.
  • Your project manager, who sees technical debt as unicorns in fairy tales.
  • Not enough tests, bugs slip into production build.
  • Lack of documentation, you better pray for team newbies can read your code.
  • Work alone, no interactions with team. You'll be just like the arrogant guy in most horror movies, walk away into some dark room, become the first victim of some crazy chainsaw murderer.
  • Too many develop branches, worse, simultaneous, worst, no interactions ==> Team Fight !!
  • Too stylish developers. I know, programming is art, I know.
Technical Debt happens everyday~

https://refactoring.guru/refactoring/when

When to refactoring ?

When the third time


  1. First time, just get it done
  2. Second time, well ... I'll just copy here... paste here... no body hurt...
  3. Third time, MAN... I got to refactor this shit, has to be this way, shit...

When adding feature to some shit

Refactor the shit, otherwise you are just adding something into the shit.

When debug and the bugs fly

Yes they fly, when you want to catch them, they fly like a hell.
Try to refactor the code, contain bugs into smaller area like a single class.

When review code

IDK... this is more or less like a "Having to do" in a code review



https://refactoring.guru/refactoring/how-to

How to refactor ?

Check list, examine your refactoring result every once in a while


  • Is code become cleaner ?
  • No new functionality
  • Run test !!



https://refactoring.guru/refactoring/smells

Stinky, stinky code



What stinky about ?

  • Bloaters ─ Godzilla code, look at this class I made, it's so BIIIIIIIG~
  • Object-Orientation Abusers ─ Use OO just for convenience.
  • Change Preventers ─ Duplicate codes scattered around
  • Dispensables ─ ... are dispensable, just ditch them
  • Couplers ─  Code: you can't just replace me, you have to replace my whole family, and you'll definitely pay for it I'm warning you~




No comments:

Post a Comment