這是一篇不完整的 Clean Code — Uncle Bob 速記,內文請與影片搭配使用。
Lesson 1
Function
- Function name as noun is terrible idea, should be verb, cause functions do things
- Every line of a function should be at the same level of abstraction and that level should be one below the name (一個函式內僅有一種等級的物件,函式第一行物件即是該等級)
- Smaller (smaller block, shorter name) is better
- headline(all high level terms)→ abstract(slightly more detailed) → and increasing details until we get the bottom
Allow the reader to escape early
How do people read news?
Do one thing
- how small it need to be? a function should do one thing. The one thing should be you can’t meaningfully extract another function from it.
- Sink in the sea of tiny little functions?No, you won’t. Name it properly.
- “Class” consists of extracted functions that manipulate a set of variables.
- The indent level of as function should not be greater than one or two(as the function would be large to have nests)
Arguments
- A function more than 3 args is too complicated!
- To much args → should be a object or contained
- Don’t pass boolean variable into functions!
- Don’t pass a “output args” for collecting outputs.
- Code make you “double take” is rude, make sure your code is not surprising.
Avoid switch statements
- Avoid switch statements, use Polymorphism.
- Open close principle: open for extensions, close for modifications.
- switch statement may result in a large amount of recompiling.
- 1:23:55 linking and loading
No side-effects
- Side-effects pairs things should be finished in one function.
- Convention:
Command(return void) must have a side effect
Query(return value) doesn’t
Exception, not returning error code
Don’t repeat your self
- For multiple loop, JAVA lambda is a solution.
Structured Programming
- Why did Dijkstra not want go-to
- We don’t prove software correct like math, but we test it not incorrect like science.
Lesson 2
Comments
- Comments are used to explain code if the code can’t explain itself.
- Comments are not “pure good”, a comment is a failure for explain code by itself
- Comments silently rot.
- Write Bad code → Don’t comment it → Clean it!
- If you can’t clean it, then comment it.
- Always use name to explain code first, not comments.
- Know Design Pattern!
- Comments for DP could be informative
- Comments for regex could be informative
- Javados(like pydoc) are good, but they can lie.
Bad comments
- Don’t mumbling to yourself with comments(what comments say should be very valid, local and simple.)
- Don’t comment if code is simpler than the comment.
- The place make the comments most readable is in the code, not HTML with Javadocs.
Format
- number of line in File, Avg. 50 lines, most of files are within 100 lines, probably be good.
- length of a line, 30–40 is probably good and shorter than 80.
Naming
- A name that requires a comment, does not reveal it’s intent.
- How long should a name be?
Variable: small scope, short name; large scope, long name.
Function, Class: large scope, short name; vice versa. - Naming need to be disambiguated
- Avoid situations where code will break if a spelling error is fixed
- data vs dataProduct vs pd vs aProduct vs theProduct vs productInfo
- getActiveAccount vs getActiveAccounts vs getActiveAccountInfo, which fn to call?
Lesson 3
I am your new CTO
Don’t ship shit.
- When you release code, you will know it works, don’t guess it.
We will always be ready
- Shorter sprint length might be better, cause we document well, test it, integrate it more frequently, and is ready to deploy it.
- shippable, deployable, deliverable
Stable productivity
- the longer the project goes, the slower you don’t get
- if you do, because something is a mess
Inexpensive Adaptability
- Be cheap and easy to make changes to the system. the cost of the change = k × the scope of the change
- soft ware = changable product, if not , it’s hardware
Continuous Improvement of the system
- code to get better with time.
Fearless Competence
- if code need to clean, clean it, don’t leave it.Test it.
- Get green little test buttom
QA will(should) find nothing.
- Let QA automated
We cover for each other
- how to make sure someone can cover for you? pairing, so code reviewing might consider to cancel
Honest Estimates
- define the shape of what we don’t know
- 3 number: best case, nominal case, worst case
Lesson 4
You will say “no”
Test-driven development
- Don’t write code until you write a test that fails, because the code doesn’t exist.
- Don’t write more of a test that is sufficient to fail and “not compiling” is the failure.
- Don’t write code that is sufficient to pass the failing test.
- Test but don’t debug
- Test code is the code example
- testable ←→ decoupled
- TDD is double entry bookkeeping, they both do it twice.
- Mutation test
- Production code → more general, Test code → more specific
- Don’t run into the gold when writing test code, as long as you can.
- Learn TDD externally at first.
Lesson 5
- Architecture does not emerge from nothing.
- Shape of Architectures might change, not fixed.
Goal
- Software Architect is to minimize the human resources required to build and maintain software systems.
- Measure of quality = k / Measure of efforts to meet requirement(inverse proportion)