{"meta":{"title":"코드 가독성 및 유지 관리 기능 향상","intro":"공동 파일럿 채팅은 코드를 더 쉽게 이해하고 유지 관리할 방법을 제안할 수 있습니다.","product":"GitHub Copilot","breadcrumbs":[{"href":"/ko/copilot","title":"GitHub Copilot"},{"href":"/ko/copilot/tutorials","title":"자습서"},{"href":"/ko/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat 매뉴얼"},{"href":"/ko/copilot/tutorials/copilot-chat-cookbook/refactor-code","title":"코드 리팩터링"},{"href":"/ko/copilot/tutorials/copilot-chat-cookbook/refactor-code/improve-code-readability","title":"코드 가독성 향상"}],"documentType":"article"},"body":"# 코드 가독성 및 유지 관리 기능 향상\n\n공동 파일럿 채팅은 코드를 더 쉽게 이해하고 유지 관리할 방법을 제안할 수 있습니다.\n\n가독성이 낮은 코드는 다른 개발자가 유지 관리하고 확장하기 어렵습니다. 공동 파일럿 채팅은 여러 가지 방법으로 사용자에게 도움이 될 수 있습니다. 예를 들면 다음과 같습니다.\n\n* [변수 이름 개선 제안](#improving-variable-names)\n* [순차적 조건부 검사 방지](#avoiding-sequential-conditional-checks)\n* [중첩된 논리 감소](#reducing-nested-logic)\n* [큰 메서드를 더 작고 읽기 쉬운 메서드로 분할](#splitting-up-large-methods)\n\n코드 문서화는 코드의 유지 관리를 개선하는 또 다른 방법입니다. 공동 파일럿 채팅을 사용하여 코드에 유용한 주석을 추가하는 방법에 대한 자세한 내용은 [코드 문서화](/ko/copilot/copilot-chat-cookbook/documenting-code)의 예제 프롬프트를 참조하세요.\n\n> \\[!NOTE] 이 문서에 표시된 응답은 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.\n\n## 변수 이름 개선\n\n설명이 포함된 변수 이름과 매개 변수 이름을 사용하면 해당 용도를 더 쉽게 이해할 수 있습니다.\n\n### 예제 시나리오\n\n이 JavaScript 코드는 사용자의 나이에 대한 메시지를 콘솔에 기록합니다. 추상 매개 변수 이름을 사용하면 코드의 용도를 이해하기가 어렵습니다.\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### 예시 프롬프트\n\n편집기에서 변경할 함수를 선택한 다음, 공동 파일럿 채팅에게 다음을 요청하세요.\n\n```copilot copy prompt ref=log-persons-age\nImprove the variable names in this function\n```\n\n### 예제 응답\n\nCopilot는 설명적인 변수 이름을 제안합니다.\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## 순차적 조건부 검사 방지\n\n특히 긴 경우 `if...else` 체인을 읽기 어려울 수 있습니다.\n\n### 예제 시나리오\n\n이 Python 코드는 다양한 동물이 만드는 소리(정의된 경우)를 출력하거나 동물 유형이 인식되지 않는 경우 “알 수 없는 동물”을 출력합니다. 그러나 `if...else` 문 체인 때문에 코드가 비효율적이고 번거로워집니다.\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### 예시 프롬프트\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### 예제 응답\n\nCopilot는 사전을 사용하여 동물 유형을 해당 클래스에 매핑할 것을 제안합니다.\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## 중첩된 논리 감소\n\n깊이 중첩된 구조는 코드를 따라가기 어렵게 만들 수 있으므로 나중에 코드의 논리를 수정하거나 확장하기가 어려울 수 있습니다.\n\n### 예제 시나리오\n\n이 Ruby 코드는 세 가지 매개 변수를 기반으로 사용자 계정에 대한 정보를 출력합니다. 중첩된 `if...else` 문을 사용하면 코드가 불필요하게 복잡해집니다.\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### 예시 프롬프트\n\n```copilot copy prompt ref=determine-access\nRewrite this code to avoid the nested if/else statements\n```\n\n### 예제 응답\n\nCopilot는 가드 절을 사용하여 조건을 조기에 처리하고 적절한 메시지를 반환할 것을 제안합니다.\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## 큰 메서드 분할\n\n메서드나 함수가 너무 길면 정확히 파악하기 어려서 유지 관리가 어려울 수 있습니다. 여러 작업을 수행하는 메서드나 함수를 다른 컨텍스트에서 다시 사용할 수 없습니다. 격리된 각 작업을 테스트하는 것도 어려울 수 있습니다.\n\n### 예제 시나리오\n\n이 Java 메서드는 고객 주문을 처리하고 메시지를 출력합니다. 단일 메서드로 여러 작업을 수행합니다.\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### 예시 프롬프트\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### 예제 응답\n\nCopilot는 이 메서드를 더 작고 집중적인 메서드로 나누는 것을 제안합니다.\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## 추가 참고 자료\n\n* [GitHub Copilot 채팅에 대한 프롬프트 엔지니어링](/ko/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [GitHub 부필로트 사용에 대한 모범 사례](/ko/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}