My notes below on Bobs book.. also need next article on refactoring PrimeNumber code.
We need 'Clean Code' because otherwise it can bring a company to its knees
As messy code builds up, productivity decreases
What is clean code?
Elegant, Efficient, Does one thing well, Simple, crisp abstractions, Has Unit Tests, Meaningful Names, minimal dependencies, Clear, no duplication, minimal number of entities, What you expect, beautiful
Make code easy to read
Boy Scout Rule: Leave the campground cleaner than you found it... same for code.. leave it cleaner than you found it
Dave's Simplest Refactoring List
** change one variable name for the better
** break up a function thats a bit too large
** eliminate one small bit of duplication
** clean up a composite if statement
SRP - Single Responsibility Principle
OCP - Open Closed Principle
DIP - Dependency Inversion Principle
DRY - Don't repeat yourself
2. Meaningful Names
intention revealing names
// this is bad!
int d; // elapsed time in days
**// better....What is being measured, and the units
see c:\development\ch2 Intentional Names.
and ch2 Statistics Message
**Class names - nouns (things), or noun phrases eg PrimeGenerator, WikiPage, Customer, Account, AddressParser.... not a verb (doing) eg manager, data, info
**Method names - verb or very phrases eg GeneratePrimes, postPayment, deletePage
**First rule: they should be small
Second rule: they should be smaller than that!
Do one thing!
** shrink function down.. so one level of abstration per function
TO RenderPageWithSetupsAndTeardown, we check to see whether the page is a test page and if so, we include the setups and teardown. In either case we render the page in HTML.
One level of abstraction per function.
Comments are to compensate for our failure to express ourselves in code
// Check to see if the employee is eligible for full benefits
if ((employee.flag && HOURLY_FLAG) && (employee.age > 65))
Dont comment out code
ch4Primes - great to understand at a higher level of abstraction
6. Objects and Data Abstraction
A class does not push its variables out through getters and setters
Rather it exposes abstract interfaces that allow its users to manipulate the essence of the data,
without having to know its implementation.
** am here.. would be good to type in the code again looking at procedural stuff from page 96.
Objects expose behaviour and hide data.
Data Structures expose data and have no significant behaviour.
DTO's - public variables and no functions
Active Records are special forms of DTO's...with public variables... and naviagtaional methods like save and find.
7. Error Handling
9. Unit Tests
Very important to keep clean
Good prime generator example
Open Closed principle
Seperation of main
12. Emergent Design
14. Successive Refinement - Case Study - command line parser
15. JUnit internals - Case study (smaller)
16. Refactoring SerialDate
17. Smells and Heuristics