Skip to content

Refactor: Reduce the Cognitive Complexity of digest_args method #4273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
murilodepa opened this issue Jun 7, 2025 · 0 comments
Open

Refactor: Reduce the Cognitive Complexity of digest_args method #4273

murilodepa opened this issue Jun 7, 2025 · 0 comments

Comments

@murilodepa
Copy link

murilodepa commented Jun 7, 2025

Description:

  • The digest_args method has a Cognitive Complexity of 33, exceeding the recommended limit of 15. This makes the method difficult to read, understand, and maintain, especially for new contributors or in scenarios requiring modifications.
  • The function handles many different responsibilities, such as processing CLI arguments, updating configurations, and handling flags, which contributes to its high complexity. Breaking it into smaller, focused helper methods would significantly improve its readability and maintainability.

Motivation:

  • Readability: Reducing complexity makes the code easier to understand, especially for new contributors;
    
  • Maintainability: Smaller, well-defined methods are easier to test and modify without introducing bugs;
    
  • Tool Conformance: Addressing this issue aligns the code with SonarQube's recommended standards and improves overall code quality.
    

Impact on Existing Code:

  • The functionality of the digest_args method should remain unchanged.
    
  • Existing tests should be updated or added to ensure the refactored method behaves as expected.
    

Proposed Solution:

Refactor the digest_args method into smaller helper methods, each responsible for a specific aspect of the configuration process. For example in this file _"manim/config/utils.py":

1. Handle Config Files

def _handle_config_files(self, args: argparse.Namespace):
    if args.file.suffix == ".cfg":
        args.config_file = args.file
    if str(args.file) == "-":
        self.input_file = args.file
    if args.config_file:
        self.digest_file(args.config_file)

2. Handle CLI Arguments

def _update_from_args(self, args: argparse.Namespace, keys: List[str]):
    for key in keys:
        if hasattr(args, key):
            attr = getattr(args, key)
            if attr is not None:
                self[key] = attr

3. Process Flags and Quality

def _process_flags(self, args: argparse.Namespace):
    if self["save_last_frame"]:
        self["write_to_movie"] = False
    nflag = args.from_animation_number
    if nflag:
        self.from_animation_number = nflag[0]
        try:
            self.upto_animation_number = nflag[1]
        except Exception:
            logger.info(
                f"No end scene number specified in -n option. Rendering from {nflag[0]} onwards..."
            )
    self.quality = _determine_quality(getattr(args, "quality", None))

The refactored digest_args method would look like this:

def digest_args(self, args: argparse.Namespace) -> Self:
    self._handle_config_files(args)
    self._update_from_args(args, [
        "notify_outdated_version", "preview", "show_in_file_browser", 
        "write_to_movie", "save_last_frame", "save_pngs", "save_as_gif",
        # ... other keys
    ])
    self._process_flags(args)
    # Add additional helper calls here
    return self

References:

  • SonarQube Report: Cognitive Complexity Exceeded (33 > 15)
    
  • Affected Method: digest_args
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

No branches or pull requests

1 participant