unzip-bot icon indicating copy to clipboard operation
unzip-bot copied to clipboard

[CRITICAL] (PY-R1000) Function with cyclomatic complexity higher than threshold

Open EDM115 opened this issue 1 year ago • 3 comments

Description

A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.

Occurrences

There are 3 occurrences of this issue in the repository.

See all occurrences on DeepSource → app.deepsource.com/gh/EDM115/unzip-bot/issue/PY-R1000/occurrences/

EDM115 avatar Jan 18 '24 12:01 EDM115

https://chat.openai.com/share/e6166e61-bfca-44e1-8c24-065b6df09b2e

EDM115 avatar Jan 18 '24 12:01 EDM115

related to #159 :

from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3

# ... [Your existing code] ...

async def send_file(unzip_bot, c_id, doc_f, query, full_path, log_msg, split):
    # ... [Your existing code] ...
    try:
        # ... [Your existing code] ...

        # Attempt to extract audio metadata
        audio_meta = {}
        try:
            audio = MP3(doc_f, ID3=EasyID3)
            audio_meta['duration'] = int(audio.info.length)  # Duration in seconds
            audio_meta['performer'] = audio.get('artist', [None])[0]  # Performer name
            audio_meta['title'] = audio.get('title', [None])[0]  # Track name
        except Exception as e:
            LOGGER.error(f"Error extracting metadata: {e}")

        # ... [Your existing code for upmsg and thumbornot] ...

        if ul_mode == "media" and fext in extentions_list["audio"]:
            send_kwargs = {
                'chat_id': c_id,
                'audio': doc_f,
                'caption': Messages.EXT_CAPTION.format(fname),
                'disable_notification': True,
                'progress': progress_for_pyrogram,
                'progress_args': (
                    Messages.TRY_UP.format(fname),
                    upmsg,
                    time(),
                    unzip_bot,
                ),
                'duration': audio_meta.get('duration'),
                'performer': audio_meta.get('performer'),
                'title': audio_meta.get('title'),
            }

            if thumbornot:
                send_kwargs['thumb'] = Config.THUMB_LOCATION + "/" + str(c_id) + ".jpg"

            await unzip_bot.send_audio(**send_kwargs)
            
    # ... [Your existing code] ...

using kwargs might not be a bad idea

EDM115 avatar Jan 18 '24 17:01 EDM115

DESCRIPTION
A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.
Functions with high cyclomatic complexity are more likely to have bugs and be harder to test. They may lead to reduced code maintainability and increased development time.
To reduce the cyclomatic complexity of a function, you can:

  • Break the function into smaller, more manageable functions.
  • Refactor complex logic into separate functions or classes.
  • Avoid multiple return paths and deeply nested control expressions.
Risk category Cyclomatic complexity range Recommended action
low 1-5 No action needed.
medium 6-15 Review and monitor.
high 16-25 Review and refactor. Recommended to add comments if the function is absolutely needed to be kept as it is.
very-high 26-50 Refactor to reduce the complexity.
critical >50 Must refactor this. This can make the code untestable and very difficult to understand.

Occurrences

  • unzipper/modules/commands.py add_vip has a cyclomatic complexity of 16 with "high" risk
  • unzipper/modules/ext_script/metadata_helper.py get_audio_metadata has a cyclomatic complexity of 20 with "high" risk
  • unzipper/modules/ext_script/up_helper.py send_file has a cyclomatic complexity of 27 with "very-high" risk
  • unzipper/modules/callbacks.py unzipper_cb has a cyclomatic complexity of 169 with "critical" risk

EDM115 avatar Feb 12 '24 10:02 EDM115