Prompt
Remove redundant operations, unnecessary code blocks, and verbose constructs to improve code clarity and maintainability. Focus on eliminating dead code, avoiding redundant loops, removing unnecessary conditional checks, and using more concise expressions where appropriate.
Key practices:
- Remove unnecessary loops in comprehensions when variables aren’t used
- Eliminate redundant conditional checks (e.g., checking the same condition twice)
- Use generator expressions instead of building intermediate lists
- Inline short-lived variables that don’t add clarity
- Remove unnecessary
passstatements when docstrings are present - Avoid unnecessary
ifblocks when the contained code is a no-op - Use guard clauses to reduce nesting levels
Example of improvement:
# Before: Unnecessary loop variable
CSS_CLASS_TO_ELEMENT_TYPE_MAP = {
element_type().css_class_name: element_type
for element_type in ALL_ONTOLOGY_ELEMENT_TYPES
for tag in element_type().allowed_tags # 'tag' is unused
}
# After: Remove unused loop variable
CSS_CLASS_TO_ELEMENT_TYPE_MAP = {
element_type().css_class_name: element_type
for element_type in ALL_ONTOLOGY_ELEMENT_TYPES
}
# Before: Verbose list building
lines = []
for each in obj:
lines.append(json.dumps(each, **kwargs))
return "\n".join(lines)
# After: Concise generator expression
return "\n".join(json.dumps(each, **kwargs) for each in obj)
This approach reduces cognitive load, improves performance, and makes code more maintainable by eliminating distractions and focusing on essential logic.