CppCon 2019: Better code: relationships

Sean Parent

  • Let’s rotate (cppcon six years ago); meme?
  • – Goal: No contradictions
  • A novice sees only the chessman; an amateur sees the board; a master sees the game

Entity notation

  • Witnessed Relationships: a relationship represented by an object
    • copyable and equality comparable
  • When an object is copied or moved any witnessed relationship is either maintained, severed, or invalidated
  • When an object is destructed any witnessed relationship is either severed, or invalidated

Safety

  • std::move()

    is an unsafe operation

C++20

  • One new feature: concepts (joke about contracts)
  • Hoare Logic (paper from 1969, C.A.R Hoare)

Equality

Applying “Design by Contract”

Contracts

  • Preconditions (require)
  • Postconditions
  • Invariants

Concepts

A compile time constraint to select an appropriate operation

Contracts assert at runtime if an an operations

A runtime constraint to select an appropriate operation is know a pattern matching

No Raw Loops

The Game

  • Architecture is the art and practice of designing and constructing structures

Timer example

Registry

Container support these:

  • Add an object and obtain receipt
  • Use receipt to retrieve the object
  • iterate

Russian Coat Check Algorithm

  • Receipts are ordered
    • Coats always appended with stub
    • Binary search to retrieve coat
      • when more than half the slot are empty; compact the coats
  • Coats are always ordered receipt stubs
  • Coats ordered by insertion

Double entry bookkeeping

  • Every transaction twice
  • 5 standard accounts: Asses, Capital, Liabilities, Revenues, and Expenses
  • This ensure the mechanical process of entering a transaction is done in two ways

Contradictions

  • unit tests expose contradictions

No Raw Synchronization Primitives

Use strong preconditions to move the issue to the caller

Setting a property

  • Code is redundant
  • – Different aspects of the same code
    • value is a*b when a changes
    • other_value is a*b when b changes
    • asking for troubles
  • Different, mutually exclusive relationships with non-local control
  • Implied last in wins relationship
  • An incidental algorithm; property convert to the correct value
  • property is not a simple property but a stream, trigger, or latch
  • or it’s just wrong

No Raw Pointers

https://sean-parent.stlab.cc for talks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.