Back to all reviewers

Optimize code location scope

open-telemetry/opentelemetry-python
Based on 7 comments
Python

Place code in the most specific and appropriate location to improve findability and maintainability. Follow these principles: 1. Avoid generic utility modules - place code in specific, relevant modules

Code Style Python

Reviewer Prompt

Place code in the most specific and appropriate location to improve findability and maintainability. Follow these principles:

  1. Avoid generic utility modules - place code in specific, relevant modules
  2. Keep functions in the most specific scope possible
  3. Extract repeated logic into dedicated functions
  4. Split large files into focused modules
  5. 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.

7
Comments Analyzed
Python
Primary Language
Code Style
Category

Source Discussions