{"meta":{"title":"Explaining legacy code","intro":"Copilot Chat can help with explaining unfamiliar code.","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/document-code","title":"Document code"},{"href":"/en/copilot/tutorials/copilot-chat-cookbook/document-code/explain-legacy-code","title":"Explain legacy code"}],"documentType":"article"},"body":"# Explaining legacy code\n\nCopilot Chat can help with explaining unfamiliar code.\n\nOne of the biggest challenges with legacy code is helping developers understand it who aren't familiar with the languages or frameworks. With Copilot Chat, you can explain the background you have and ask for an explanation.\n\n## Example scenario\n\nConsider the following COBOL code. If you're a Python developer (as an example), you might not be familiar with COBOL, so you could ask Copilot Chat to explain the code to you.\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\nYou can ask for an explanation of the COBOL code from the point of view of a Python developer.\n\n```copilot copy prompt ref=cobol-insert-record\nI'm a Python developer. Help me better understand this code. Please provide analogies.\n```\n\n## Example response\n\n> \\[!NOTE] The following response is an example. Copilot Chat responses are non-deterministic, so you may get a different response from the one shown here.\n\nCopilot provides a high-level overview of the code, before walking through the various operations and explaining them in a way that Python developers can understand. It uses analogies to make the explanation more accessible. The response might start with the following:\n\n```text\nLet me explain this COBOL code using Python analogies:\n\nHigh-Level Overview\nThis is similar to a Python function that inserts a person's record into a database with error handling.\n```\n\n```python\n# In Python, the COBOL divisions would be like:\n\n# IDENTIFICATION DIVISION = module/file name\n# PROGRAM-ID = function name\ndef insert_record():\n    pass\n\n# DATA DIVISION would be like declaring these variables:\nclass WorkingStorage:\n    db_status = \"\"  # WS-DB-STATUS\n    sqlcode = 0     # WS-SQLCODE\n    error_msg = \"\"  # WS-ERROR-MSG\n\n# LINKAGE SECTION is like function parameters\nclass PersonRecord:\n    def __init__(self, person_id: int, person_name: str, person_age: int):\n        self.person_id = person_id    # PERSON-ID\n        self.person_name = person_name # PERSON-NAME\n        self.person_age = person_age   # PERSON-AGE\n        self.success = False          # LS-RESULT\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)"}