Skip to content

Remove Redundant If Statements

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: remove-redundant-if

Section titled “Sourcery refactoring id: remove-redundant-if”

Removes conditional tests where the conditional is always True or False

def hello(name: str):
if name.startswith("L"):
self.sing("Hip Hip Horray! For " + name)
elif not name.startswith("L"):
self.sing("Hello " + name + ", it's nice to meet you.")
def hello(name: str):
if name.startswith("L"):
self.sing("Hip Hip Horray! For " + name)
else:
self.sing("Hello " + name + ", it's nice to meet you.")

This refactoring will re-structure conditionals where Sourcery determines that one of the tests is always True or False. This reveals the actual logic of the conditional to the reader, making the code easier to understand.

See also: remove-redundant-boolean