Code

Human-centric code...

 

...uses human-centric vocabulary

...exists in human-sized units

...clearly expresses dependencies between elements

...is declarative

 

 

Human centric vocabulary

 

  • Business logic code should be readable by a business user.
  • Function names should communicate intent, not mechanics
  • Decisions are separate from effects

 

 

 

 

Human-sized units

 

  • Functions > 10 lines smell bad
  • Functions > 1 indent smell worse

Explicit dependencies

public void DoAThing() {

   var thingToDo = new Thing(otherGlobalThing);

   // You didn't tell me you needed a thing, and           // where did this global come from?

   Do(thingToDo);

}

public DoneThing DoAThing(Thing thingToDo) {

   return Do(thingToDo);

}

Declarative Code vs Imperative Code

Imperative recipe (How)

 

1) Place plate on counter top

2) Place slice of bread on plate

3) Spread peanut butter on bread

4) Spread jelly on bread

5) Place another bread slice on top

 

Declarative recipe (What)

 

A peanut butter and jelly sandwich IS a thin layer of peanut butter and a thin layer of jelly between two slices of bread

 

 

Programming errors are much more likely to arise from misunderstanding intent than from misunderstanding syntax

 

Code to communicate intent

Code

By Michael Atkins