Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static modeling (& lifetime) Using types to test, document and reduce system complexity.

Similar presentations

Presentation on theme: "Static modeling (& lifetime) Using types to test, document and reduce system complexity."— Presentation transcript:

1 Static modeling (& lifetime) Using types to test, document and reduce system complexity

2 Welcome

3 This talk/debate Static modeling Lifetime given time Interrupt at any time!

4 Safety nets Design/code reviews, pair programming Compiler Static modeling Unit tests/module tests Pipeline tests Developer testing Continuos integration Static analysis tools (PCLint etc) Dynamic analysis tools (Boundschecker, valgrind, etc) QA testing Design by contract Assertions Bold = expressed in code

5 Fail fast Minimize turn around time – minimize waste Bug found => ”Possible to catch earlier?” Review => ”Possible to catch earlier?”

6 Failing fast in C++ Build time Code generation Macro expansion Template instantiation Translation unit compilation Linking Installation/configuration Run time Load time Static initialization Bootstrapping/Initialization code Level load... Specific/rare situations

7 Static modeling Model invariants and tests with (static) types Either compile time or instantiation time int x=-10; bounded i(x); quantity v = 1*meter/second; quantity t = 2.0*minute; quantity l = v/t;

8 Value of static modeling 1.Type up ASAP - fail fast 2.Documentation 3.Reduces complexity / state space

9 Code based tests Static modeling, assertions, design by contract, automated test hooks, unit tests etc. - Add ”weight” when prototyping + ”Easy” to add to existing/legacy code

10 How to reduce the state space Init/set/modify -> constructor/factory Split objects into smaller pieces Lock down dynamics ASAP

11 Demarshalling Type up immediately

12 Indices Indices only needed when de-/serializing Look up immediately, pass around instance

13 Use typed ids struct foo_id { int id; }; bool operator==(...);

14 Distuinguish invalid ids by type enum error {err1, err2}; class result { bool has_id(); const /// @pre valid() foo_id get_id() const; /// @pre !valid() error get_error() const; private: int data; }; result get_id(...); // Using boost boost::variant get_id();

15 Optional optional find_closest_x(); void do_stuff(const X& x); Isolate inited/not inited state Clear semantics, all-or-nothing bool find_closest_x(X& x);

16 Right/left hand system Either compile time, runtime or combined Better granularity than math asserts on/off

17 RAII new/delete aquire/unaquire connect/disconnect lock/unlock subscribe/unsubscribe register/unregister open/close Need to match call pairs AND know if an object is initialized or not

18 RAII example void foo(int x, int y); signal sig; signal_connection con(sig, foo);

19 Side note: RAII pillars Exceptions Deterministic destruction

20 Static modeling and lifetime More work in constructor/destructor Object lifetime becomes very important

21 Lifetime primitives scoped_ptr (not copyable) shared_ptr weak_ptr move_ptr (not copyable) reference (not assignable)

22 Interfaceless objects /// @param foo is guaranteed to outlive /// return value move_ptr create_stuff(const function & onBar, shared_ptr gui, const SomeFacade& foo, move_ptr x);

23 Summary Static modeling pros: Fails fast, documentation, minimizes total logic Static modeling increasingly important in longer and larger projects Static modeling increases need of clear lifetime semantics

24 Static modeling vs. DbC Catch errors that must be checked at runtime by typing up or DbC/assertions? Is the invariant local? Duplicate preconditions => time to type up! S.M. only applicable to single variables

25 Opposite of static modeling class message_channel { void send(const string& key, const string& value); void listen(const string& key, listener& l) const; }; Dynamic modeling good for prototyping/iterating Optimal: Lock down invariants in code by typing up incrementally. No major language supports this well. 

26 Don’t get lured by ”power” Dynamic components are very powerful but can also be abused Specialized, less powerful = easy to understand and test, smaller state space Think reflection, introspection, tuple spaces

Download ppt "Static modeling (& lifetime) Using types to test, document and reduce system complexity."

Similar presentations

Ads by Google