-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-mutable-ripple.err
More file actions
47 lines (40 loc) · 1.09 KB
/
Copy path03-mutable-ripple.err
File metadata and controls
47 lines (40 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# SPDX-License-Identifier: MPL-2.0
# Example: Mutation Ripple Effect
# Learning: Mutable state affects code stability
main
# Mutable counter
let mut counter = 0
# Mutation creates "ripples"
counter = counter + 1
# Every function that reads counter is now "tainted"
println("Counter:", counter)
display(counter)
calculate(counter)
# Try making counter immutable (remove 'mut')
# Notice stability increase
end
function display(n)
println("Display:", n)
end
function calculate(n)
let result = n * 2
println("Calculate:", result)
end
# 🔴 EXPECTED CONSEQUENCES:
# - Mutation at line 8: -10 stability
# - 3 readers (println, display, calculate): -15 stability
# - Total: stability = 75
#
# 🎓 LEARNING OUTCOME:
# Mutable state creates invisible dependencies.
# Every mutation affects all readers.
# This is why functional programming prefers immutability!
#
# 💡 STABILIZATION:
# Make immutable, pass as parameter → stability: 100
#
# ALTERNATIVE CODE:
# let counter = 0
# let counter = counter + 1
# # Each "mutation" creates a new value
# # Stability: 100