False is false and false is True
I was reviewing a codebase recently, and found a subtle bug with passing values. The front-end was sending a boolean flag to the back-end, which was written in Python. The back-end parsed the json payload and converted the text false
to a boolean in Python. Python uses False
, so this actually set the flag to True
- and the behaviour was the opposite as expected.
I discovered this by:
- Wondering why some log messages weren’t showing up.
- Reading the code and noticing some branch conditions based on flags.
- Adding more logs to check which flags are set and which branches are being taken.
- Verified a flag was an unexpected value and noticed a long chain of value passing, so checked the source (sent from the front-end).
- The front-end had the flag stored and retrieved in local storage, and the value there looked correct.
- Found the back-end code that reads the flag from the front-end request.
- Stared at the screen wondering how
false
was evaluating toTrue
. - Realized what was going on. Wondered how it ever worked.
Be careful when passing values between platforms.