{"meta":{"title":"Documenting legacy code","intro":"副驾驶聊天 can help with documenting legacy code.","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-cookbook","title":"GitHub Copilot Cookbook"},{"href":"/zh/copilot/tutorials/copilot-cookbook/document-code","title":"Document code"},{"href":"/zh/copilot/tutorials/copilot-cookbook/document-code/document-legacy-code","title":"Document legacy code"}],"documentType":"article","redirectedFrom":"/zh/copilot/tutorials/copilot-chat-cookbook/document-code/document-legacy-code"},"body":"# Documenting legacy code\n\n副驾驶聊天 can help with documenting legacy code.\n\nWorking with legacy code can be challenging for developers, especially when the code is complex or not well-documented. In such cases, it can be helpful to use Copilot Chat to explain unclear or complex code to other developers or to document it for future reference.\n\n## Example scenario\n\nThe block of COBOL below connects to a database and inserts a record. The code lacks documentation, which makes it difficult to understand what it does and how it works.\n\n```text id=cobol-insert-record\nIDENTIFICATION DIVISION.\nPROGRAM-ID. INSERT-RECORD.\n\nENVIRONMENT DIVISION.\n\nDATA DIVISION.\nWORKING-STORAGE SECTION.\n01  WS-STATUS-FLAGS.\n    05 WS-DB-STATUS     PIC X(2).\n       88 WS-SUCCESS    VALUE \"00\".\n    05 WS-SQLCODE       PIC S9(9) COMP.\n    05 WS-ERROR-MSG     PIC X(50).\n\nLINKAGE SECTION.\n01  LS-PARAMETERS.\n    05 LS-PERSON-RECORD.\n       10 PERSON-ID     PIC 9(6).\n       10 PERSON-NAME   PIC X(50).\n       10 PERSON-AGE    PIC 9(3).\n    05 LS-RESULT        PIC X.\n       88 SUCCESS       VALUE 'T'.\n       88 FAILED        VALUE 'F'.\n\nPROCEDURE DIVISION USING LS-PARAMETERS.\n    PERFORM INSERT-AND-VALIDATE\n    GOBACK\n    .\n\nINSERT-AND-VALIDATE.\n    EXEC SQL\n        INSERT INTO persons (id, name, age)\n        VALUES (:PERSON-ID, :PERSON-NAME, :PERSON-AGE)\n    END-EXEC\n\n    IF SQLCODE = 0\n        EXEC SQL COMMIT END-EXEC\n        SET SUCCESS TO TRUE\n    ELSE\n        EXEC SQL ROLLBACK END-EXEC\n        SET FAILED TO TRUE\n        STRING \"DB Error: \" SQLCODE\n            DELIMITED BY SIZE\n            INTO WS-ERROR-MSG\n        DISPLAY WS-ERROR-MSG\n    END-IF\n    .\n```\n\n## Example prompt\n\nWe can use a simple prompt to ask 副驾驶聊天 to add documentation to the code.\n\n```copilot copy prompt ref=cobol-insert-record\nComment this code thoroughly\n```\n\n> \\[!NOTE] If you want the documentation generated in another format like Markdown, you can specify the desired output format in the prompt. For example: `Document this code in Markdown`.\n\n## Example response\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\nCopilot will provide a text overview of the code block, and an updated version with comments explaining the logic and purpose of each section.\n\nFor example:\n\n```text\n*****************************************************************\n* Program: INSERT-RECORD\n* Purpose: Inserts a person record into the database with\n*          transaction management and error handling\n*****************************************************************\n IDENTIFICATION DIVISION.\n PROGRAM-ID. INSERT-RECORD.\n\n ENVIRONMENT DIVISION.\n\n DATA DIVISION.\n WORKING-STORAGE SECTION.\n* Status flags and error handling variables\n 01  WS-STATUS-FLAGS.\n     05 WS-DB-STATUS     PIC X(2).              *> Database status\n        88 WS-SUCCESS    VALUE \"00\".            *> Success flag\n     05 WS-SQLCODE       PIC S9(9) COMP.        *> SQL return code\n     05 WS-ERROR-MSG     PIC X(50).             *> Error message\n\n LINKAGE SECTION.\n* Input/Output parameters\n 01  LS-PARAMETERS.\n     05 LS-PERSON-RECORD.\n        10 PERSON-ID     PIC 9(6).              *> Person ID\n        10 PERSON-NAME   PIC X(50).             *> Person name\n        10 PERSON-AGE    PIC 9(3).              *> Person age\n     05 LS-RESULT        PIC X.                 *> Operation result\n        88 SUCCESS       VALUE 'T'.             *> Success flag\n        88 FAILED        VALUE 'F'.             *> Failure flag\n\n PROCEDURE DIVISION USING LS-PARAMETERS.\n     PERFORM INSERT-AND-VALIDATE\n     GOBACK\n     .\n\n*****************************************************************\n* Handles database insertion with transaction management\n*****************************************************************\n INSERT-AND-VALIDATE.\n     EXEC SQL\n         INSERT INTO persons (id, name, age)\n         VALUES (:PERSON-ID, :PERSON-NAME, :PERSON-AGE)\n     END-EXEC\n\n     IF SQLCODE = 0\n         EXEC SQL COMMIT END-EXEC                *> Commit if successful\n         SET SUCCESS TO TRUE\n     ELSE\n         EXEC SQL ROLLBACK END-EXEC              *> Rollback if failed\n         SET FAILED TO TRUE\n         STRING \"DB Error: \" SQLCODE\n             DELIMITED BY SIZE\n             INTO WS-ERROR-MSG\n         DISPLAY WS-ERROR-MSG\n     END-IF\n     .\n```\n\n## Further reading\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}