Skip to the content.

#Clean Code

-

Clean Code

Any fool can write code a system can understand. Good programmers write code that humans can understand.

-

Be predictable!

No Side Effects!

-

Meaningful Descriptive Names

-

Meaningful Descriptive Names

int d; // elapsed time in days

VS

int elapsedTimeInDays;

-

Meaningful Descriptive Names

What is this doing?

public List flags() {
   List list1 = new ArrayList();
   for (int[] x : this.theList){
    if (x[0] == 4) {
    list1.add(x);
    }
   }
   return list1;
}

-

Meaningful Descriptive Names


-
-

# Single Responsibility
- Do one thing
  - variable
  - method
  - class
  - test




-
# Small methods
- Do one thing (SRP)
  - change the state of an object
  - get information about that object
- Less than 20 lines
- Maximum of 3 params

-
-
# One word per concept
- Length / size
- fetch / retrieve / get

-
-

# Testable
- Write the test, then write to code to make the test pass
- Tests should be
    - Fast
    - Independent - no test should depend on another test
    - Repeatable - can run in any environment
    - Self-validating (assert pass or fail, no manual check)
    - Timely (don’t write your test too late)

-
-
# Don’t repeat yourself! (DRY)
Sharing is caring


-
-
# Comments
- Express yourself in code
- Use comments to explain why not
  ```java
    // This test is extremely slow, only run it when you have time

-

Formatting

-

Encapsulation

-

Conclusion

-

Follow the boyscout rule:

“Leave the code cleaner than you found it.”