Structured Programming Done Right

Bart Massey 2013-11-05

Overview

  • Misc notes
  • Principles of Structured Programming
  • Review of textbook chapters

Misc Notes

Principles of Structured Programming

  • Goal: Program is as readable, understandable and maintainable as possible.

    • Non-Goal: Program is as efficient as possible. Microefficiency doesn't matter.
  • Basic Ideas:

    • Simple, clear view of relevant state at any given point in the program

    • Simple, clear, left-to-right top-to-bottom control flow

Rules of Programming Discipline

  • In order of priority

    • Chameleon Rule: When working in someone else's code, make your code look like theirs

    • Standards Rule: When coding standards are in place, follow them

    • Consensus Rule: Follow the community consensus

    • Simplicity Rule: Do the simplest, clearest thing that can work

Non-rules of Programming Discipline

  • Avoid these:

    • Shortest code wins

    • Most efficient code wins

    • First thing you can think of wins

Control Flow

  • Left-to-right:

    • Except assignments and function calls, darnit

    • Short-circuit boolean expressions

    • Avoid pre- and post- operators

  • Top-to-bottom:

    • Except loops (with structured exits)

    • May need to skip statements

Code Smells

  • High nesting depth

  • Complex expressions

  • Tangled control flow (c.f. McCabe complexity)

  • Violations of rules of programming discipline

Code Review

  • Desk check your code. Think about how it can be cleaned up as you write it, and immediately after

  • Get someone else to read your code. Listen carefully to their suggestions for improvement

  • Ask an expert how they'd do things. Learn from their answer

Let's Talk About The Textbook

  • Review of Chapters 14-19
Last modified: Tuesday, 5 November 2013, 11:18 AM