バグを修正し、コードを改善する

完了

開発者が記述するコードは、ほとんどの場合は機能しますが、バグ、構文、パフォーマンス、モジュール性を修正することで改善できる場合もあります。 Azure OpenAI モデルを使用すると、コードを改善する方法を特定でき、より優れたコードを記述する方法が提案されます。

コードのバグを修正する

Azure OpenAI モデルでは、コードが分析され、問題を修正できる可能性のある変更が提案されるため、コードのバグを修正するのに役立ちます。 これは、開発者がより迅速かつ効率的にバグを特定して解決するのに役立ちます。

たとえば、次の関数を作成したが、機能しないとしましょう。

def calculate_average(numbers):  
    total = 0  
    for i in range(len(numbers)):  
        number = numbers[i]  
        total += number  
    average = total  
    rerun averave  
public double CalculateAverage(int[] numbers)
{
    int total = 0;
    for (int i = 0; i < numbers.Length; i++)
    {
        int number = numbers[i];
        total += number;
    }
    double average = (double)total;
    rerun averave;
}

この関数をプロンプト Fix the bugs in this function と共にモデルに提供すると、修正されたコードと、修正された内容に関する説明が返されます。

def calculate_average(numbers):  
    total = 0  
    for i in range(len(numbers)):  
        number = numbers[i]  
        total += number  
    average = total / len(numbers)  
    return average  
public double CalculateAverage(int[] numbers)
{
    int total = 0;
    for (int i = 0; i < numbers.Length; i++)
    {
        int number = numbers[i];
        total += number;
    }
    double average = (double)total / numbers.Length;
    return average;
}
There were a few issues with the original code:

1. The variable "average" was assigned the value of "total" instead of the actual average.
2. There was a typo in the last line - "rerun" should be "return".
3. The average was not actually calculated - the corrected code divides the total by the length of 
the "numbers" list to get the average.

パフォーマンスの向上

コード開発者が記述したコードは機能する可能性がありますが、より効率的にタスクを実行する方法があるかもしれません。 最初の n 個の正の整数の合計を計算する関数の例を次に示します。これは、効率的ではない可能性があります。

def sum_of_n(n):
    result = 0
    for i in range(1, n+1):
        result += i
    return result
public int SumOfN(int n)  
{  
    int result = 0;  
    for (int i = 1; i <= n; i++)  
    {  
        result += i;  
    }  
    return result;  
}  

このバージョンは正しく機能しますが、時間計算量は O(n) です。 モデルに提供した場合の応答を次に示します。

We can improve this function it by using a mathematical formula instead.

Here's the more efficient version:
def sum_of_n(n):
    return n * (n + 1) // 2
public int SumOfN(int n)  
{  
    return n * (n + 1) / 2;  
}  

このバージョンでも正しい結果が返されますが、時間計算量は O(1) になり、効率性がはるかに向上します。

非効率的なコードをリファクタリングする

優れたコードほどバグが発生する可能性が低く、保守が容易になります。Azure OpenAI モデルは、コードのリファクタリング方法を開発者にガイドするのに役立ちます。

次の関数について考えてみましょう。

def calculateTotalPrice(item, quantity):
    if item == 'apple':
        return quantity * 0.5
    elif item == 'banana':
        return quantity * 0.75
    elif item == 'orange':
        return quantity * 0.6
    else:
        return 0
public double CalculateTotalPrice(string item, int quantity)
{
    if (item == "apple")
    {
        return quantity * 0.5;
    }
    else if (item == "banana")
    {
        return quantity * 0.75;
    }
    else if (item == "orange")
    {
        return quantity * 0.6;
    }
    else
    {
        return 0;
    }
}

このコードは、名前と数量に基づいて特定の項目の合計価格を計算します。 ただし、このコードはモジュール化されていないため、保守が困難な可能性があります。 これをリファクタリングの要求と共にモデルに提供した場合の応答を次に示します。

item_prices = {  
    'apple': 0.5,  
    'banana': 0.75,  
    'orange': 0.6  
}  

def calculate_total_price(item, quantity):  
    price = item_prices.get(item, 0)  
    return quantity * price  
static Dictionary<string, double> itemPrices = new Dictionary<string, double>()  
{  
    {"apple", 0.5},  
    {"banana", 0.75},  
    {"orange", 0.6}  
};  

static double CalculateTotalPrice(string item, int quantity)  
{  
    double price;  
    if (!itemPrices.TryGetValue(item, out price))  
    {  
        price = 0;  
    }  
    return quantity * price;  
}  

モデルからは、コードと共に、リファクタリングの説明が提供されます。

In this refactored code, we've created an object called itemPrices that maps item names to their 
prices. We've also simplified the calculateTotalPrice function by using the itemPrices object to look 
up the price of the item, rather than using a series of if/else statements. This makes the code more 
modular and easier to maintain, and also reduces the risk of introducing bugs in the future.