{"id":28346829,"url":"https://github.com/mestackcodes/cleancodedelphi","last_synced_at":"2026-02-09T00:01:49.986Z","repository":{"id":294566442,"uuid":"987387126","full_name":"MEStackCodes/CleanCodeDelphi","owner":"MEStackCodes","description":"A short guide will review some key concepts for clean code with Delphi.","archived":false,"fork":false,"pushed_at":"2025-05-21T02:21:09.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-01T12:10:49.201Z","etag":null,"topics":["clean-code","code","delphi","object-pascal"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MEStackCodes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-21T02:19:23.000Z","updated_at":"2025-05-21T10:12:24.000Z","dependencies_parsed_at":"2025-05-21T03:39:27.678Z","dependency_job_id":null,"html_url":"https://github.com/MEStackCodes/CleanCodeDelphi","commit_stats":null,"previous_names":["mestackcodes/cleancodedelphi"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MEStackCodes/CleanCodeDelphi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MEStackCodes%2FCleanCodeDelphi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MEStackCodes%2FCleanCodeDelphi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MEStackCodes%2FCleanCodeDelphi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MEStackCodes%2FCleanCodeDelphi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MEStackCodes","download_url":"https://codeload.github.com/MEStackCodes/CleanCodeDelphi/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MEStackCodes%2FCleanCodeDelphi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29250180,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-08T22:49:53.206Z","status":"ssl_error","status_checked_at":"2026-02-08T22:49:51.384Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clean-code","code","delphi","object-pascal"],"created_at":"2025-05-27T15:10:58.797Z","updated_at":"2026-02-09T00:01:49.944Z","avatar_url":"https://github.com/MEStackCodes.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Clean Code with Delphi (Short Guide)\n\n**Clean Code**, describes the practice of writing code that is readily readable, understandable, maintainable, and modifiable. It goes beyond simply adhering to style conventions and includes applying design principles that promote long-term code comprehension and maintainability. This short guide will review some key concepts.\n\n## Efficient Naming\nMeaningful names are at the heart of clean code. In Object Pascal (Delphi):\n- **InfixCaps**: Use InfixCaps naming convention.\n- **Variables:** Use intention-revealing names (`UserAge` instead of `a, Tmp, vName`).\n- **Methods:** Names should clearly express purpose (`CalculateTotalPrice`).\n- **Properties:** Name properties descriptively to reflect their purpose (`CustomerName` rather than `CN`)\n- **Type Prefix**: Prefix all classes and custom types with a `T letter` (`TCustomer`), for exception classes `E letter` (`ECustomerType`) and interface types `I letter` (`ICustomerResponse`).\n- **Classes:** Avoid ambiguity by using nouns (`InvoiceProcessor`).\n- **Constants:** Avoid underscores \u0026 symbols (`DefaultCustomerAge: Integer = 18`).\n- **Reserved Words**: Language keywords, reserved words, and compiler directives must all be lowercase.\n- **Use English**: Always name variables, methods, and classes in English.\n\n## Functions/Methods/Properties Declaration\nSmall, focused methods enhance clarity. Follow these principles:\n- **Single Responsibility:** Each method should handle only one task.\n- **Avoid Side Effects:** Predictable methods yield cleaner interactions.\n- **Avoid direct field access:** Use properties to enforce validation, computed values, or controlled modification.\n- **Define read-only properties:** When values shouldn’t be modified after initialization.\n\n## Comments\nComments are powerful but should be used sparingly:\n- **Avoid Redundancy:** Don’t comment on obvious code.\n- **Explain Intent:** Focus on why something is done, not what is done.\n- **TODO Notes:** Document areas requiring improvement.\n\n### Example:\n```pascal\n// Adjusts prices based on inflation rate\nprocedure UpdatePrices;\nbegin\n  // Complex business logic here\nend;\n```\n\n## Formatting\nConsistency and readability are crucial:\n- **Indentation:** Standardize across the team.\n- **Line Breaks:** Use them generously to enhance readability.\n- **Block Alignment:** Ensure clarity in nested structures.\n\n### Example:\n```pascal\nif Condition then\nbegin\n  ExecuteAction;\nend\nelse\nbegin\n  HandleError;\nend;\n```\n\n## Class, Objects and Data Structures\nCohesive classes and proper encapsulation are essential:\n- **Encapsulation:** Use public, private or protected access modifiers to hide implementation details.\n- **Avoid Large Classes:** Break down large classes into smaller, specialized ones to maintain focus and simplicity.\n- **Constructor Use:** Initialize fields properly.\n- **Use Destructor to Free Memory:** Always pair the constructor with a destructor to clean up resources and manage memory efficiently.\n\n\n### Example:\n```pascal\ntype\n  TCustomer = class\n  private\n    FName: string;\n    procedure SetName(Value: string);\n  public\n    constructor Create(Name: string);\n    destructor Destroy; override;\n    property Name: string read FName write SetName;\n  end;\n\nconstructor TCustomer.Create(Name: string);\nbegin\n  FName := Name;\nend;\n\ndestructor TCustomer.Destroy;\nbegin\n  inherited;\nend;\n\nprocedure TCustomer.SetName(Value: string);\nbegin\n  if Value \u003c\u003e '' then\n  begin\n   FName := Value\n  end;  \nend;\n\n```\n\n## Error Handling\nClean error handling safeguards code integrity:\n- **Check Resource Availability**: Always verify whether a resource exists or is available before trying to use it. This can prevent exceptions and ensure that the program continues running smoothly.\n- **Exceptions:** Use `try...except or finally` blocks effectively.\n- **Meaningful Messages:** Provide context in error messages.\n- **Avoid Silent Errors:** Always report issues explicitly.\n\n### Example:\n```pascal\nvar\n  Customer: TCustomer;\n\nbegin\n  try\n    if Assigned(Customer) then\n      ShowMessage('Customer Name: ' + Customer.Name)\n    else\n      raise Exception.Create('Error: Customer object is not initialized.');\n  except\n    on E: Exception do\n    begin\n      ShowMessage('Exception Caught: ' + E.Message);\n    end;\nend;\n```\n\n## FireMonkey / Multiplatform Application\nFireMonkey (FMX) is Delphi's cross-platform framework, enabling the development of applications for Windows, macOS, iOS, Android, and more. Here are key recommendations:\n- **Platform-Specific Code:** Leverage Delphi’s compiler directives to conditionally compile code for different platforms. This ensures clarity and avoids unnecessary clutter in shared units.\n- **Separate Units for Platform:** Organize platform-specific implementations into separate units. Use naming conventions such as `UnitName.Android.pas, UnitName.iOS.pas, etc.`, for clarity and consistency.\n- **Optimize Performance for Mobile:** Mobile platforms are more resource-constrained compared to desktops. Use Platform Services and Interfaces.\n\n### Example:\n```pascal\nprocedure PlatformSpecificFeature;\nbegin\n  {$IFDEF MSWINDOWS}\n  ShowMessage('Windows-specific code here');\n  {$ENDIF}\n\n  {$IFDEF ANDROID}\n  ShowMessage('Android-specific code here');\n  {$ENDIF}\nend;\n```\n\n## Testing\nUnit testing is a cornerstone of clean code:\n- **Test Coverage:** Aim to cover critical functionality.\n- **Readable Tests:** Tests should be as clean as the code they verify.\n- **Automate:** Integrate tests into your build process.\n\n### Example:\n```pascal\nprocedure TestCalculateTotalPrice;\nbegin\n  Assert.AreEqual(50, FCalculator.CalculateTotalPrice(5, 10), 'The price calculation is incorrect');\nend;\n```\n\n## Share\nIf you liked and found this repository useful for your projects, star it. Thank you for your support! ⭐\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmestackcodes%2Fcleancodedelphi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmestackcodes%2Fcleancodedelphi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmestackcodes%2Fcleancodedelphi/lists"}