Logo Complexity Reduction

Complexity Reduction

Cộng đồng
rustyrazorblade
complexity-reduction

Measure cognitive complexity and duplicate code with PMD (Java) or rust-code-analysis-cli (Rust), then reduce both by pure-move extraction, re-measuring after every step. Runs over a subsystem or over a PR diff. Use this when the user asks to simplify, refactor, clean up, or reduce the complexity of a subsystem, asks "how complex is X", asks to find duplicate code, asks to make code "beautiful", or asks for a complexity check on a diff or PR. It measures before it edits and re-measures after; it never accepts an unmeasured claim that a change helped. It reports only, unless the user passes `--refactor`. It is not `/simplify`, which reads the current diff and has no metric.

Tổng quan

Nhà phát hànhrustyrazorblade
Kho mãskills
Tên kỹ năngcomplexity-reduction
Sao
44
Bản fork
9
Tệp đi kèm
1
Giấy phépApache-2.0
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI là Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 1 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được rustyrazorblade công bố trên GitHub. Hãy đọc nội dung nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Complexity Reduction trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL thư mục kỹ năng bên dưới và xác nhận.
    https://github.com/rustyrazorblade/skills/tree/main/plugins/dev-skills/skills/complexity-reduction
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/rustyrazorblade/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/plugins/dev-skills/skills/complexity-reduction .claude/skills/complexity-reduction
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Complexity Reduction trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự tải Complexity Reduction ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên Complexity Reduction không bị giới hạn ở mô hình mà kỹ năng này được viết cho. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên kỹ năng chưa dùng không tốn chi phí.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Complexity reduction, measured

Reduce cognitive complexity and duplication in a subsystem or in a diff. Every claim is a number from a tool. Every edit is a pure move: behaviour identical, only the shape changes.

Cognitive complexity at or above 15 for one function marks a potential problem, in every language below. It is a threshold to report, not a rule to enforce.

1. Tools

Pick the tool by language. Never report a number you did not measure.

Java: pmd

pmd (Homebrew: brew install pmd). Two of its commands matter.

bash
pmd check -R <skill dir>/pmd-complexity.xml --file-list <list> -f csv -r <out>.csv --no-progress
pmd cpd --minimum-tokens 45 --language java --file-list <list> --format csv

pmd-complexity.xml beside this file enables CognitiveComplexity (report level 15), CyclomaticComplexity and NPathComplexity, and nothing else.

Rust: rust-code-analysis-cli

Install it with cargo. --locked is required: without it, v0.0.25 fails to compile, because the dependency graph resolves two different tree-sitter versions (0.20 expected, 0.27 found).

bash
cargo install --locked rust-code-analysis-cli

Measure, then pull out every function at or above the threshold:

bash
rust-code-analysis-cli -m -p <path> -O json --pr > <out>.json

Scores live in a recursive spaces[] tree, at metrics.cognitive.sum. A flat filter misses nested functions and closures, so recurse:

bash
jq -r '
  def scores($file):
    (if .kind == "function" then
       "\($file):\(.start_line)\t\(.name)\t\(.metrics.cognitive.sum)"
     else empty end),
    (.spaces[]? | scores($file));
  . as $r | $r | scores($r.name)' <out>.json \
  | awk -F'\t' '$3 >= 15'

-p takes a file or a directory. For a list of files, repeat the flag — -p a.rs -p b.rs. A space-separated list after one -p fails, which is exactly the case diff mode hands you. Pass -I '*.rs' to include and -X to exclude by glob.

There is no cpd equivalent here, so duplication in Rust is a read, not a measurement. Say so rather than implying a tool checked it.

Reading the metric

Cognitive Complexity is the SonarSource metric. It charges for nesting depth, so a triply-nested condition costs far more than three flat ones. That is why extraction of a deeply nested region buys more than extraction of a long flat one, and it is why you always attack the deepest nesting first.

cpd finds only near-identical token runs. It cannot see a repeated decision whose branches differ. Read for that yourself; it is usually the more valuable finding.

2. Pick the mode, then define the file set

Two modes. Ask the user which one if the request does not say.

ModeFile setEnds at
Subsystem (default)The files a feature ownsSection 4, unless --refactor
DiffThe files a diff or PR changesSection 4, unless --refactor

The mode picks the file set. --refactor decides whether anything gets edited, and it is the only thing that does.

Diff mode

Score whole changed files, not only the functions the diff touched. A small edit can push a neighbouring function past the threshold, and touched-only scoring hides that. Mark which findings the diff introduced and which were already there, so the author sees what they own.

bash
git diff --name-only --diff-filter=d <base>...HEAD -- '*.java'   # or '*.rs'

Use the PR's merge base as <base>, not HEAD~1: a branch of several commits is one unit of review. --diff-filter=d drops deleted files, which no tool can measure.

Subsystem mode

Ask the user what the subsystem is if it is not obvious. Do not guess, and do not default to the diff.

In a git repo, the reliable way to find the files a feature owns:

bash
git log --format='%H' --grep='<feature>' -i | while read -r c; do
  git show --diff-filter=A --name-only --format='' "$c" -- 'src/**/*.java'
done | sort -u

Beware false positives: an unrelated old commit whose subject contains the same word. Read the commit subjects and drop the ones that do not belong.

Add the files the branch modifies. Then say plainly which files the feature owns and which are pre-existing, because that changes what is safe to touch.

3. Baseline, before any edit

Record all four. Without them you cannot prove the work helped.

  1. The complexity tool: total points, function count at or over threshold, and the per-file breakdown. PMD reports all three directly; for Rust, sum metrics.cognitive.sum yourself.
  2. Duplication: the CPD blocks. Rust has no CPD, so record "not measured" rather than "none".
  3. Tests: the subsystem's tests, run on a clean build, and the passing count.
  4. Any performance guard the project already has, for example an allocation test that prints measured bytes. Record the numbers it prints, not just that it passed.

Read the test verdict from the runner's structured output, never from its exit code. For a JUnit result XML, treat tests="0" as a failure: it means the class never started. For cargo nextest, read the summary line, and treat a zero-test run the same way.

4. Report before you edit

Group the findings. For each, give the location, the measured cost, and the concrete change. Say which findings are in code the feature owns and which are pre-existing.

Do not rank findings by urgency and do not recommend an order of work. State the facts and let the user choose.

Stop here unless the user passed --refactor. Reporting is the default in both modes. Sections 5 through 8 edit code; run them only on --refactor. Never start extracting because a score looks bad, and never treat a bad score as the user asking you to fix it.

5. The constraints that outrank tidiness

Read the subsystem's own documentation first. A hot path usually carries promises that a naive extraction breaks.

  • Allocation-free means allocation-free. If a class documents that it allocates nothing per row or per cell, an extraction may not return a new object. Use a reusable holder field.
  • No extra copying. A buffer swap must stay a reference swap. Never turn an alias into a copy to make a signature nicer.
  • Pure move only. If an extraction changes behaviour, it is not this skill's work. Stop and raise it.
  • Coverage must not shrink. A deleted or skipped test is a finding, not a simplification.

Extraction changes JIT inlining in both directions. Never claim smaller is faster. Measure.

6. Work order

Deepest nesting first. Extract the innermost region before the region containing it, or the outer extraction simply inherits the score.

Re-run the complexity tool after each extraction, not once at the end. Then you can say which extraction bought what, and you notice immediately when one buys nothing.

7. Commit discipline

One commit per kind of change. Duplication removal and complexity extraction are separate commits; their diffs are unreadable when mixed.

Never amend unless the user says to. Never push.

If the repo uses a rebased branch stack, a new commit on a lower branch invalidates every branch above it. Say so, and rebase when the user asks.

8. Verify, then report

Clean build. Run the subsystem's tests. Compare against the Section 3 baseline:

  • test count must not drop
  • performance guard numbers must not regress
  • the complexity total must fall, and no function may rise

Report the before and after side by side. If a number moved the wrong way, say so plainly rather than reporting the ones that improved.

Tệp đi kèm

Mô hình đọc các tệp này khi cần trong lúc kỹ năng được tải. Các tệp được cung cấp ở dạng có thể đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Complexity Reduction làm được gì?

Measure cognitive complexity and duplicate code with PMD (Java) or rust-code-analysis-cli (Rust), then reduce both by pure-move extraction, re-measuring after every step. Runs over a subsystem or over a PR diff. Use this when the user asks to simplify, refactor, clean up, or reduce the complexity of a subsystem, asks "how complex is X", asks to find duplicate code, asks to make code "beautiful", or asks for a complexity check on a diff or PR. It measures before it edits and re-measures after; it never accepts an unmeasured claim that a change helped. It reports only, unless the user passes...

Vì sao nên dùng Complexity Reduction trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Complexity Reduction là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Complexity Reduction trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/rustyrazorblade/skills/tree/main/plugins/dev-skills/skills/complexity-reduction. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm và cài đặt thành một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Complexity Reduction?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Bạn có thể dùng Complexity Reduction với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Complexity Reduction với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Complexity Reduction có miễn phí không?

Có. Kỹ năng này được rustyrazorblade phát hành trên GitHub theo giấy phép Apache-2.0. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp cho mô hình các công cụ mới để gọi — mã được chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và tiêu chí của đầu ra tốt. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇