Skip to content

Reduce cognitive complexity in text selection distance calculation#8952

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/refactor-cognitive-complexity-again
Open

Reduce cognitive complexity in text selection distance calculation#8952
Copilot wants to merge 3 commits intomasterfrom
copilot/refactor-cognitive-complexity-again

Conversation

Copy link
Contributor

Copilot AI commented Dec 26, 2025

How the feature works? / How did you fix the issue?

  • Summary: Refactored findClosestText distance calculation to lower cognitive complexity while preserving selection behavior around text bounding boxes.
  • Changes
    • Added a clamped-coordinate helper to compute cursor-to-text distance in a single path, avoiding nested conditionals.
    • Kept selection threshold logic intact, using minimal-distance tracking consistent with prior behavior.
  • Example
    const dist = calculateDistanceToText(cursorPosition, text.getReferencePoints());
    if (dist < SELECTION_DISTANCE_COEFFICIENT && (!ret || dist < minDist)) {
      minDist = dist;
      ret = { id, dist: minDist };
    }

Check list

  • unit-tests written
  • e2e-tests written
  • documentation updated
  • PR name follows the pattern #1234 – issue name
  • branch name doesn't contain '#'
  • PR is linked with the issue
  • base branch (master or release/xx) is correct
  • task status changed to "Code review"
  • reviewers are notified about the pull request
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor restruct.texts.forEach((text, id) function to reduce its Cognitive Complexity from 22 to the 15 allowed.</issue_title>
<issue_description>Problem:
Refactor restruct.texts.forEach((text, id) function to reduce its Cognitive Complexity from 22 to the 15 allowed.
Cognitive Complexity of functions should not be too high

Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.

As a rule of thumb, high cognitive complexity is a sign that the code should be refactored into smaller, easier-to-manage pieces.
Which syntax in code does impact cognitive complexity score?

Here are the core concepts:

  • Cognitive complexity is incremented each time the code breaks the normal linear reading flow.
    This concerns, for example, loop structures, conditionals, catches, switches, jumps to labels, and conditions mixing multiple operators.
  • Each nesting level increases complexity.
    During code reading, the deeper you go through nested layers, the harder it becomes to keep the context in mind.
  • Method calls are free
    A well-picked method name is a summary of multiple lines of code. A reader can first explore a high-level view of what the code is performing then go deeper and deeper by looking at called functions content.
    Note: This does not apply to recursive calls, those will increment cognitive score.

The method of computation is fully detailed in the pdf

Note that the calculation of cognitive complexity at function level deviates from the documented process. Given the functional nature of JavaScript, nesting functions is a prevalent practice, especially within frameworks like React.js. Consequently, the cognitive complexity of functions remains independent from one another. This means that the complexity of a nesting function does not increase with the complexity of nested functions.
What is the potential impact?

Developers spend more time reading and understanding code than writing it. High cognitive complexity slows down changes and increases the cost of maintenance.

How can I fix it?

Noncompliant code example
The code is using a complex condition and has a cognitive cost of 3.

function calculateFinalPrice(user, cart) {
  let total = calculateTotal(cart);
  if (user.hasMembership                       // +1 (if)
    && user.orders > 10                        // +1 (more than one condition)
    && user.accountActive
    && !user.hasDiscount
    || user.orders === 1) {                    // +1 (change of operator in condition)
      total = applyDiscount(user, total);
  }
  return total;
}

Compliant solution
Even if the cognitive complexity of the whole program did not change, it is easier for a reader to understand the code of the calculateFinalPrice function, which now only has a cognitive cost of 1.

function calculateFinalPrice(user, cart) {
  let total = calculateTotal(cart);
  if (isEligibleForDiscount(user)) {       // +1 (if)
    total = applyDiscount(user, total);
  }
  return total;
}

function isEligibleForDiscount(user) {
  return user.hasMembership
    && user.orders > 10                     // +1 (more than one condition)
    && user.accountActive
    && !user.hasDiscount
    || user.orders === 1                    // +1 (change of operator in condition)
}

Problem location:
packages/ketcher-core/src/application/editor/shared/closest.js:40
</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Refactor restruct.texts.forEach function to reduce complexity Reduce cognitive complexity in text selection distance calculation Dec 26, 2025
Copilot AI requested a review from AlexeyGirin December 26, 2025 11:08
@AlexeyGirin AlexeyGirin marked this pull request as ready for review December 26, 2025 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor restruct.texts.forEach((text, id) function to reduce its Cognitive Complexity from 22 to the 15 allowed.

2 participants