Simplify for readability

Improve code readability by simplifying complex logic, using Pythonic idioms, and removing unnecessary code. Extract helper functions for complex conditions, avoid code duplication, and leverage Python's built-in features for cleaner code.

copy reviewer prompt

Prompt

Reviewer Prompt

Improve code readability by simplifying complex logic, using Pythonic idioms, and removing unnecessary code. Extract helper functions for complex conditions, avoid code duplication, and leverage Python’s built-in features for cleaner code.

Key practices:

  • Extract helper functions for complex boolean conditions instead of inline logic
  • Use Pythonic idioms like if not items: instead of items == [] (PEP 8)
  • Leverage walrus operator := to reduce redundant dictionary lookups
  • Avoid code duplication by refactoring common patterns
  • Remove commented-out code and unused imports - git tracks history
  • Simplify repeated conditional checks with single variables

Example:

# Instead of:
if self.save_only_first_rank and not self.metadata.is_first_rank():
    return

# Use a helper function:
def is_passive(self):
    return self.save_only_first_rank and not self.metadata.is_first_rank()

if self.is_passive():
    return

# Instead of:
if memory_objs == []:
    return

# Use Pythonic style:
if not memory_objs:
    return

Source discussions