State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity...
Transcript of State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity...
![Page 1: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/1.jpg)
Michael Saelee : CS100-F12 talk
Does programming really need to be so hard?State, Concurrency & Complexity
![Page 2: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/2.jpg)
Objectives- explain how complexity impacts soware- distinguish between necessary and
accidental complexity- identify ways we deal with complexity- understand how state affects programs
![Page 3: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/3.jpg)
Objectives (2)- understand the importance of concurrency- recognize ties between state, concurrency,
and complexity- understand how different programming
paradigms deal with state & concurrency
![Page 4: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/4.jpg)
Topics1. Complexity (overview)2. Managing complexity3. State4. Concurrency5. Object oriented programming6. Functional programming
![Page 5: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/5.jpg)
§ Complexity
![Page 6: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/6.jpg)
What makes programming hard?- language & API- code volume- algorithmic complexity- performance requirements- backwards/forwards compatibility- complexity
![Page 7: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/7.jpg)
Complexity is the root cause of the vast majority of problems with soware today. Unreliability, late delivery, lack of security — oen even poor performance in large-scale systems can all be seen as deriving ultimately om unmanageable complexity.
- Ben Moseley and Peter Marks, Out of the Tar Pit
![Page 8: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/8.jpg)
but what makes this interesting is that we can partition complexity into different categories
![Page 9: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/9.jpg)
essential complexity arises from the actual problem we’re trying to solve.e.g., determine the next best chess move, given all prior moves and the rules of the game
![Page 10: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/10.jpg)
in an ideal world, a sufficiently detailed problem description can be used to automatically generate a working solution (!)
![Page 11: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/11.jpg)
layout of a chess board, rules of the game, meaning of “good” outcomes (e.g., checkmate)
Perfect Chess Artificial Intelligence
Magic solution machine
![Page 12: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/12.jpg)
accidental complexity arises because our programming tools aren’t perfect:
- expressing logic via (imperfect) language - user defined data types (e.g., chessboard)- managing derived data (e.g., game tree)- performance optimization
![Page 13: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/13.jpg)
we want to minimize accidental complexityi.e., don’t make it harder than it needs to be!
![Page 14: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/14.jpg)
§ Managing complexity
![Page 15: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/15.jpg)
Ask any project manager: How do we deal with complexity in a large soware project?
… add more programmers!
![Page 16: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/16.jpg)
… our estimating techniques fallaciously confuse effort with progress, hiding the assumption that men and months are interchangeable.
Adding manpower to a late soware project makes it later.
- Frederick P. Brooks, e Mythical Man-Month
![Page 17: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/17.jpg)
Ask any project manager: How do we deal with complexity in a large soware project?
… add more programmers!
![Page 18: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/18.jpg)
1. Do not make overly-optimistic estimates2. Plan appropriately (reasoning) 3. Divide and conquer (modularization)4. Test, test, test! (testing)
![Page 19: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/19.jpg)
Reasoning:- pencil & paper planning- algorithmic proofs- “hammock-driven development”
… but the brain’s working store is very limited!
![Page 20: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/20.jpg)
Modularization:- break problem into manageable pieces- work on each piece separately- define clear application programming
interfaces (APIs) to connect them
![Page 21: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/21.jpg)
Testing:- ideally, start before implementation
- given input, specify output/behavior- unit tests for discrete program modules- perform continuous integration
![Page 22: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/22.jpg)
Testing granularity?- functions- packages- programs- systems
![Page 23: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/23.jpg)
Ideally, modules being tested are composable.e.g., if A is tested, and B is tested, then we know that A + B works predictably.
![Page 24: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/24.jpg)
Modularization & Composability allow us to use prior work & ignore their implementation
![Page 25: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/25.jpg)
i.e., modularization & composability give us automatic abstraction
(and programmers ♥ abstraction!)
![Page 26: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/26.jpg)
Civilization advances by extending the number of important operations which we can perform without thinking.
- Alfred North Whitehouse
![Page 27: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/27.jpg)
but … what defeats composability?
![Page 28: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/28.jpg)
§ State
![Page 29: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/29.jpg)
state |stāt|noun1 the particular condition that someone or something is in at a specific time
vs.
![Page 30: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/30.jpg)
given a particular set of inputs, a stateless function always returns the same result
a.k.a. pure function
![Page 31: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/31.jpg)
mathematical functions are stateless
- we can always replace identical function calls with the same value
- known as referential transparency
Z 2
0x dx ·
✓Z 2
0x dx+ 5 ·
Z 2
0x dx
◆= ?
= 2
Z 2
0x dx =
x
2
2
�2
0
![Page 32: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/32.jpg)
referential transparency supports composability— if we test a pure function in isolation, it will work exactly as predicted anywhere we use it.
![Page 33: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/33.jpg)
def quadratic_roots(a,b,c): d = discriminant(a,b,c) if d == 0: return -b / 2*a elif d > 0: sqrt_d = math.sqrt(d) return ((-b+sqrt_d)/2*a, (-b-sqrt_d)/2*a) else: return "No real roots!"
def discriminant(a,b,c): return b*b - 4*a*c
quadratic_roots(1,4,4) => -2
quadratic_roots(1,-1,-2) => (2.0, -1.0)
quadratic_roots(1,3,8) => “No real roots!”
(� = b
2 � 4ac
x = �b±p�
2a
![Page 34: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/34.jpg)
however, functions in programming languages may reference and update mutable state, which can affect the result of its computations
![Page 35: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/35.jpg)
num_times = 0
def foo(): global num_times num_times += 1 if num_times < 100: return 10 else: return "I'm too old for this!"
# => “I’m too old for this!”
# => 10foo()
for _ in range(99): foo()
foo()
# assume we don't know what went before ...
foo() + foo() # => ?
![Page 36: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/36.jpg)
num_times = 0
def foo(): global num_times num_times += 1 if num_times < 100: return 10 else: return "I'm too old for this!"
we say that the function foo has side effects (or is a stateful function)
![Page 37: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/37.jpg)
functions with side effects are harder to test — their results are dependent on the current stateeven worse: this state may be modified by other stateful functions, too!
![Page 38: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/38.jpg)
One of the issues (that affects both testing and reasoning) is the exponential rate at which the number of possible states grows — for every single bit of state that we add we double the total number of possible states.
- Ben Moseley and Peter Marks, Out of the Tar Pit
![Page 39: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/39.jpg)
note: if an otherwise stateless function calls a stateful function, it is no longer referentially transparenti.e., statefulness is contagious!
![Page 40: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/40.jpg)
Anyone who has ever telephoned a support desk for a soware system and been told to “try it again”, or “reload the document”, or “restart the program”, or “reboot your computer” or “re-install the program” or even “re- install the operating system and then the program” has direct experience of the problems that state causes for writing reliable, understandable soware.
- Ben Moseley and Peter Marks, Out of the Tar Pit
![Page 41: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/41.jpg)
reasoning about stateful programs is hard … we need to consider all possible paths through a program and how changes to state affect results
![Page 42: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/42.jpg)
but … what if more than one path were being taken through our programs simultaneously?
![Page 43: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/43.jpg)
§ Concurrency
![Page 44: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/44.jpg)
e ee lunch is over. We have grown used to the idea that our programs will go faster when we buy a next-generation processor, but that time has passed. While that next-generation chip will have more CPUs, each individual CPU will be no faster than the previous year’s model. If we want our programs to run faster, we must learn to write parallel programs.
- Simon Peyton Jones, Beautiful Concurrency
![Page 45: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/45.jpg)
concurrency within a program = multiple threads executing functions (potentially the same ones) simultaneously
![Page 46: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/46.jpg)
results = {}
def cache_result(a,b,c): results[(a,b,c)] = quadratic_roots(a,b,c)
results[(1,4,4)] # => -2
results[(1,3,2)] # => (-1.0, -2.0)
results[(1,1,1)] # => No real roots!
# allow for 10 threads to run concurrentlypool = ThreadPool(10)
for a in range(1,5): for b in range(1,5): for c in range(1,5): pool.add_task(cache_result, a, b, c)
![Page 47: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/47.jpg)
def foo(n): global shared for _ in range(n): shared = shared + 1
def bar(n): global shared for _ in range(n): shared = shared + 1
def test(n): global shared shared = 0 pool = ThreadPool(2) pool.add_task(foo, n) pool.add_task(bar, n) pool.wait_completion() print(shared_var)
=> 692171
=> 1000
=> 10000
=> 81443
test(500)
test(5000)
test(50000)
test(500000)
test(50) => 100
![Page 48: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/48.jpg)
results are non-deterministic— caused by “race conditions”
![Page 49: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/49.jpg)
Concurrency also affects testing, for in this case, we can no longer even be assured of result consistency when repeating tests on a system — even if we somehow ensure a consistent starting state. Running a test in the presence of concurrency with a known initial state and set of inputs tells you nothing at all about what will happen the next time you run that very same test with the very same inputs and the very same starting state. . . and things can’t really get any worse than that.
- Ben Moseley and Peter Marks, Out of the Tar Pit
![Page 50: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/50.jpg)
side effects + concurrency kill composability, and make reasoning & testing nigh impossible!
![Page 51: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/51.jpg)
how do our programming language paradigms deal with state & concurrency?
![Page 52: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/52.jpg)
§ Object Oriented Programming
![Page 53: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/53.jpg)
OOP essentials:- identify the nouns in a problem- model them with user-defined classes- instances of these classes encapsulate data
with behavior-defining methods
![Page 54: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/54.jpg)
OOP implies stateful programming.
![Page 55: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/55.jpg)
OOP also simplifies (but complicates) identity- identity is disconnected from state- an object’s state (attributes) can change, but
it’s still considered the same object
Student s = new Student()s.setName("Michael");...s.setName("Jane"); // `s` is still the same object!
![Page 56: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/56.jpg)
In the real world, objects perpetually advance through separate, instantaneous states
- we conveniently use names (i.e., references) to refer to the most recent state
- we can’t go back and changea state (a moment in time!)
![Page 57: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/57.jpg)
OOP lacks state stability!- while a thread is observing an object, its
state may be changed by another thread- to avoid issues, use locks to prevent threads
from concurrently accessing objects- inefficient & really hard to test
![Page 58: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/58.jpg)
OOP is not particularly suitable for writing concurrent programs!
![Page 59: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/59.jpg)
§ Functional Programming
![Page 60: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/60.jpg)
Functional programming essentials:- eschew stateful computation- no statements, only expressions- prefer (or require) all functions to be pure- functions are “first class” — i.e., they can be
created, stored, and passed like other data
![Page 61: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/61.jpg)
discriminant (a,b,c) = b**2 - 4*a*c
quadratic_roots (a,b,c) = [(-b+d)/2*a, (-b-d)/2*a] where d = sqrt (discriminant (a,b,c))
quadratic_roots (1,4,4) => [-2.0,-2.0]
eqns = [(a,b,c) | a <- [1..4], b <- [1..4], c <- [1..4]]
map discriminant eqns => [-3.0,-7.0,-11.0,...,-32.0,-48.0]
map quadratic_roots $ filter ((>= 0) . discriminant) eqns => [[-1.0,-1.0],[-0.38,-2.62],...,[-3.0,-9.0],[-0.5,-0.5]]
filter ((>= 0) . discriminant) eqns => [(1,2,1),(1,3,1),(1,3,2),(1,4,1),(1,4,2),(1,4,3), (1,4,4),(2,3,1),(2,4,1),(2,4,2),(3,4,1),(4,4,1)]
![Page 62: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/62.jpg)
functional purity → referential transparency- great for composability- huge boon to reasoning and testing- enables automatic performance
optimizations (e.g., memoization)
![Page 63: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/63.jpg)
but … not everything’s a pure function!- “search the web for ‘memoization’”- “remotely start my car”- “fire my shrink-ray!”
![Page 64: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/64.jpg)
key is to minimize & isolate state manipulation- separate pure & impure aspects- distinguish identity and state- enable comprehensive testing and
high level reasoning
![Page 65: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/65.jpg)
many topics to explore!- message-passing frameworks- soware transactional memory- monads & monadic composition- languages: Erlang, Clojure, Haskell
![Page 66: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/66.jpg)
Conentional programming languages are growing ever more enormous, but not stronger. Inherent defects at the most basic level cause them to be both fat and weak: their primitive word-at-a-time style of programming …, their close coupling of semantics to state transitions, their division of programming into a world of expressions and a world of statements, their inability to effectively use powerful combining forms for building new programs om existing ones, and their lack of useful mathematical properties for reasoning about programs.
- John Backus, Can Programming Be Liberated om the on Neumann Style? (1978)
![Page 67: State, Concurrency & ComplexityState, Concurrency & Complexity Objectives - explain how complexity impacts so ware - distinguish between necessary and accidental complexity - identify](https://reader030.fdocuments.in/reader030/viewer/2022041015/5ec65b665d00b902244b8498/html5/thumbnails/67.jpg)
References:- Frederick P. Brooks, "e Mythical Man-Month."- Ben Moseley and Peter Marks, "Out of the Tar Pit."- Simon Peyton Jones, "Beautiful Concurrency."- Rich Hickey, "Are We ere Yet?"- John Backus, "Can Programming Be Liberated from the
von Neumann Style?"