{"id":28726382,"url":"https://github.com/0xm4ze/clean-code-cpp","last_synced_at":"2025-06-15T12:37:57.339Z","repository":{"id":269104190,"uuid":"906436293","full_name":"0xm4ze/clean-code-cpp","owner":"0xm4ze","description":"📝 Clean Code concepts adapted for c++","archived":false,"fork":false,"pushed_at":"2024-12-20T23:05:42.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-16T04:34:31.606Z","etag":null,"topics":["bestpractices","clean-architecture","clean-code","cplusplus","cpp","encapsulation","inheritance","principles"],"latest_commit_sha":null,"homepage":"","language":"C++","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/0xm4ze.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":"2024-12-20T22:57:46.000Z","updated_at":"2024-12-20T23:06:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"634fd154-47ff-4194-8123-e141c6c62cf9","html_url":"https://github.com/0xm4ze/clean-code-cpp","commit_stats":null,"previous_names":["tal7aouy/clean-code-cpp","0xm4ze/clean-code-cpp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/0xm4ze/clean-code-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xm4ze%2Fclean-code-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xm4ze%2Fclean-code-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xm4ze%2Fclean-code-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xm4ze%2Fclean-code-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0xm4ze","download_url":"https://codeload.github.com/0xm4ze/clean-code-cpp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xm4ze%2Fclean-code-cpp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259977531,"owners_count":22941114,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["bestpractices","clean-architecture","clean-code","cplusplus","cpp","encapsulation","inheritance","principles"],"created_at":"2025-06-15T12:37:55.729Z","updated_at":"2025-06-15T12:37:57.323Z","avatar_url":"https://github.com/0xm4ze.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Clean Code in C++\n\n## Table of Contents\n\n1. [Introduction](#introduction)\n2. [Variables](#variables)\n3. [Functions](#functions)\n4. [Classes](#classes)\n5. [SOLID Principles](#solid-principles)\n6. [Testing](#testing)\n7. [Error Handling](#error-handling)\n8. [Formatting](#formatting)\n9. [Comments](#comments)\n10. [Translation](#translation)\n\n## Introduction\n\n![Humorous image of software quality estimation as a count of how many expletives you shout when reading code](https://www.osnews.com/images/comics/wtfm.jpg)\n\nThis guide presents software engineering principles adapted from Robert C. Martin's book [_Clean Code_](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882), specifically tailored for C++. It is not a style guide; rather, it serves as a roadmap for producing [readable, reusable, and maintainable](https://github.com/ryanmcdermott/3rs-of-software-architecture) software in C++.\n\nNot every principle outlined here must be strictly adhered to, and many may not be universally agreed upon. These are guidelines based on years of collective experience by the authors of _Clean Code_.\n\nOur craft of software engineering is relatively young, just over 50 years old, and we are continuously evolving. When software architecture matures as much as traditional architecture, perhaps we will have more rigid rules to follow. Until then, let these guidelines be a touchstone for assessing the quality of the C++ code you and your team produce.\n\nOne more thing: understanding these principles won't instantly transform you into a better software developer, and even years of practice won't eliminate mistakes. Every piece of code begins as a rough draft, akin to wet clay waiting to be shaped into its final form. We refine it through peer review, chipping away at imperfections. Instead of criticizing your first drafts, focus on improving the code itself!\n\n## **Variables**\n\n### Use meaningful and pronounceable variable names\n\n**❌ Bad:**\n```cpp\nint a; // What does 'a' represent?\n```\n\n**✅ Good:**\n```cpp\nint age; // Clearly indicates the variable's purpose.\n```\n\n### Use the same vocabulary for the same type of variable\n\n**❌ Bad:**\n```cpp\ngetUserInfo();\ngetClientData();\ngetCustomerRecord();\n```\n\n**✅ Good:**\n```cpp\ngetUser(); // Consistent naming improves readability.\n```\n\n### Avoid Mental Mapping\n\n**❌ Bad:**\n```cpp\nstd::vector\u003cstd::string\u003e cities = {\"Austin\", \"New York\", \"San Francisco\"};\nfor (auto c : cities) {\n    // Wait, what is `c` for again?\n    processCity(c);\n}\n```\n\n**✅ Good:**\n```cpp\nstd::vector\u003cstd::string\u003e cities = {\"Austin\", \"New York\", \"San Francisco\"};\nfor (auto city : cities) {\n    processCity(city); // Clearer context for the variable.\n}\n```\n\n### Use constants for magic numbers\n\n**❌ Bad:**\n```cpp\ndouble area = 3.14 * radius * radius; // What does 3.14 represent?\n```\n\n**✅ Good:**\n```cpp\nconst double PI = 3.14;\ndouble area = PI * radius * radius; // Clearer meaning.\n```\n\n## **Functions**\n\n### Function arguments (2 or fewer ideally)\n\n**❌ Bad:**\n```cpp\nvoid createMenu(std::string title, std::string body, std::string buttonText, bool cancellable);\n```\n\n**✅ Good:**\n```cpp\nstruct MenuConfig {\n    std::string title;\n    std::string body;\n    std::string buttonText;\n    bool cancellable;\n};\n\nvoid createMenu(const MenuConfig\u0026 config);\n```\n\n### Functions should do one thing\n\n**❌ Bad:**\n```cpp\nvoid emailClients(std::vector\u003cClient\u003e\u0026 clients) {\n    for (auto\u0026 client : clients) {\n        if (client.isActive()) {\n            email(client);\n        }\n    }\n}\n```\n\n**✅ Good:**\n```cpp\nvoid emailActiveClients(std::vector\u003cClient\u003e\u0026 clients) {\n    for (const auto\u0026 client : clients) {\n        if (isActiveClient(client)) {\n            email(client);\n        }\n    }\n}\n\nbool isActiveClient(const Client\u0026 client) {\n    return client.isActive();\n}\n```\n\n### Function names should clearly indicate what they do\n\n**❌ Bad:**\n```cpp\nvoid doStuff(int x); // What does this function do?\n```\n\n**✅ Good:**\n```cpp\nvoid calculateInterest(double principal, double rate, int time); // Clear purpose.\n```\n\n### Avoid side effects\n\n**❌ Bad:**\n```cpp\nint count = 0;\n\nvoid incrementCount() {\n    count++; // Modifies global state\n}\n```\n\n**✅ Good:**\n```cpp\nint increment(int count) {\n    return count + 1; // Pure function, no side effects.\n}\n```\n\n## **Classes**\n\n### Prefer using C++11/14/17 features\n\n**❌ Bad:**\n```cpp\nclass Animal {\npublic:\n    void move();\n};\n```\n\n**✅ Good:**\n```cpp\nclass Animal {\npublic:\n    virtual void move() = 0; // Pure virtual function\n};\n```\n\n### Use method chaining\n\n**❌ Bad:**\n```cpp\nclass Car {\npublic:\n    void setMake(std::string make);\n    void setModel(std::string model);\n    void setColor(std::string color);\n};\n```\n\n**✅ Good:**\n```cpp\nclass Car {\npublic:\n    Car\u0026 setMake(std::string make);\n    Car\u0026 setModel(std::string model);\n    Car\u0026 setColor(std::string color);\n};\n\nCar myCar;\nmyCar.setMake(\"Ford\").setModel(\"F-150\").setColor(\"red\");\n```\n\n### Prefer composition over inheritance\n\n**❌ Bad:**\n```cpp\nclass Engine {\npublic:\n    void start();\n};\n\nclass Car : public Engine {\npublic:\n    void drive();\n};\n```\n\n**✅ Good:**\n```cpp\nclass Engine {\npublic:\n    void start();\n};\n\nclass Car {\nprivate:\n    Engine engine; // Composition\npublic:\n    void drive();\n};\n```\n\n## **SOLID Principles**\n\n### Single Responsibility Principle (SRP)\n\n**❌ Bad:**\n```cpp\nclass UserSettings {\npublic:\n    void changeSettings();\n    void verifyCredentials();\n};\n```\n\n**✅ Good:**\n```cpp\nclass UserAuth {\npublic:\n    void verifyCredentials();\n};\n\nclass UserSettings {\npublic:\n    void changeSettings(UserAuth\u0026 auth);\n};\n```\n\n### Open/Closed Principle (OCP)\n\n**❌ Bad:**\n```cpp\nclass Shape {\npublic:\n    virtual double area();\n};\n\nclass Rectangle : public Shape {\npublic:\n    double area() { return width * height; }\n};\n```\n\n**✅ Good:**\n```cpp\nclass Shape {\npublic:\n    virtual double area() const = 0; // Abstract\n};\n\nclass Rectangle : public Shape {\npublic:\n    double area() const override { return width * height; }\n};\n```\n\n### Liskov Substitution Principle (LSP)\n\n**❌ Bad:**\n```cpp\nclass Rectangle {\npublic:\n    void setWidth(int width);\n    void setHeight(int height);\n};\n\nclass Square : public Rectangle {\npublic:\n    void setWidth(int width) { Rectangle::setWidth(width); setHeight(width); }\n    void setHeight(int height) { Rectangle::setHeight(height); setWidth(height); }\n};\n```\n\n**✅ Good:**\n```cpp\nclass Shape {\npublic:\n    virtual double area() const = 0;\n};\n\nclass Rectangle : public Shape {\npublic:\n    Rectangle(int width, int height) : width(width), height(height) {}\n    double area() const override { return width * height; }\nprivate:\n    int width, height;\n};\n\nclass Square : public Shape {\npublic:\n    Square(int length) : length(length) {}\n    double area() const override { return length * length; }\nprivate:\n    int length;\n};\n```\n\n## **Testing**\n\nTesting is crucial for maintaining code quality. Aim for high coverage and write tests for each new feature.\n\n### Single concept per test\n\n**❌ Bad:**\n```cpp\nTEST(MomentTest, HandlesDateBoundaries) {\n    // Test multiple concepts in one test case\n}\n```\n\n**✅ Good:**\n```cpp\nTEST(MomentTest, Handles30DayMonths) {\n    // Test one concept\n}\n\nTEST(MomentTest, HandlesLeapYear) {\n    // Test another concept\n}\n```\n\n## **Error Handling**\n\n### Don't ignore caught errors\n\n**❌ Bad:**\n```cpp\ntry {\n    riskyFunction();\n} catch (const std::exception\u0026 e) {\n    // Ignore error\n}\n```\n\n**✅ Good:**\n```cpp\ntry {\n    riskyFunction();\n} catch (const std::exception\u0026 e) {\n    std::cerr \u003c\u003c \"Error: \" \u003c\u003c e.what() \u003c\u003c std::endl;\n}\n```\n\n### Don't ignore rejected promises (if using async)\n\n**❌ Bad:**\n```cpp\nasyncFunction()\n    .then(result =\u003e {\n        // Process result\n    })\n    .catch(error =\u003e {\n        // Ignore error\n    });\n```\n\n**✅ Good:**\n```cpp\nasyncFunction()\n    .then(result =\u003e {\n        // Process result\n    })\n    .catch(error =\u003e {\n        std::cerr \u003c\u003c \"Error: \" \u003c\u003c error.message() \u003c\u003c std::endl;\n    });\n```\n\n## **Formatting**\n\n### Use consistent naming conventions\n\n**❌ Bad:**\n```cpp\nint DaysInWeek = 7;\n```\n\n**✅ Good:**\n```cpp\nconst int daysInWeek = 7;\n```\n\n### Keep function callers and callees close\n\n**❌ Bad:**\n```cpp\nclass PerformanceReview {\npublic:\n    void perfReview();\n    void getPeerReviews();\n    void lookupPeers();\n};\n```\n\n**✅ Good:**\n```cpp\nclass PerformanceReview {\npublic:\n    void perfReview() {\n        getPeerReviews();\n        // Additional logic...\n    }\n\n    void getPeerReviews() {\n        const auto peers = lookupPeers();\n        // Process peers...\n    }\n\n    auto lookupPeers() {\n        // Lookup logic...\n    }\n};\n```\n\n## **Comments**\n\n### Only comment complex business logic\n\n**❌ Bad:**\n```cpp\nvoid hashIt(std::string data) {\n    // The hash\n    int hash = 0;\n    // Length of string\n    int length = data.length();\n    // Loop through every character in data\n    for (int i = 0; i \u003c length; i++) {\n        // Get character code.\n        int charCode = data[i];\n        // Make the hash\n        hash = (hash \u003c\u003c 5) - hash + charCode;\n        // Convert to 32-bit integer\n        hash \u0026= hash;\n    }\n}\n```\n\n**✅ Good:**\n```cpp\nvoid hashIt(std::string data) {\n    int hash = 0;\n    int length = data.length();\n\n    for (int i = 0; i \u003c length; i++) {\n        int charCode = data[i];\n        hash = (hash \u003c\u003c 5) - hash + charCode; // Update hash with character code\n    }\n}\n```\n\n### Avoid leaving commented-out code\n\n**❌ Bad:**\n```cpp\n// void unusedFunction() {}\n```\n\n**✅ Good:**\n```cpp\n// Function removed; see version history.\n```\n\n### Avoid journal comments\n\n**❌ Bad:**\n```cpp\nvoid combine(int a, int b) {\n    return a + b;\n}\n```\n\n**✅ Good:**\n```cpp\nvoid combine(int a, int b) {\n    return a + b; // Implementation updated for clarity\n}\n```\n\n### Avoid positional markers\n\n**❌ Bad:**\n```cpp\n$scope.model = {\n    menu: \"foo\",\n    nav: \"bar\"\n};\n```\n\n**✅ Good:**\n```cpp\n$scope.model = {\n    menu: \"foo\",\n    nav: \"bar\"\n}; // Instantiation of model\n```\n\n## **Translation**\n\nThis guide can be translated into other languages. Contributions are welcome!\n\n---\n\nBy following these guidelines, you can write clean, maintainable, and efficient C++ code. Remember, the goal is not only to write code that works but to write code that others can read and understand.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xm4ze%2Fclean-code-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0xm4ze%2Fclean-code-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xm4ze%2Fclean-code-cpp/lists"}