Seven Pillars of Pretty Code Christopher Seiwald (Presented by - - PowerPoint PPT Presentation

seven pillars of pretty code christopher seiwald
SMART_READER_LITE
LIVE PREVIEW

Seven Pillars of Pretty Code Christopher Seiwald (Presented by - - PowerPoint PPT Presentation

Seven Pillars of Pretty Code Christopher Seiwald (Presented by Ulrik Schou) Overview/motivation Visual parsing Working code -> working readable code. 1,2,3,4,5: Formularic 6,7: requires intuition Pillar 1 : Blend in. -


slide-1
SLIDE 1

Seven Pillars of Pretty Code Christopher Seiwald (Presented by Ulrik Schou)

slide-2
SLIDE 2

Overview/motivation

  • Visual parsing
  • Working code -> working readable code.
  • 1,2,3,4,5: Formularic
  • 6,7: requires intuition
slide-3
SLIDE 3

Pillar 1 : Blend in.

  • Changes should be invisible.
  • scope: absolutely within functions, generally within a file, and if

you're lucky across the system.

  • if too ugly code : rewrite
slide-4
SLIDE 4

Pillar 2: Bookish

  • Keep columns narrow: like a book
  • Remedies : shorter names

lineup function arguments streamline logic

  • Rule of thumb: 80 cols.
slide-5
SLIDE 5

Pillar 3: Disentangle Code Blocks

Break code into blocks within functions. Each block should do a ”single kind of thing”. Approaches: minifunctions = blocks. When no minifunctions: organize activity(init vars, param check, computing results, returning results, printing debug). Apply approaches recursively.

slide-6
SLIDE 6

Pillar 4: Comments

Set off code blocks with whitespace and comments that describe each block. Comments should rephrase not be a translation. Big comments for subtle or problematic code. 15% blank and 25% comment lines.

slide-7
SLIDE 7

Pillar 5: Declutter

Reduce, reduce, reduce. Remove anything that will distract the reader. Tighter scope = shorter names. Avoid: unnessesary complicated statements needless casts heavy parenthesizing. Drop all dead code.

slide-8
SLIDE 8

Pillar 6: Make Alike Look Alike

Two or more pieces of code that do the same or similar thing should be made to look the same(patterns). Line up Look Alikes. Initialize variables together. Consistently use 'this' (or don't). Line up function arguments. Consistently use {} around if/else clauses: either all blocks have them, or none do. Put the { of a if/for/while on its line (because the closing } is). Break apart conditionals at the &&'s or ||'s and align them.

slide-9
SLIDE 9

Pillar 7: Overcome indentaion.

The left edge of the code defines its structure, while the right side holds the detail. -> Fight indentation. Approaches:

  • ne level of indentation for if/while/for/do/switch.

Use break/continue/return even goto Rearrange conditionals..

slide-10
SLIDE 10

Overview

  • Blend In
  • Bookish
  • Disentangle Code Blocks
  • Comments
  • Declutter
  • Make Alike Look Alike
  • Overcome Indentation
slide-11
SLIDE 11

The end

Any questions?