{"id":26689399,"url":"https://github.com/aliezzahn/clean-code-principles","last_synced_at":"2026-02-23T18:37:05.801Z","repository":{"id":282176961,"uuid":"947726659","full_name":"aliezzahn/clean-code-principles","owner":"aliezzahn","description":"Writing clean code is essential for creating maintainable, readable, and professional software.","archived":false,"fork":false,"pushed_at":"2025-03-13T06:42:30.000Z","size":4,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-27T04:35:05.613Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aliezzahn.png","metadata":{"files":{"readme":"README.mdx","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2025-03-13T06:38:21.000Z","updated_at":"2025-03-20T14:58:44.000Z","dependencies_parsed_at":"2025-03-13T07:46:31.778Z","dependency_job_id":null,"html_url":"https://github.com/aliezzahn/clean-code-principles","commit_stats":null,"previous_names":["aliezzahn/clean-code-principles"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/aliezzahn/clean-code-principles","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliezzahn%2Fclean-code-principles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliezzahn%2Fclean-code-principles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliezzahn%2Fclean-code-principles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliezzahn%2Fclean-code-principles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aliezzahn","download_url":"https://codeload.github.com/aliezzahn/clean-code-principles/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aliezzahn%2Fclean-code-principles/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29750726,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T07:44:07.782Z","status":"ssl_error","status_checked_at":"2026-02-23T07:44:07.432Z","response_time":90,"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":[],"created_at":"2025-03-26T14:34:35.024Z","updated_at":"2026-02-23T18:37:05.769Z","avatar_url":"https://github.com/aliezzahn.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# 5 Clean Code Principles Every Software Engineer Should Know\n\nWriting clean code is essential for creating maintainable, readable, and professional software. Whether you're preparing for a coding interview or working on a large-scale project, adhering to clean code principles can make a significant difference. In this blog post, we'll explore **five clean code principles** with practical examples in **C**, **C++**, **Rust**, **Java**, **PHP**, **JavaScript**, and **Python**.\n\n---\n\n## 1. Reduce Unnecessary Nesting\n\nDeeply nested code is hard to read and maintain. Flatten your code by using early exits (`continue`, `return`, or `break`) to simplify logic.\n\n### **Example**\n\n#### C\n\n```c\n// Bad: Nested if statements\nif (condition1) {\n    if (condition2) {\n        if (condition3) {\n            do_something();\n        }\n    }\n}\n\n// Good: Flattened with early exit\nif (!condition1) return;\nif (!condition2) return;\nif (!condition3) return;\ndo_something();\n```\n\n#### C++\n\n```cpp\n// Bad: Nested if statements\nif (condition1) {\n    if (condition2) {\n        if (condition3) {\n            doSomething();\n        }\n    }\n}\n\n// Good: Flattened with early exit\nif (!condition1) return;\nif (!condition2) return;\nif (!condition3) return;\ndoSomething();\n```\n\n#### Rust\n\n```rust\n// Bad: Nested if statements\nif condition1 {\n    if condition2 {\n        if condition3 {\n            do_something();\n        }\n    }\n}\n\n// Good: Flattened with early exit\nif !condition1 { return; }\nif !condition2 { return; }\nif !condition3 { return; }\ndo_something();\n```\n\n#### Java\n\n```java\n// Bad: Nested if statements\nif (condition1) {\n    if (condition2) {\n        if (condition3) {\n            doSomething();\n        }\n    }\n}\n\n// Good: Flattened with early exit\nif (!condition1) return;\nif (!condition2) return;\nif (!condition3) return;\ndoSomething();\n```\n\n#### PHP\n\n```php\n// Bad: Nested if statements\nif ($condition1) {\n    if ($condition2) {\n        if ($condition3) {\n            doSomething();\n        }\n    }\n}\n\n// Good: Flattened with early exit\nif (!$condition1) return;\nif (!$condition2) return;\nif (!$condition3) return;\ndoSomething();\n```\n\n#### JavaScript\n\n```javascript\n// Bad: Nested if statements\nif (condition1) {\n  if (condition2) {\n    if (condition3) {\n      doSomething();\n    }\n  }\n}\n\n// Good: Flattened with early exit\nif (!condition1) return;\nif (!condition2) return;\nif (!condition3) return;\ndoSomething();\n```\n\n#### Python\n\n```python\n# Bad: Nested if statements\nif condition1:\n    if condition2:\n        if condition3:\n            do_something()\n\n# Good: Flattened with early exit\nif not condition1:\n    return\nif not condition2:\n    return\nif not condition3:\n    return\ndo_something()\n```\n\n---\n\n## 2. Use Descriptive Variable Names\n\nClear and descriptive variable names make your code self-explanatory. Avoid ambiguous names like `i`, `temp`, or `x`.\n\n### **Example**\n\n#### C\n\n```c\n// Bad: Ambiguous variable names\nint x = 10;\nint y = 5;\nint z = x + y;\n\n// Good: Descriptive variable names\nint total_price = 10;\nint discount = 5;\nint final_price = total_price - discount;\n```\n\n#### C++\n\n```cpp\n// Bad: Ambiguous variable names\nint x = 10;\nint y = 5;\nint z = x + y;\n\n// Good: Descriptive variable names\nint totalPrice = 10;\nint discount = 5;\nint finalPrice = totalPrice - discount;\n```\n\n#### Rust\n\n```rust\n// Bad: Ambiguous variable names\nlet x = 10;\nlet y = 5;\nlet z = x + y;\n\n// Good: Descriptive variable names\nlet total_price = 10;\nlet discount = 5;\nlet final_price = total_price - discount;\n```\n\n#### Java\n\n```java\n// Bad: Ambiguous variable names\nint x = 10;\nint y = 5;\nint z = x + y;\n\n// Good: Descriptive variable names\nint totalPrice = 10;\nint discount = 5;\nint finalPrice = totalPrice - discount;\n```\n\n#### PHP\n\n```php\n// Bad: Ambiguous variable names\n$x = 10;\n$y = 5;\n$z = $x + $y;\n\n// Good: Descriptive variable names\n$total_price = 10;\n$discount = 5;\n$final_price = $total_price - $discount;\n```\n\n#### JavaScript\n\n```javascript\n// Bad: Ambiguous variable names\nlet x = 10;\nlet y = 5;\nlet z = x + y;\n\n// Good: Descriptive variable names\nlet totalPrice = 10;\nlet discount = 5;\nlet finalPrice = totalPrice - discount;\n```\n\n#### Python\n\n```python\n# Bad: Ambiguous variable names\nx = 10\ny = 5\nz = x + y\n\n# Good: Descriptive variable names\ntotal_price = 10\ndiscount = 5\nfinal_price = total_price - discount\n```\n\n---\n\n## 3. Single Responsibility Principle (SRP)\n\nEach function or class should have a single responsibility. Break down large functions into smaller, focused ones.\n\n### **Example**\n\n#### C\n\n```c\n// Bad: Monolithic function\nvoid process_order(Order order) {\n    if (!order.is_valid()) return;\n    update_inventory(order);\n    generate_receipt(order);\n    send_notification(order);\n}\n\n// Good: Split into smaller functions\nint is_order_valid(Order order) {\n    return order.is_valid();\n}\n\nvoid process_order(Order order) {\n    if (!is_order_valid(order)) return;\n    update_inventory(order);\n    generate_receipt(order);\n    send_notification(order);\n}\n```\n\n#### C++\n\n```cpp\n// Bad: Monolithic function\nvoid processOrder(Order order) {\n    if (!order.isValid()) return;\n    updateInventory(order);\n    generateReceipt(order);\n    sendNotification(order);\n}\n\n// Good: Split into smaller functions\nbool isOrderValid(Order order) {\n    return order.isValid();\n}\n\nvoid processOrder(Order order) {\n    if (!isOrderValid(order)) return;\n    updateInventory(order);\n    generateReceipt(order);\n    sendNotification(order);\n}\n```\n\n#### Rust\n\n```rust\n// Bad: Monolithic function\nfn process_order(order: Order) {\n    if !order.is_valid() { return; }\n    update_inventory(order);\n    generate_receipt(order);\n    send_notification(order);\n}\n\n// Good: Split into smaller functions\nfn is_order_valid(order: \u0026Order) -\u003e bool {\n    order.is_valid()\n}\n\nfn process_order(order: Order) {\n    if !is_order_valid(\u0026order) { return; }\n    update_inventory(order);\n    generate_receipt(order);\n    send_notification(order);\n}\n```\n\n#### Java\n\n```java\n// Bad: Monolithic function\npublic void processOrder(Order order) {\n    if (!order.isValid()) return;\n    updateInventory(order);\n    generateReceipt(order);\n    sendNotification(order);\n}\n\n// Good: Split into smaller functions\npublic boolean isOrderValid(Order order) {\n    return order.isValid();\n}\n\npublic void processOrder(Order order) {\n    if (!isOrderValid(order)) return;\n    updateInventory(order);\n    generateReceipt(order);\n    sendNotification(order);\n}\n```\n\n#### PHP\n\n```php\n// Bad: Monolithic function\nfunction processOrder($order) {\n    if (!$order-\u003eisValid()) return;\n    updateInventory($order);\n    generateReceipt($order);\n    sendNotification($order);\n}\n\n// Good: Split into smaller functions\nfunction isOrderValid($order) {\n    return $order-\u003eisValid();\n}\n\nfunction processOrder($order) {\n    if (!isOrderValid($order)) return;\n    updateInventory($order);\n    generateReceipt($order);\n    sendNotification($order);\n}\n```\n\n#### JavaScript\n\n```javascript\n// Bad: Monolithic function\nfunction processOrder(order) {\n  if (!order.isValid()) return;\n  updateInventory(order);\n  generateReceipt(order);\n  sendNotification(order);\n}\n\n// Good: Split into smaller functions\nfunction isOrderValid(order) {\n  return order.isValid();\n}\n\nfunction processOrder(order) {\n  if (!isOrderValid(order)) return;\n  updateInventory(order);\n  generateReceipt(order);\n  sendNotification(order);\n}\n```\n\n#### Python\n\n```python\n# Bad: Monolithic function\ndef process_order(order):\n    if not order.is_valid():\n        return\n    update_inventory(order)\n    generate_receipt(order)\n    send_notification(order)\n\n# Good: Split into smaller functions\ndef is_order_valid(order):\n    return order.is_valid()\n\ndef process_order(order):\n    if not is_order_valid(order):\n        return\n    update_inventory(order)\n    generate_receipt(order)\n    send_notification(order)\n```\n\n---\n\n## 4. Avoid Magic Numbers\n\nMagic numbers are hardcoded values without context. Replace them with named constants.\n\n### **Example**\n\n#### C\n\n```c\n// Bad: Magic numbers\nint shipping_cost = weight * 5;\n\n// Good: Named constants\n#define SHIPPING_RATE 5\nint shipping_cost = weight * SHIPPING_RATE;\n```\n\n#### C++\n\n```cpp\n// Bad: Magic numbers\nint shippingCost = weight * 5;\n\n// Good: Named constants\nconst int SHIPPING_RATE = 5;\nint shippingCost = weight * SHIPPING_RATE;\n```\n\n#### Rust\n\n```rust\n// Bad: Magic numbers\nlet shipping_cost = weight * 5;\n\n// Good: Named constants\nconst SHIPPING_RATE: i32 = 5;\nlet shipping_cost = weight * SHIPPING_RATE;\n```\n\n#### Java\n\n```java\n// Bad: Magic numbers\nint shippingCost = weight * 5;\n\n// Good: Named constants\nfinal int SHIPPING_RATE = 5;\nint shippingCost = weight * SHIPPING_RATE;\n```\n\n#### PHP\n\n```php\n// Bad: Magic numbers\n$shipping_cost = $weight * 5;\n\n// Good: Named constants\ndefine('SHIPPING_RATE', 5);\n$shipping_cost = $weight * SHIPPING_RATE;\n```\n\n#### JavaScript\n\n```javascript\n// Bad: Magic numbers\nlet shippingCost = weight * 5;\n\n// Good: Named constants\nconst SHIPPING_RATE = 5;\nlet shippingCost = weight * SHIPPING_RATE;\n```\n\n#### Python\n\n```python\n# Bad: Magic numbers\nshipping_cost = weight * 5\n\n# Good: Named constants\nSHIPPING_RATE = 5\nshipping_cost = weight * SHIPPING_RATE\n```\n\n---\n\n## 5. Minimize Comments\n\nWrite self-documenting code to reduce the need for comments. Use comments only for complex or non-obvious logic.\n\n### **Example**\n\n#### C\n\n```c\n// Bad: Excessive comments\n// Check if the user is eligible for a discount\nif (user.age \u003e 65) {  // Senior citizen discount\n    apply_discount(user);\n}\n\n// Good: Self-documenting code\n#define SENIOR_CITIZEN_AGE 65\nif (user.age \u003e SENIOR_CITIZEN_AGE) {\n    apply_discount(user);\n}\n```\n\n#### C++\n\n```cpp\n// Bad: Excessive comments\n// Check if the user is eligible for a discount\nif (user.age \u003e 65) {  // Senior citizen discount\n    applyDiscount(user);\n}\n\n// Good: Self-documenting code\nconst int SENIOR_CITIZEN_AGE = 65;\nif (user.age \u003e SENIOR_CITIZEN_AGE) {\n    applyDiscount(user);\n}\n```\n\n#### Rust\n\n```rust\n// Bad: Excessive comments\n// Check if the user is eligible for a discount\nif user.age \u003e 65 {  // Senior citizen discount\n    apply_discount(user);\n}\n\n// Good: Self-documenting code\nconst SENIOR_CITIZEN_AGE: i32 = 65;\nif user.age \u003e SENIOR_CITIZEN_AGE {\n    apply_discount(user);\n}\n```\n\n#### Java\n\n```java\n// Bad: Excessive comments\n// Check if the user is eligible for a discount\nif (user.getAge() \u003e 65) {  // Senior citizen discount\n    applyDiscount(user);\n}\n\n// Good: Self-documenting code\nfinal int SENIOR_CITIZEN_AGE = 65;\nif (user.getAge() \u003e SENIOR_CITIZEN_AGE) {\n    applyDiscount(user);\n}\n```\n\n#### PHP\n\n```php\n// Bad: Excessive comments\n// Check if the user is eligible for a discount\nif ($user-\u003eage \u003e 65) {  // Senior citizen discount\n    applyDiscount($user);\n}\n\n// Good: Self-documenting code\ndefine('SENIOR_CITIZEN_AGE', 65);\nif ($user-\u003eage \u003e SENIOR_CITIZEN_AGE) {\n    applyDiscount($user);\n}\n```\n\n#### JavaScript\n\n```javascript\n// Bad: Excessive comments\n// Check if the user is eligible for a discount\nif (user.age \u003e 65) {\n  // Senior citizen discount\n  applyDiscount(user);\n}\n\n// Good: Self-documenting code\nconst SENIOR_CITIZEN_AGE = 65;\nif (user.age \u003e SENIOR_CITIZEN_AGE) {\n  applyDiscount(user);\n}\n```\n\n#### Python\n\n```python\n# Bad: Excessive comments\n# Check if the user is eligible for a discount\nif user.age \u003e 65:  # Senior citizen discount\n    apply_discount(user)\n\n# Good: Self-documenting code\nSENIOR_CITIZEN_AGE = 65\nif user.age \u003e SENIOR_CITIZEN_AGE:\n    apply_discount(user)\n```\n\n---\n\n## Conclusion\n\nBy following these **five clean code principles**, you can write code that is:\n\n- **Readable**: Easy for others (and your future self) to understand.\n- **Maintainable**: Simplifies debugging, testing, and updating.\n- **Professional**: Demonstrates good coding practices, especially in interviews.\n\nRemember, clean code is not just about functionality—it's about creating a positive experience for everyone who interacts with your code. Start applying these principles today, and watch your coding skills improve!\n\nHappy coding! 🚀\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faliezzahn%2Fclean-code-principles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faliezzahn%2Fclean-code-principles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faliezzahn%2Fclean-code-principles/lists"}