WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ......
Transcript of WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ......
![Page 1: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/1.jpg)
ANTI-PATTERNS IN THE WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON
![Page 2: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/2.jpg)
OVERVIEWCommon anti-patterns● Why do they happen?● What to do about them● Safe hunting
2
![Page 3: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/3.jpg)
A LITTLE BACKGROUNDWHY THINGS ARE WHAT THEY ARE
![Page 4: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/4.jpg)
MOTOROLA PRODUCTS
DIMETRA™TETRA
ASTRO® 25P25
WAVE™LTE
4Motorola Solutions Internal / Confidential Restricted
![Page 5: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/5.jpg)
ERLANG IN MSI
5
![Page 6: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/6.jpg)
ERLANG IN MSI
6
• 183000 lines of Erlang code in 531 modules• 89 gen_fsm• 104 gen_server
• 71000 lines of Erlang headers• 7000 lines of C code• 2 C ports• 5 Erlang applications (3 releasables for 1 product)
![Page 7: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/7.jpg)
ERLANG IN MSI
7
Many iterations, with different teams of people1. Prototype, with The Prophet of Erlang2. Product, with surprised C developers3. Maintenance, with people enthusiastic about
Erlang
![Page 8: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/8.jpg)
THE COMMON SPECIMENSTHE ANTI-PATTERNS
![Page 9: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/9.jpg)
THE SHIFTING STATE
9
tail
state modifying operation
not even the final form
eye
![Page 10: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/10.jpg)
THE SHIFTING STATE
10
• Immutability can be annoying to the unprepared• A lot of things can happen in a single state machine
transition• Complicated protocol in an overcomplicated
machine• Too much data in the state
![Page 11: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/11.jpg)
LESS RESPONSIBILITYToo much information stored in the state of a single process
Example• Problem: state machine of call controller has counters
in state needed for messages sent to remote monitor• Solution: create a dedicated process that keeps track
of these counters
However• Complexity might be inherent• Untangling can be non-trivial (might introduce defects)
11
![Page 12: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/12.jpg)
ASK WHAT YOU NEED
• Many state changing functions• do not demand all fields in the state• do not change all fields in the state
• Example:
• Chain of state passing can be hard to untangle• Might reveal the “real” state that is needed (here y)
12
fn(my_state, S = #state{x=X1, y=Y1}) -> Y2 = f(Y1), {X2,Y3} = g(X1, Y2), {next_state, my_state, S#state{x=X2, y=Y3}}.
fn(my_state, S1) -> S2 = f(S1), S3 = g(S2), {next_state, my_state, S3}.
![Page 13: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/13.jpg)
HOMEBREW “STATE MONAD”
13
no dependencies
every computation is wrappedin a (anonymous) function
one kind of operation: takes state and evaluates to new state
![Page 14: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/14.jpg)
ERLANDO STATE MONAD
14
do is expandedbefore compilation
an underscore ‘_’ in a right-hand-side expression is wrapped in an anonymous function
different kinds of operations
![Page 15: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/15.jpg)
CASE STUDY
15
uses state, only has side-effect; no return value
computation returning value of other type than state …
… demanded by later computation
easy to mix up one of the many versions of the state (esp. when adding or removing operations)
![Page 16: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/16.jpg)
ERLANDO STATE MONAD
16
uses state, only has side-effect; no return value
computation returning value of other type than state …
… demanded by later computation
state is managed by monad, less mistakes
![Page 17: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/17.jpg)
THE LONG CASE
17
case in case
long neck
case in case in case
case in case in case in case
secondary case in case in case in case clause
incognita
![Page 18: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/18.jpg)
THE LONG CASE
18
• C-leftover for long sequential processing e.g. validation of a message received
• In C you do not create many small functions if there’s no reuse
• No way to exit early, so cases keep nesting• Sometimes cases are duplicated
![Page 19: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/19.jpg)
THE THROWING VALIDATOR
19
Exceptions to interrupt the control flow
Sequential C-like processing
Horns
![Page 20: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/20.jpg)
THE THROWING VALIDATOR
20
• Attempt to fix sequential, nested cases• Kingdom for a return• Exceptions seemed like a good idea at the time...
![Page 21: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/21.jpg)
CASE STUDY
21
throw used to emulate a C-like return statement
expression might just be a sanity check…
… or the expression might evaluate to a value needed later
![Page 22: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/22.jpg)
HOMEBREW “ERROR MONAD”
22
one type of operation: takes a state and returns a new state
![Page 23: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/23.jpg)
ERLANDO ERROR MONAD
23
when expression evaluates to {error, <reason>} then {error, <reason>} is the result and remaining expressions are not evaluated
![Page 24: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/24.jpg)
SIDE BY SIDE COMPARISON
24
![Page 25: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/25.jpg)
ERLANDO IN PRODUCTION?
25
Clearer codereduces changeof defects
not part of the Erlang release
uses parse transform andundocumented language features
![Page 26: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/26.jpg)
THE MYSTERIOUS API
26
a mysterious macro
unidentified process’ API
camouflage spots
![Page 27: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/27.jpg)
THE MYSTERIOUS API
27
• Macro helps with typos in atom names• gen_fsm call kind of looks like an API already• General confusion with processes (which
process executes what code)
![Page 28: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/28.jpg)
THE MYSTERIOUS API
28
• Define• Your• APIs
![Page 29: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/29.jpg)
THE ABSOLUTE COORDINATOR
29
mean stare
gproc lookup
send based on lookup
![Page 30: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/30.jpg)
THE ABSOLUTE COORDINATOR
30
• The simplest architecture• Frees you from thinking about more complex
and more Erlang solutions
![Page 31: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/31.jpg)
THE ABSOLUTE COORDINATOR
31
• Difficult problem to solve - at the end of the day, there is only one socket
• Coordinator’s responsibility can be limited• Multiple processes can talk to each other
directly• gproc helps solve the problem (but it’s not
free)• Processes can let each other know their pids
![Page 32: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/32.jpg)
THE MINOR OFFENDERS
32
enormous state machine
tusks
long single clause function
![Page 33: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/33.jpg)
THE MINOR OFFENDERS
33
same module name everywhere
stripes
![Page 34: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/34.jpg)
THE MINOR OFFENDERS
34
ohgodpleasedon’tcrash
hard shell
![Page 35: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/35.jpg)
THE MINOR OFFENDERS
35
• More C habits• Big functions that enclose all functionality,
not making small ones if there is no reuse• Complicated domain with complicated
protocols• Easier to make one process than split
properly into several• Desire for interfaces or virtual classes• Defend against everything
![Page 36: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/36.jpg)
HUNTING THE PROBLEMSGENERAL TOOLS
![Page 37: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/37.jpg)
THE ARSENAL
37
![Page 38: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/38.jpg)
THE REFACTOR QUICKCHECK
38
• Get the pre-refactor code• Refactor it• Mock out all the external calls you must keep
(including the sequence)• Generate the outputs of all decision points• Run
![Page 39: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/39.jpg)
THE REFACTOR QUICKCHECK
39
is_rtp_response_equiv(NumTests) -> Collect = fun({Transition, ToState, EndState}, B) -> collect({{Transition, ToState}, EndState#state.eqc_exit_path}, B) end, equiv_ref:check(fun pre_ref_function/1, fun post_ref_function/1, equiv_generator(), fun mocking_setup/0, fun mocking_dynamic_setup/1, Collect, NumTests).
![Page 40: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/40.jpg)
THE REFACTOR QUICKCHECK• 214 lines in 1 clause in 1 function
• 212 lines• 3 functions• 10 clauses for main processing - one for each
scenario• 0 new defects
40
![Page 41: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/41.jpg)
WRANGLER
• Semi-automated refactoring
• Reduces risk of introducing new mistakes
• Desired refactoring might not be supported
• Tool can be extended• learning curve• is the transformation
correct?
41
source: https://xkcd.com/1319/
![Page 42: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/42.jpg)
ELVIS
• Automated code quality check• Can check the most obvious things, however can’t
really prevent bad code• Worth considering e.g. in pull request approval• New code goes through review• A lot of old code
42
![Page 43: WILD ANTI-PATTERNS IN THE · 2017-06-20 · WILD PAWEL ANTEMIJCZUK & MAARTEN FADDEGON. OVERVIEW ... • Problem: state machine of call controller has counters in state needed for](https://reader034.fdocuments.in/reader034/viewer/2022050410/5f86b9cd47a2cc09f75f6d9a/html5/thumbnails/43.jpg)