# ドキュメントとコードの変更の同期

コパイロットチャット は、コードのドキュメントを最新の状態に保つのに役立ちます。

コードに対する変更でドキュメントを最新の状態に保つのは難しい場合があります。 しかし、コードベースを維持し、開発者がコードを効果的に操作できるようにするためには、適切なドキュメントが不可欠です。 コパイロットチャット は、既存のコード ドキュメントの更新を支援できます。

## シナリオ例

カテゴリ名で製品を取得する TypeScript 関数があり、しかしドキュメントが古くなっているシナリオを想像してみてください。

```typescript id=fetch-products-by-category
/**
 * Retrieves all products belonging to a specific category.
 * * @param categoryId - The unique identifier of the product category
 * @returns Promise that resolves to an array of Product objects
 * * @example
 * const products = await getByCategoryName(5);
 * // Returns: [{id: 1, name: "Product 1", categoryId: 5}, ...]
 * * @throws {PrismaClientKnownRequestError} If database query fails
 * @see {@link https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany}
 */
async getByCategoryName(categoryId: number, page = 1, pageSize = 10): Promise<Products[]> {
  return await prisma.products.findMany({
    where: { categoryId },
    skip: (page - 1) * pageSize,
    take: pageSize,
    include: {
      reviews: {
        orderBy: { date: 'desc' },
        take: 5,
      },
    },
  });
}
```

## プロンプトの例

現在のコードに従ってドキュメントを更新するよう、コパイロットチャット に依頼できます。

```copilot copy prompt ref=fetch-products-by-category
Update the existing documentation for the getByCategoryName function to reflect the current implementation.
```

## 応答の例

> \[!NOTE] 次の応答は例です。 コパイロットチャット の応答は非決定的であるため、ここで示したものとは異なる応答が返される可能性があります。

Copilot は、`getByCategoryName` 関数の更新されたドキュメントを作成することを説明します。 次に、更新されたドキュメントを提供します。それには、`page` と `pageSize` パラメーター、およびスローされる可能性がある `PrismaClientKnownRequestError` エラーが含まれるようになりました。

次に例を示します。

```typescript
/**
 * Retrieves all products belonging to a specific category with pagination and recent reviews.
 * * @param categoryId - The unique identifier of the product category
 * @param page - The page number for pagination (default: 1)
 * @param pageSize - Number of products per page (default: 10)
 * @returns Promise that resolves to an array of Product objects with their 5 most recent reviews
 * * @example
 * const products = await getByCategoryName(5, 2, 20);
 * // Returns: [{id: 1, name: "Product 1", categoryId: 5, reviews: [...5 recent reviews]}, ...]
 * * @throws {PrismaClientKnownRequestError} If database query fails
 * @see {@link https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany}
 */
```

## 参考資料

* [GitHub Copilot Chat のプロンプト エンジニアリング](/ja/copilot/using-github-copilot/prompt-engineering-for-github-copilot)
* [GitHub Copilot の使用に関するベスト プラクティス](/ja/copilot/using-github-copilot/best-practices-for-using-github-copilot)