refactoring

refactoring is a cursor rule for Cursor from SecondLifes/delphi-expert. It costs 0 tokens per session (2,818 once invoked), scanned A, original, MIT.

A set of Delphi refactoring rules for improving code structure without changing what the program does. It lists common code smells and matching techniques such as extracting methods or classes.

In plain words
What is it for?
Use it when reviewing or restructuring Object Pascal code. It helps identify code smells and choose focused changes for methods, classes, parameters, conditionals, and UI logic.
Why use it?
It helps remove difficult-to-read, duplicated, deeply nested, or overly responsible code while preserving behavior. It also points out business logic that has leaked into user-interface handlers.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit Use it when reviewing or restructuring Object Pascal code. It helps identify code smells and choose focused changes for methods, classes, parameters, conditionals, and UI logic.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/secondlifes/delphi-expert/refactoring
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/SecondLifes/delphi-expert

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for refactoring

README.md
[![agentmods](https://agentmods.dev/badge/rules/secondlifes/delphi-expert/refactoring/github.svg)](https://agentmods.dev/rules/secondlifes/delphi-expert/refactoring)
Your own site
<a href="https://agentmods.dev/rules/secondlifes/delphi-expert/refactoring"><img src="https://agentmods.dev/badge/rules/secondlifes/delphi-expert/refactoring/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for refactoring

Your own site · 80×15
<a href="https://agentmods.dev/rules/secondlifes/delphi-expert/refactoring"><img src="https://agentmods.dev/badge/rules/secondlifes/delphi-expert/refactoring.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 2,818 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00000 $0.02818
Opus 5.5 $0.00000 $0.01127
Sonnet 5.5 $0.00000 $0.00564
Haiku 4.5 $0.00000 $0.00282

Measured 1mo ago against content hash ebee58831726, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

refactoring scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 1mo ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/refactoring.mdc · 406 lines

How it starts

The opening of the file, as written. The whole thing — 406 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Delphi Code Refactoring — Rules

Use these rules when detecting or fixing code smells in Object Pascal. Refactoring doesn't change behavior — it just improves the internal structure.


🔴 Code Smells that Require Immediate Refactoring

Code Smell Symptom Technique
Long method > 20 lines Extract Method
Bloated class > 300 lines / multiple responsibilities Extract Class
Magic numbers if Age > 18 Replace with Constant
Deep Nesting if...if...if above 2 levels Replace with Guard Clauses
Duplicate code Same block in ≥ 2 places Extract Method / Pull Up
Excessive parameters Method with > 3 parameters Introduce Parameter Object
Mixed UI logic Business in OnClick Move Method to Service
with statement with DataSet do... Remove With
Unnecessary temporary variable Used only once Inline Temp
Conditional with type if Obj is TSubClass then Replace Conditional with Polymorphism
Message chain A.B.C.D.Execute Law of Demeter / Introduce Method
Comment explaining what The code should explain itself Rename + Extract Method

✂️ Extract Method

Extract purposeful blocks of code into named methods.

//❌ BEFORE — long method without separation of responsibilities
procedure TOrderService.PlaceOrder(AOrder: TOrder);
var
  LTotal: Currency;
  LDiscount: Currency;
  LItem: TOrderItem;
begin
  // Calcula total
  LTotal := 0;
  for LItem in AOrder.Items do
    LTotal := LTotal + (LItem.UnitPrice * LItem.Quantity);

  //Apply discount
  if AOrder.Customer.IsVip then
    LDiscount := LTotal * 0.10
  else if LTotal > 500 then
    LDiscount := LTotal * 0.05
  else
    LDiscount := 0;

  LTotal := LTotal - LDiscount;

  //Validates stock
  for LItem in AOrder.Items do
  begin
    if LItem.Quantity > LItem.Product.StockQty then
      raise EInvalidOrderException.CreateFmt(
        'Estoque insuficiente: %s', [LItem.Product.Name]);
  end;

  AOrder.TotalAmount := LTotal;
  FRepository.Save(AOrder);
end;

//✅ AFTER — each responsibility in its own method
procedure TOrderService.PlaceOrder(AOrder: TOrder);
begin
  ValidateStock(AOrder);
  AOrder.TotalAmount := CalculateTotalWithDiscount(AOrder);
  FRepository.Save(AOrder);
end;

function TOrderService.CalculateSubtotal(AOrder: TOrder): Currency;
var LItem: TOrderItem;
begin
  Result := 0;
  for LItem in AOrder.Items do
    Result := Result + (LItem.UnitPrice * LItem.Quantity);
end;

function TOrderService.CalculateDiscount(AOrder: TOrder; ASubtotal: Currency): Currency;
const
  VIP_DISCOUNT_RATE     = 0.10;
  BULK_DISCOUNT_RATE    = 0.05;
  BULK_DISCOUNT_MINIMUM = 500;
begin
  if AOrder.Customer.IsVip then
    Result := ASubtotal * VIP_DISCOUNT_RATE
  else if ASubtotal > BULK_DISCOUNT_MINIMUM then
    Result := ASubtotal * BULK_DISCOUNT_RATE
  else
    Result := 0;
end;

function TOrderService.CalculateTotalWithDiscount(AOrder: TOrder): Currency;
var LSubtotal: Currency;
begin
  LSubtotal := CalculateSubtotal(AOrder);
  Result := LSubtotal - CalculateDiscount(AOrder, LSubtotal);
end;

procedure TOrderService.ValidateStock(AOrder: TOrder);
var LItem: TOrderItem;
begin
  for LItem in AOrder.Items do
    if LItem.Quantity > LItem.Product.StockQty then
      raise EInvalidOrderException.CreateFmt(
        'Estoque insuficiente: %s', [LItem.Product.Name]);
end;

Read the full file on GitHub · 406 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 1mo ago First seen · 406 lines · 0 tokens per session scan A ebee58831726

Subscribe to this mod's changes

refactoring is a cursor rule published in the GitHub repository SecondLifes/delphi-expert (3 stars, last pushed 1mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,818 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.