Better Code Workshop : Sean Parent¶

Introduction¶

Engineering is making informed trade-offs to find the best solution given a set of constraints.

My hope is that this workshop is generally applicable, but my experience colors the choices I've made.

Career Background¶

Demo¶

Course Outline¶

  • Introduction
  • Preface
  • Types - Goal: Write complete, expressive, and efficient types
  • Algorithms - Goal: No raw loops
  • Data Structures - Goal: No incidental data structures
  • Runtime Polymorphism - Goal: No raw pointers
  • Concurrency - Goal: No raw synchronization primitives
  • Relationships - Goal: No contradictions
  • Epilogue

Materials¶

  • Slides: https://sean-parent.stlab.cc/notebook
  • Exercises: https://github.com/sean-parent/better-code-class

Use of Jupyter w/Xeus-Cling¶

  • Currently limited to C++17
    • I will provide some C++20 examples
  • using std; is implied
  • Definitions wrapped in a namespace
  • Namespaces are often versions so I can refine implementations
namespace v0 {

int f() { return 42; }

} // namespace v0
  • Often code is wrapped in a scope so it doesn't interfere with other code
  • If the last line doesn't have a semi-colon it is displayed
    • calling display(value); has the same effect
v0::f()
{
    using namespace v0;

    display(f());
}
  • Operations can be timed with %%timeit
    • This is not a substitute for benchmarks but gives some information to compare
%%timeit
{
    int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    sort(begin(a), end(a));
}
  • I use an instrumented class which prints common operations
{
    instrumented a;
    instrumented b;
    a = b;
    a = move(b);
}