{"meta":{"title":"Improving code readability and maintainability","intro":"Copilot Chat can suggest ways to make your code easier to understand and maintain.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat Cookbook"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/refactor-code","title":"Refactor code"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/refactor-code/improve-code-readability","title":"Improve code readability"}],"documentType":"article"},"body":"# Improving code readability and maintainability\n\nCopilot Chat can suggest ways to make your code easier to understand and maintain.\n\nCode with poor readability is difficult for other developers to maintain and extend. Copilot Chat can help in a number of ways. For example, by:\n\n* [Suggesting improvements to variable names](#improving-variable-names)\n* [Avoiding sequential conditional checks](#avoiding-sequential-conditional-checks)\n* [Reducing nested logic](#reducing-nested-logic)\n* [Splitting large methods into smaller, more readable ones](#splitting-up-large-methods)\n\nDocumenting your code is another way to improve the maintainability of your code. For information about using Copilot Chat to help you add useful comments to your code, see the example prompts in [Documenting code](/en/copilot/copilot-chat-cookbook/documenting-code).\n\n> \\[!NOTE] The responses shown in this article are examples. Copilot Chat responses are non-deterministic, so you may get different responses from the ones shown here.\n\n## Improving variable names\n\nDescriptive variable names and parameter names make it easier to understand their purpose.\n\n### Example scenario\n\nThis JavaScript code logs a message about a person's age to the console. The abstract parameter names make it difficult to understand the purpose of the code.\n\n```javascript id=log-persons-age\nfunction logPersonsAge(a, b, c) {\n  if (c) {\n    console.log(a + \" is \" + b + \" years old.\");\n  } else {\n    console.log(a + \" does not want to reveal their age.\");\n  }\n}\n```\n\n### Example prompt\n\nIn the editor, select the function you want to change, then ask Copilot Chat:\n\n```copilot copy prompt ref=log-persons-age\nImprove the variable names in this function\n```\n\n### Example response\n\nCopilot suggests descriptive variable names.\n\n```javascript\nfunction logPersonAge(name, age, revealAge) {\n  if (revealAge) {\n    console.log(name + \" is \" + age + \" years old.\");\n  } else {\n    console.log(name + \" does not want to reveal their age.\");\n  }\n}\n```\n\n## Avoiding sequential conditional checks\n\n`if...else` chains can be difficult to read, especially when they are long.\n\n### Example scenario\n\nThis Python code prints the sound that various animals make, if defined, or \"Unknown animal\" if the animal type is not recognized. However, the chain of `if...else` statements makes the code inefficient and cumbersome.\n\n```python id=animal-sound\nclass Animal:\n    def speak(self):\n        pass\n\nclass Dog(Animal):\n    def speak(self):\n        return \"Woof!\"\n\nclass Cat(Animal):\n    def speak(self):\n        return \"Meow!\"\n\nclass Bird(Animal):\n    def speak(self):\n        return \"Tweet!\"\n\ndef animal_sound(animal_type):\n    if animal_type == \"dog\":\n        return Dog().speak()\n    elif animal_type == \"cat\":\n        return Cat().speak()\n    elif animal_type == \"bird\":\n        return Bird().speak()\n    else:\n        return \"Unknown animal\"\n\nprint(animal_sound(\"dog\"))\nprint(animal_sound(\"cat\"))\nprint(animal_sound(\"bird\"))\nprint(animal_sound(\"fish\"))\n```\n\n### Example prompt\n\n```copilot copy prompt ref=animal-sound\nSimplify this code. Avoid using if/else chains but retain all function return values.\n```\n\n### Example response\n\nCopilot suggests using a dictionary to map the animal types to their corresponding classes.\n\n```python\nclass Animal:\n    def speak(self):\n        pass\n\nclass Dog(Animal):\n    def speak(self):\n        return \"Woof!\"\n\nclass Cat(Animal):\n    def speak(self):\n        return \"Meow!\"\n\nclass Bird(Animal):\n    def speak(self):\n        return \"Tweet!\"\n\ndef animal_sound(animal_type):\n    animals = {\n        \"dog\": Dog,\n        \"cat\": Cat,\n        \"bird\": Bird\n    }\n    animal_class = animals.get(animal_type, Animal)\n    return animal_class().speak() if animal_class != Animal else \"Unknown animal\"\n\nprint(animal_sound(\"dog\"))\nprint(animal_sound(\"cat\"))\nprint(animal_sound(\"bird\"))\nprint(animal_sound(\"fish\"))\n```\n\n## Reducing nested logic\n\nDeeply nested structures can make the code hard to follow, making it difficult to modify or extend the logic of the code in future.\n\n### Example scenario\n\nThis Ruby code prints information about a user account based on three parameters. The use of nested `if...else` statements makes the code unnecessarily complex.\n\n```ruby id=determine-access\ndef determine_access(user_role, has_permission, is_active)\n  if user_role == \"admin\"\n    if has_permission\n      if is_active\n        \"Active admin account with full access.\"\n      else\n        \"Inactive admin account.\"\n      end\n    else\n      \"Admin account lacks necessary permissions.\"\n    end\n  else\n    \"Access denied.\"\n  end\nend\n\nputs determine_access(\"admin\", true, true)\nputs determine_access(\"admin\", true, false)\nputs determine_access(\"admin\", false, true)\nputs determine_access(\"user\", true, true)\n```\n\n### Example prompt\n\n```copilot copy prompt ref=determine-access\nRewrite this code to avoid the nested if/else statements\n```\n\n### Example response\n\nCopilot suggests using guard clauses to handle the conditions early and return the appropriate messages.\n\n```ruby\ndef determine_access(user_role, has_permission, is_active)\n  return \"Access denied.\" unless user_role == \"admin\"\n  return \"Admin account lacks necessary permissions.\" unless has_permission\n  return \"Inactive admin account.\" unless is_active\n\n  \"Active admin account with full access.\"\nend\n\nputs determine_access(\"admin\", true, true)\nputs determine_access(\"admin\", true, false)\nputs determine_access(\"admin\", false, true)\nputs determine_access(\"user\", true, true)\n```\n\n## Splitting up large methods\n\nIt can be difficult to grasp exactly what a method or function does if it is too long, making it difficult to maintain. Methods or functions that perform multiple tasks may not be reusable in other contexts. It may also be difficult to test each task in isolation.\n\n### Example scenario\n\nThis Java method processes a customer order and prints a message. It performs multiple tasks in a single method.\n\n```java id=process-order\npublic void processOrder(Order order) {\n  if (order == null || order.getItems().isEmpty()) {\n    throw new IllegalArgumentException(\"Order is invalid.\");\n  }\n\n  double totalPrice = 0.0;\n  for (Item item : order.getItems()) {\n    totalPrice += item.getPrice() * item.getQuantity();\n  }\n  order.setTotalPrice(totalPrice);\n\n  if (totalPrice > 0) {\n    order.setStatus(\"Processed\");\n  } else {\n    order.setStatus(\"Pending\");\n  }\n\n  System.out.println(\"Order for customer \" + order.getCustomerName() + \" has been processed. Total price: \" + totalPrice);\n}\n```\n\n### Example prompt\n\n```copilot copy prompt ref=process-order\nHow could the processOrder method be refactored to be more useful and easier to maintain\n```\n\n### Example response\n\nCopilot suggests breaking this method down into smaller, more focused methods.\n\n```java\npublic void processOrder(Order order) {\n    validateOrder(order);\n    double totalPrice = calculateTotalPrice(order);\n    updateOrderStatus(order, totalPrice);\n    printOrderSummary(order, totalPrice);\n}\n\nprivate void validateOrder(Order order) {\n    if (order == null || order.getItems().isEmpty()) {\n        throw new IllegalArgumentException(\"Order is invalid.\");\n    }\n}\n\nprivate double calculateTotalPrice(Order order) {\n    double totalPrice = 0.0;\n    for (Item item : order.getItems()) {\n        totalPrice += item.getPrice() * item.getQuantity();\n    }\n    order.setTotalPrice(totalPrice);\n    return totalPrice;\n}\n\nprivate void updateOrderStatus(Order order, double totalPrice) {\n    if (totalPrice > 0) {\n        order.setStatus(\"Processed\");\n    } else {\n        order.setStatus(\"Pending\");\n    }\n}\n\nprivate void printOrderSummary(Order order, double totalPrice) {\n    System.out.println(\"Order for customer \" + order.getCustomerName() + \" has been processed. Total price: \" + totalPrice);\n}\n```\n\n## Further reading\n\n* [Prompt engineering for GitHub Copilot Chat](/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [Best practices for using GitHub Copilot](/en/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}