一键导入
delphi-clean-code
Pragmatic clean code standards for Delphi — concise, direct, no over-engineering
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Pragmatic clean code standards for Delphi — concise, direct, no over-engineering
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Pragmatic clean code standards for Delphi — concise, direct, no over-engineering
Delphi code review checklist — quality, security, performance, SOLID, memory
Good memory management practices, memory leak prevention and exception handling in Delphi
SOLID implementation patterns for Delphi projects — Repository, Service, Factory, Strategy with constructor injection and interfaces
Implementation of the 23 GoF (Gang of Four) patterns in Object Pascal / Delphi with interfaces, TInterfacedObject and SOLID principles. Covers Creational, Structural and Behavioral patterns.
Standards for using DevExpress (DEXT) components in Delphi VCL applications
| name | Delphi Clean Code |
| description | Pragmatic clean code standards for Delphi — concise, direct, no over-engineering |
CRITICAL SKILL — Be concise, direct and solution-focused.
| 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 |
| 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.
| 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 |
| 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 |
// ❌ RUIM — nesting excessivo
procedure ProcessOrder(AOrder: TOrder);
begin
if Assigned(AOrder) then
begin
if AOrder.Items.Count > 0 then
begin
if AOrder.IsValid then
begin
// logic real aqui
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');
// logic real aqui — sem nesting
end;
| ❌ 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 |
// ✅ Objetos temporários — sempre try/finally
LList := TStringList.Create;
try
LList.Add('item');
// usar LList
finally
LList.Free;
end;
// ✅ Interfaces — reference counting automático
var LService: IMyService;
LService := TMyService.Create; // liberado automaticamente
// ✅ Owner pattern para componentes visuais
LButton := TButton.Create(Self); // Self libera automaticamente
| Situation | Action |
|---|---|
| User requests feature | Write directly |
| User reports bug | Correct, don't explain |
| Requirement unclear | Ask, don't assume |
| 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.
| 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.