Code Smell 230 - Schr?dinger Code

Code Smell 230 - Schr?dinger Code

Your code is dead and alive

TL;DR: Look carefully for race conditions

Problems

Solutions

  1. Avoid race conditions
  2. Avoid global variables
  3. Use proper synchronization

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

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

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

Disclaimer

Code Smells are my opinion.

Credits

Photo by Yerlin Matu on Unsplash


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

Alan Kay


This article is part of the CodeSmell Series.


要查看或添加评论,请登录

Maximiliano Contieri的更多文章

  • Code Smell 295 - String Concatenation

    Code Smell 295 - String Concatenation

    Untangling the string mess in your code TL;DR: Avoid string concatenation for complex strings, use templates. Problems…

  • Code Smell 294 - Implicit Return

    Code Smell 294 - Implicit Return

    Your language adds clever features. Making YOU more obsolete TL;DR: Overusing implicit returns makes your code harder…

  • The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    How AI is Redefining the Role of Programmers in the Tech Industry TL;DR: AI-generated code outperforms lazy…

    1 条评论
  • Refactoring 024 - Replace Global Variables with Dependency Injection

    Refactoring 024 - Replace Global Variables with Dependency Injection

    Break Hidden Dependencies for Cleaner Code TL;DR: Replace global variables with dependency injection to improve…

  • 10 More Simple Tips to Boost Your Productivity x2

    10 More Simple Tips to Boost Your Productivity x2

    The previous article on life hacks was a huge success. Let's start the year with more productivity tips! TL;DR: More…

  • Code Smell 293 - isTesting

    Code Smell 293 - isTesting

    Don’t let test code sneak into production TL;DR: Avoid adding isTesting or similar flags. Problems ?? Leaky abstraction…

  • Refactoring 001 - Remove Setters

    Refactoring 001 - Remove Setters

    Setters violate immutability and add accidental coupling TL;DR: Make your attributes private to favor mutability…

  • Code Smell 292 - Missing Return

    Code Smell 292 - Missing Return

    When your code loses its way TL;DR: Missing return statements cause unexpected behavior. Problems ?? Silent failures…

  • Code Smell 291 - Mutable Keys

    Code Smell 291 - Mutable Keys

    Changing Keys, Losing Values TL;DR: When you use mutable objects as keys in hashed collections, changing them breaks…

    2 条评论
  • Refactoring 023 - Replace Inheritance with Delegation

    Refactoring 023 - Replace Inheritance with Delegation

    Transform your rigid inheritance into flexible delegations TL;DR: Replace restrictive inheritance hierarchies with…

社区洞察

其他会员也浏览了