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:

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