Delphi Clean Code
Skill53 starsChanged 8 months agoMITView on GitHub
Pragmatic clean code standards for Delphi — concise, direct, no over-engineering
In this repository35 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .claude/CLAUDE.mdCLAUDE.md
- .github/copilot-instructions.mdCopilot instructions
- .cursor/rules/acbr-patterns.mdCursor rule
- .cursor/rules/delphi-conventions.mdCursor rule
- .cursor/rules/design-patterns.mdCursor rule
- .cursor/rules/dext-patterns.mdCursor rule
- .cursor/rules/dmvc-patterns.mdCursor rule
- .cursor/rules/firebird-patterns.mdCursor rule
- .cursor/rules/horse-patterns.mdCursor rule
- .cursor/rules/intraweb-patterns.mdCursor rule
- .cursor/rules/memory-exceptions.mdCursor rule
- .cursor/rules/mysql-patterns.mdCursor rule
- .cursor/rules/postgresql-patterns.mdCursor rule
- .cursor/rules/refactoring.mdCursor rule
- .cursor/rules/solid-patterns.mdCursor rule
- .cursor/rules/tdd-patterns.mdCursor rule
- .cursor/rules/threading-patterns.mdCursor rule
- acbr-componentsSkill
- Delphi Code ReviewSkill
- Delphi Memory and ExceptionsSkill
- Delphi SOLID PatternsSkill
- Design Patterns GoF — DelphiSkill
- DevExpress/DEXT ComponentsSkill
- Dext Framework PatternsSkill
- DelphiMVCFrameworkSkill
- DUnitX Testing PatternsSkill
- Firebird DatabaseSkill
- Horse FrameworkSkill
- Intraweb FrameworkSkill
- MySQL DatabaseSkill
- PostgreSQL DatabaseSkill
- Delphi Code RefactoringSkill
- Delphi Test-Driven Development (TDD) and DUnitXSkill
- Threading & Multi-ThreadingSkill
The file
Files
Delphi Clean Code — Skill
CRITICAL SKILL — Be concise, direct and solution-focused.
Fundamental Principles
| Principle | Rule |
| SRP | A function/class does ONE thing |
| DRY | Don't repeat code — extract and reuse |
| KISS | Simplest solution that works |
| YAGNI | Don't build what wasn't asked for |
| Boy Scout | Leave the code better than you found it |
Naming Rules (Pascal Guide)
| Element | Convention |
| Variables | Reveal intent: LCustomerCount not N |
| Methods | Verb + noun: GetCustomerById not Customer |
| Booleans | Question form: IsActive, HasPermission, CanEdit |
| Constants | SCREAMING_SNAKE: MAX_RETRY_COUNT |
| Fields | Prefix F: FCustomerName |
| Parameters | Prefix A: ACustomerName |
| Var. locations | Prefix L: LCustomer |
Rule: If you need a comment to explain a name, rename it.
Method Rules
| Rule | Description |
| Short | Maximum 20 lines, ideal 5-10 |
| One Thing | Do one thing and do it well |
| One Level | One level of abstraction per method |
| Few Args | Maximum 3 arguments, prefer 0-2 |
| No Side Effects | Don't mute inputs unexpectedly |
Code Structure
| Standard | Application |
| Guard Clauses | Early returns for edge cases |
| Flat > Nested | Avoid deep nesting (max 2 levels) |
| Composition | Small compound methods |
| Colocation | Related code together |
Guard Clauses in Delphi
//❌ BAD — excessive nesting
procedure ProcessOrder(AOrder: TOrder);
begin
if Assigned(AOrder) then
begin
if AOrder.Items.Count > 0 then
begin
if AOrder.IsValid then
begin
//real logic here
end;
end;
end;
end;
//✅ BOM — guard clauses
procedure ProcessOrder(AOrder: TOrder);
begin
if not Assigned(AOrder) then
raise EArgumentNilException.Create('AOrder cannot be nil');
if AOrder.Items.Count = 0 then
raise EBusinessRuleException.Create('Order must have items');
if not AOrder.IsValid then
raise EValidationException.Create('Order validation failed');
//real logic here — no nesting
end;Anti-Patterns (DO NOT DO)
| ❌ Pattern | ✅ Fix |
| Comment each line | Delete obvious comments |
| Method > 20 lines | Share by responsibility |
| Magic numbers | Named constants |
with statement | Explicit local variables |
| Global variables | Constructor injection |
| Generic Catch | Specific exceptions |
Logic in OnClick | Delegate to Service |
| God class / God unity | One class = one responsibility |
Ignore Free | try/finally always |
Memory Management
//✅ Temporary objects — always try/finally
LList := TStringList.Create;
try
LList.Add('item');
//use LList
finally
LList.Free;
end;
//✅ Interfaces — reference counting automático
var LService: IMyService;
LService := TMyService.Create; //automatically released
//✅ Owner pattern for visual components
LButton := TButton.Create(Self); //Self frees automaticallyAI Code Style
| Situation | Action |
| User requests feature | Write directly |
| User reports bug | Correct, don't explain |
| Requirement unclear | Ask, don't assume |
🔴 Before Editing (THINK FIRST!)
| Question | Why |
| Which units use this? | They can break |
| What does this unit matter? | Interfaces can change |
| What tests cover this? | Tests may fail |
| Is it a shared component? | Multiple points affected |
🔴 Rule: Edit the file + all dependents in the SAME task.
🔴 Self-Check (MANDATORY)
| Check | Pergunta |
| ✅ Goal achieved? | Did I do exactly what was asked? |
| ✅ Edited files? | Have I modified everything necessary? |
| ✅ Does the code work? | Have I tested/verified? |
| ✅ No errors? | Compiles without warnings? |
| ✅ Nothing forgotten? | Edge cases treated? |
| ✅ Memory safe? | Objects released correctly? |
🔴 Rule: If ANY check fails, correct it before finishing.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

