Why Using 'or' for Default Config Values Is a Silent and Dangerous Bug

A developer running a real-money trading bot discovered that a single line of code caused the bot to operate with an unintended default capital value for months. The root cause was using Python's 'or' operator to fall back to a default, which silently substitutes the default not only when a variable is missing but also when its value is zero or any other falsy value. When an environment variable was wiped during a system restore, the program raised no error and continued running with the hardcoded fallback undetected. The same flaw exists across languages, including JavaScript's '||' operator and Go's empty-string checks, making it a widespread anti-pattern in configuration handling. The recommended fix is to either require critical variables to be explicitly set — crashing loudly on absence — or use language constructs like Python's 'getenv(key, default)' or JavaScript's nullish coalescing operator '??' that distinguish between a missing value and a legitimate zero.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in