Clean Code

Topics

  • Why Clean Code
  • What is Clean Code
  • Identity Good Code And Bad Code
  • How to Write Good Code
  • Transform Bad Code Into Good Code

Technology Agnostic

Why Clean Code

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
- John Woods

Why is it hard to write clean code

Who is responsible?

What is Good Code?

5S Principles

https://en.wikipedia.org/wiki/5S_%28methodology%29

Seiri - Sort

Seiri is sorting through all items in a location and removing all unnecessary items from the location.

Seiton - Set in Order/Straighten

Seiton is putting all necessary items in the optimal place for fulfilling their function in the workplace.

Seiso - Shine

Seiso is sweeping or cleaning and inspecting the workplace, tools and machinery on a regular basis.

Seiketsu - Standardize

Seiketsu is to standardize the processes used to sort, order and clean the workplace.

Shitsuke - Sustain

Shitsuke or sustain is the developed processes by self-discipline of the workers. Also translates as "do without being told".

Meaningful Names

Use Intention Revealing Names

              
                function getResults() {
                  // TODO
                }
              
            

Avoid Disinformation

  • Abbreviations
  • Inconsistent Spelling
  • Variable Names Like l and o
  • Misleading Terms
            
              function getResults2() {
                // TODO
              }
            
          

Make Meaningful Distinctions

  • MyProfile
  • MyAccount
  • EditProfile

Use Searchable Names

              
                function getEsResults() {
                  const ES_URL = process.env.ES_URL;
                  //
                }
              
            

Avoid Encodings

  • Hungarian Notation
  • Member Prefixes

Avoid Mental Mapping

Class/Component Names

Noun or Noun Phrase

Should not be a verb

Method / Function Names

  • Verb or Verb Phrase
  • Should not be a noun

Functions

Small!

Do One Thing!

Use Descriptive Names

              
                function esFunction() {
                  const ES_URL = process.env.ES_URL;
                  //
                }
              
            

Function Arguments

  • Number of Arguments
  • Flag Arguments
  • Argument Objects
  • Output Arguments
  • No Side Effects

Command Query Separation

Prefer Exceptions Over Error Codes / Booleans

DRY - Do Not Repeat Yourself

Comments

Comments Do Not Make Up For Bad Code

Explain Yourself In Code

Good Comments

  • Legal Comments
  • Explanation of Intent
  • Clarification
  • Warning of Consequences
  • TODO Comments
  • Amplification
  • Documentation Comments

Bad Comments

  • Mumbling
  • Reduntant Comments
  • Misleading Comments
  • Noisy Comments
  • Comments instead of not naming functions properly
  • Position Markers
  • Closing Brace Comments
  • Commented Out Code
  • Too Much Comments
  • Documentation comments in non-public code

Formatting

  • Vertical Formatting
  • Dependent Functions
  • Vertical Ordering
  • Horizontal Formatting
  • Indentation - Keep It Shorter

Exception Handling

  • Use Exception Rather Than Return Codes / Booleans
  • Don't Return Null

Unit Testing

  • TDD
  • Clean Tests

Classes

  • Small
  • Single Responsibility
  • Cohesion
  • Open-Close Principle

References & Credits

Thank You!