# Code Smell 230 - Schrödinger Code

> TL;DR: Look carefully for race conditions

# Problems

- Principle of Least Surprise violation

- [Race Conditions](https://en.wikipedia.org/wiki/Race_condition)

# Solutions

1.  Avoid race conditions 

2. Avoid global variables

3. Use proper [synchronization](https://en.wikipedia.org/wiki/Semaphore_(programming))

# Context

Schrödinger code is code that can be in two different states at the same time, but the state of the code is not determined until it is executed. 

This can happen when the code contains a race condition, or when the code depends on the state of a global variable that can be changed by other threads or processes.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/b55ffb2e174db3880e40a162405d8fd1)
```python
import threading

cats_alive = 0

def thread_1():
  cats_alive += 1

def thread_2():
  cats_alive -= 1

if cats_alive > 0:
  feedThem()

# The value of cats_alive is indeterminate, 
# So the code can be in either of the two states:
#
# 1. cats_alive > 0 and feedThem() is called.
# 2. cats_alive <= 0 and feedThem() is not called.

```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/d147d5084dc7c0680f4ff01c10cce152)
```python
import threading

lock = threading.Lock()
cats_alive = 0

def thread_1():
  with lock:
    cats_alive += 1

def thread_2():
  with lock:
    cats_alive -= 1

if cats_alive > 0:
  feedThem()

# With the lock, the two threads cannot access 
# the `cats_alive` variable at the same time.
# This means that the value of `cats_alive` is always determined, 
# and the program will not exhibit Schrödinger code behavior.

```

# Detection

[X] Manual

Make code reviews on concurrent code

# Tags

- Concurrency

- Globals

# Conclusion

To avoid Schrödinger code, avoid race conditions and avoid depending on the state of global variables that can be changed by other threads or processes.

If you need to use a global variable in your code, ensure it is correctly synchronized.

# Relations

%[https://maximilianocontieri.com/code-smell-198-hidden-assumptions]

%[https://maximilianocontieri.com/code-smell-32-singletons]

%[https://maximilianocontieri.com/code-smell-60-global-classes]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Yerlin Matu](https://unsplash.com/@yerlinmatu) on [Unsplash](https://unsplash.com/photos/shallow-focus-photography-of-white-and-brown-cat-GtwiBmtJvaU)  
  
* * *

> The last thing you wanted any programmer to do is mess with internal state

_Alan Kay_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
