Side effects
While working on his tower defense game, Steve noticed some unexpected behavior. Units were taking inconsistent damage from towers. After some investigation, he realized the damage calculation function relied on a global variable that could change unpredictably - a classic side effect.
Side effects in programming refer to any application state changes that occur outside the function being executed. These changes could include modifying a global or static variable, changing the original value of function parameters, performing I/O operations, or even throwing an exception. Side effects make the behavior of a function dependent on the context, reducing predictability and potentially increasing bugs.
Common sources of side effects
When writing code, it’s good to know where side effects might come from. Side effects can make code unpredictable. Let’s break down some common sources.
Global variables
Problem: Using global variables can lead to unexpected...