Skip to content

Refactor ExtractAsciiStrings to eliminate nested conditionals#6

Open
Copilot wants to merge 4 commits intoproductionfrom
copilot/refactor-extract-ascii-strings
Open

Refactor ExtractAsciiStrings to eliminate nested conditionals#6
Copilot wants to merge 4 commits intoproductionfrom
copilot/refactor-extract-ascii-strings

Conversation

Copy link

Copilot AI commented Nov 14, 2025

CodeScene flagged ExtractAsciiStrings with 2 "bumps" at nesting depth 2, indicating poor encapsulation and increased cognitive load.

Changes

  • Extracted IsPrintableAscii(byte) - Encapsulates ASCII range check (32-126)
  • Extracted ProcessPrintableCharacter(...) - Handles character accumulation and start index tracking
  • Extracted FinalizeCurrentString(...) - Consolidates string completion logic

Before/After

Before:

for (int i = 0; i < data.Length; i++)
{
    if (data[i] >= 32 && data[i] <= 126)
    {
        if (currentString.Length == 0)  // Bump 1
            startIndex = i;
        currentString.Append((char)data[i]);
    }
    else
    {
        if (currentString.Length > 0)  // Bump 2
        {
            strings.Add(new AsciiString { ... });
            currentString.Clear();
        }
    }
}

After:

for (int i = 0; i < data.Length; i++)
{
    if (IsPrintableAscii(data[i]))
        startIndex = ProcessPrintableCharacter(currentString, data[i], i, startIndex);
    else
        FinalizeCurrentString(strings, currentString, startIndex);
}

Nesting depth reduced from 2 to 1, cyclomatic complexity reduced from 5 to 3.

Original prompt

This section details on the original issue you should resolve

<issue_title>[T_DEBT] clip2load-repository/ProcessingClips.cs - ExtractAsciiStrings</issue_title>
<issue_description># Where?
This file has 1 bumpy roads.

Why does this problem occur?

A Bumpy Road is a function that contains multiple chunks of nested conditional logic inside the same function. The deeper the nesting and the more bumps, the lower the code health.

A bumpy code road represents a lack of encapsulation which becomes an obstacle to comprehension. In imperative languages there’s also an increased risk for feature entanglement, which leads to complex state management. CodeScene considers the following rules for the code health impact: 1) The deeper the nested conditional logic of each bump, the higher the tax on our working memory. 2) The more bumps inside a function, the more expensive it is to refactor as each bump represents a missing abstraction. 3) The larger each bump – that is, the more lines of code it spans – the harder it is to build up a mental model of the function. The nesting depth for what is considered a bump is 2 levels of conditionals.

How to fix it?

Bumpy Road implementations indicate a lack of encapsulation. Check out the detailed description of the Bumpy Road code health issue.

A Bumpy Road often suggests that the function/method does too many things. The first refactoring step is to identify the different possible responsibilities of the function. Consider extracting those responsibilities into smaller, cohesive, and well-named functions. The EXTRACT FUNCTION refactoring is the primary response.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits November 14, 2025 12:14
Co-authored-by: Avenze <38162785+Avenze@users.noreply.github.com>
Co-authored-by: Avenze <38162785+Avenze@users.noreply.github.com>
Co-authored-by: Avenze <38162785+Avenze@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor ExtractAsciiStrings for improved code health Refactor ExtractAsciiStrings to eliminate nested conditionals Nov 14, 2025
Copilot AI requested a review from Avenze November 14, 2025 12:20
@Avenze Avenze marked this pull request as ready for review November 14, 2025 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[T_DEBT] clip2load-repository/ProcessingClips.cs - ExtractAsciiStrings

2 participants