Back to all reviewers

avoid code duplication

home-assistant/core
Based on 8 comments
Python

Create reusable components instead of duplicating similar code patterns. When you find yourself writing similar classes, functions, or logic blocks, extract the common functionality into base classes, helper functions, or generic components that can be parameterized.

Code Style Python

Reviewer Prompt

Create reusable components instead of duplicating similar code patterns. When you find yourself writing similar classes, functions, or logic blocks, extract the common functionality into base classes, helper functions, or generic components that can be parameterized.

Examples of duplication to avoid:

  • Multiple entity classes with similar structure - create a base entity class
  • Repeated device info logic across platforms - move to shared base entity or helper function
  • Similar validation or processing logic - extract to utility functions in separate modules
  • Multiple classes differing only in small details - create generic class with configurable descriptions

For instance, instead of creating separate button classes:

class SetSleepTimerButton(ButtonEntity):
    # implementation

class ClearSleepTimerButton(ButtonEntity):  
    # similar implementation

Create a generic class with descriptions:

class GenericButton(ButtonEntity):
    def __init__(self, description: ButtonEntityDescription):
        self.entity_description = description
        
    async def async_press(self) -> None:
        await self.entity_description.press_fn()

This approach improves maintainability, reduces bugs, and makes code more readable by eliminating repetitive patterns.

8
Comments Analyzed
Python
Primary Language
Code Style
Category

Source Discussions