Skip to content

Simplify String Length Comparison

Documentation Index

Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt

Use this file to discover all available pages before exploring further.

Sourcery refactoring id: simplify-string-len-comparison

Section titled “Sourcery refactoring id: simplify-string-len-comparison”

Changes an indirect comparison of a string’s length to 0 into a direct comparison of the string to the empty string.

if len(s) == 0:
...
if len(r) > 0:
...
if s == "":
...
if r != "":
...

This refactoring avoids an unnecessary calculation and keeps the logic in the domain of string types. It tends to unlock further improvements to string comparisons.