Prompt
Place code in the most specific and appropriate location to improve findability and maintainability. Follow these principles:
- Avoid generic utility modules - place code in specific, relevant modules
- Keep functions in the most specific scope possible
- Extract repeated logic into dedicated functions
- Split large files into focused modules
- Prefer module-level functions over static methods when no instance state is needed
Example - Instead of:
# generic util.py
def sanitize(key): ...
class CustomCollector:
def _translate_to_prometheus(self, metric_record):
# using global sanitize function
label_keys.append(sanitize(label_key))
Better approach:
# prometheus_translator.py
class CustomCollector:
def _translate_to_prometheus(self, metric_record):
def sanitize(key): # Function in most specific scope
# sanitization logic
pass
label_keys.append(sanitize(label_key))
This organization improves code findability, reduces coupling, and makes the codebase more maintainable.