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.

Delphi code refactoring — techniques to improve readability, remove code smells and apply patterns without changing behavior.

Cursor rule for Cursor

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.

agentmods
npx agentmods add rules/secondlifes/delphi-expert/refactoring
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.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.svg" alt="Measured on agentmods" 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. Scan, not verified.
Origin unknown 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 $0.00000 $0.02818
Opus 5 $0.00000 $0.01409
Sonnet 5 $0.00000 $0.00564
Haiku 4.5 $0.00000 $0.00282

Measured today against content hash ebee58831726, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, 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 today.

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. today 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 2d 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.