【GitHub Copilot】GitHub Copilot的功能:生成注释
2025年09月22日
文章浏览:50
【GitHub Copilot】GitHub Copilot的功能:生成注释

如果我们的系统里面有比较古老的代码,却又没有注释,这样非常不容易理解并维护。

自从有了人工智能之后,我们就能够非常容易地添加程序的注释。


比如我有下面的Salesforce Apex代码


public with sharing class PDFController {
    public Account AccountRecord         {get; set;}
    public List<Contact> displayContacts {get; set;}

    public PDFController(ApexPages.StandardController controller) {   
        String AccountId = ApexPages.currentPage().getParameters().get('accountId');
        List<Account> Accounts = new List<Account>([
            SELECT Id,Name
            FROM   Account
            WHERE  Id =: AccountId
        ]);
            AccountRecord = Accounts.get(0);
    }

    public PageReference displayPDF(){
        try{
            /* 事前確認画面で選択した取引先責任者の数とIdをパラメータから取得 */
            List<Id> targetContactIds = new List<Id>();
            Integer contactNumParam = Integer.ValueOf(ApexPages.currentPage().getParameters().get('contactNum'));
            for(integer i = 0; i < contactNumParam; i++){
                targetContactIds.add(ApexPages.currentPage().getParameters().get('contactIds' + i));
            }
            
            displayContacts = new List<Contact>([
                SELECT Id,Name,Phone,Email
                FROM   Contact
                WHERE  ID IN: targetContactIds
            ]);
            return null;
            
        }catch(Exception ex){
           //エラー処理
           return null;
        }           
    }
}


向Chat发出添加注释的请求。

generate comments for PDFController.cls , comments for every method, and comments for each line.


GitHub Copilot Chat生成的代码,我们用WinMerge来比较一下。

结果显示,生成的注释符合我们的要求。



点击ApplyTo按钮。



下面就是我们要求达到的效果。 


正式代码如下


// PDF表示用コントローラークラス
public with sharing class PDFController {
    // 取引先レコード
    public Account AccountRecord         {get; set;}
    // 表示する取引先責任者リスト
    public List<Contact> displayContacts {get; set;}

    // コンストラクタ(標準コントローラーを受け取る)
    public PDFController(ApexPages.StandardController controller) {   
        // ページパラメータから取引先IDを取得
        String AccountId = ApexPages.currentPage().getParameters().get('accountId');
        // 取引先IDでAccountを検索
        List<Account> Accounts = new List<Account>([
            SELECT Id,Name
            FROM   Account
            WHERE  Id =: AccountId
        ]);
        // 取得したAccountをセット
        AccountRecord = Accounts.get(0);
    }

    // PDF表示処理
    public PageReference displayPDF(){
        try{
            // 事前確認画面で選択した取引先責任者の数とIdをパラメータから取得
            List<Id> targetContactIds = new List<Id>(); // 選択されたContactのIDリスト
            Integer contactNumParam = Integer.ValueOf(ApexPages.currentPage().getParameters().get('contactNum')); // Contact数取得
            for(integer i = 0; i < contactNumParam; i++){
                targetContactIds.add(ApexPages.currentPage().getParameters().get('contactIds' + i)); // 各ContactのIDを取得しリストに追加
            }
            
            // 選択されたContactを取得
            displayContacts = new List<Contact>([
                SELECT Id,Name,Phone,Email
                FROM   Contact
                WHERE  ID IN: targetContactIds
            ]);
            return null; // ページ遷移なし
            
        }catch(Exception ex){
           // エラー処理
           return null;
        }           
    }
}


翻译注释

如果我们需要把代码的注释从日语,翻译到英语。

如果在没有GitHub Copilot之前,这是一个相当大的工作量。

但现在我们已经不需要担心这样的需求了。因为我们有了GitHub Copilot,能够轻松完成注释的翻译工作。


方法也很简单,就是向Chat发送请求就可以了。

好,我们尝试着向Chat发送如下请求。

please translate all comments from japanese into english. 


在正式采用之前,我们用WinMerge来检查一下。


好像没什么问题。 

点击Keep按钮。


采用英语注释后的代码如下。

// Controller class for PDF display
public with sharing class PDFController {
    // Account record
    public Account AccountRecord         {get; set;}
    // List of contacts to display
    public List<Contact> displayContacts {get; set;}

    // Constructor (receives standard controller)
    public PDFController(ApexPages.StandardController controller) {   
        // Get Account ID from page parameters
        String AccountId = ApexPages.currentPage().getParameters().get('accountId');
        // Search for Account by Account ID
        List<Account> Accounts = new List<Account>([
            SELECT Id,Name
            FROM   Account
            WHERE  Id =: AccountId
        ]);
        // Set the retrieved Account
        AccountRecord = Accounts.get(0);
    }

    // PDF display processing
    public PageReference displayPDF(){
        try{
            // Get the number and IDs of contacts selected on the pre-confirmation screen from parameters
            List<Id> targetContactIds = new List<Id>(); // List of selected Contact IDs
            Integer contactNumParam = Integer.ValueOf(ApexPages.currentPage().getParameters().get('contactNum')); // Get number of contacts
            for(integer i = 0; i < contactNumParam; i++){
                targetContactIds.add(ApexPages.currentPage().getParameters().get('contactIds' + i)); // Get each Contact ID and add to list
            }
            
            // Retrieve selected Contacts
            displayContacts = new List<Contact>([
                SELECT Id,Name,Phone,Email
                FROM   Contact
                WHERE  ID IN: targetContactIds
            ]);
            return null; // No page navigation
            
        }catch(Exception ex){
           // Error handling
           return null;
        }           
    }
}


关注 收藏