クラスの説明をするChatGPTプロンプト(Python用)

ChatGPTにソースコードを渡して単に説明してと問いかけると、ひたすらコードを解説してくれます。ただ、後で見やすいような形にしたかったので、表やクラス図にしてもらう方法を考えました。

以下のコード中のクラスをコピペしてChatGPT4に解説をお願いした結果です。
ちなみにコード上にはほとんどコメントはないです。
[minesweeper_grid.py](https://github.com/yu8ikmnbgt6y/chatgpt_minesweeper/blob/v1.0.0/src/minesweeper_grid.py)

## コード->メンバの説明テーブル
### プロンプト
```markdown
# Code Description Table

Create a table that interprets the code written in Python in the ##CODE field and describes the class.
The table to be answered should follow the conditions in the ##Condition field

## Condition
Format: Markdown, Table
Table Header Content: ["Name", "Access", "Member Type", "Data Type", "Description"]
Row: member

Please enter the items in the table according to the content of the header
The description of each item is as follows

* "Name":
  * The name of member
* "Access":
  Describes which Access Specifier the member corresponds to. It will be "Public" or "Private" in Python code.
* "Member Type":
	Describes whether the member is a "Method", "Variable" or "Property".
	When the Method is decorated as @classmethod add "/class" after the "Method".
	When the Method is decorated as @staticmethod add "/static" after the "Method".
* "DataType":
  * DataType of the member.
  * Describe what Datatype is returned when the member is called.
  * If the member is a method or property, this item means the return value's DataType.
  * If the member is a variable, this item means the member's own DataType.
* "Description":
  * Description of the member.
  * The length of this item depends on the importance of the member.
  * If the member is of high importance, the description should be longer, up to 8 sentences.
  * IF the member is not important, make description shorter. One sentence is acceptable.

Ensure that the table is ordered according to these two priorities:
	1. Members whose 'access' is 'public' must appear above those whose access is 'private'.
	2. Members should be listed in the same order as they appear in the source code unless the above conditions are violated.


## CODE

*********************************************
*************ここにコードをコピペ*************
*********************************************
```

### 結果例

| Name                   | Access | Member Type | Data Type            | Description                                                                                                                                           |
|------------------------|--------|-------------|----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|
| `__init__`             | Public | Method      | None                 | Initializes the MinesweeperGrid instance with the given number of rows, columns, and mines and creates the grid with Cell objects.                    |
| `grid_width`           | Public | Property    | int                  | Returns the width of the grid in pixels.                                                                                                              |
| `grid_height`          | Public | Property    | int                  | Returns the height of the grid in pixels.                                                                                                             |
| `n_flagged`            | Public | Property    | int                  | Returns the number of flagged cells in the grid.                                                                                                      |
| `in_bounds`            | Public | Method      | bool                 | Returns True if the given row and column are within the grid bounds, otherwise False.                                                                 |
| `initialize_grid`      | Public | Method      | None                 | Sets up the grid with mines and adjacent mines after the first click.                                                                                 |
| `open_cell`            | Public | Method      | Tuple[bool, List[Cell]] | Opens the cell at the given row and column and returns a tuple with a boolean indicating if a mine was hit and a list of opened cells.               |
| `flag_cell`            | Public | Method      | Cell                 | Toggles the flag status of the cell at the given row and column and returns the modified cell.                                                        |
| `check_game_status`    | Public | Method      | str                  | Returns the game status as "lost", "won", or "ongoing" based on the opened and flagged cells in the grid.                                             |
| `print_opened_cells`   | Public | Method      | None                 | Prints a textual representation of the opened cells in the grid.                                                                                      |
| `__str__`              | Public | Method      | str                  | Returns a string representation of the grid with mines and adjacent mines count.                                                                      |
| `setup_mines`          | Private| Method      | None                 | Sets up the mines in the grid, excluding the cell at the given row and column.                                                                        |
| `setup_adjacent_mines` | Private| Method      | None                 | Sets up the adjacent mines count for each cell in the grid.                                                                                           |
| `n_rows`               | Private| Variable    | int                  | The number of rows in the grid.                                                                                                                       |
| `n_cols`               | Private| Variable    | int                  | The number of columns in the grid.                                                                                                                    |
| `n_mines`              | Private| Variable    | int                  | The number of mines in the grid.                                                                                                                      |
| `cell_pixel_size`      | Private| Variable    | int                  | The size of each cell in pixels.                                                                                                                      |
| `cells`                | Private| Variable    | List[List[Cell]]     | The 2D list containing Cell instances for each position in the grid.                                                                                  |
| `initialized`          | Private| Variable    | bool                 | A boolean indicating whether the grid has been initialized with mines and adjacent mines after the first click.                                       |


　おそらく、privateならmethodだけを出力しろとかもできると思います。
ただし、表のソートは割といい加減でよく忘れます。なので、もう一回

```markdown
Ensure that the table is ordered according to these two priorities:
	1. Members whose 'access' is 'public' must appear above those whose access is 'private'.
	2. Members should be listed in the same order as they appear in the source code unless the above conditions are violated.
```
とコピペして打つと、ソートを修正してくれます。

また、Descriptionを日本語にしてもらうとして
```markdown
* "Description":
  * This item must be written in Japanese
```
と追記して、試しに問い合わせてみました。
その結果、
Descriptionの項を日本語にすること自体はできたものの、上記の表より5,6個出力数が少なかったり、メンバでないクラスそのもの(MinesweeperGrid)を出してしまったり、と微妙に性能が落ちてしまいました。
もう少し小さめのクラスであればいけるかもしれません。


## コード->クラス図

　一つのクラスに着目するパターンと、たくさんのクラスの関係に着目するパターンの二種類があります。コードについて何度も話しているチャット内で実行する方が精度が高いです。

### 単一クラス
#### プロンプト
```markdown
# ClassDiagram
Please create a class diagram from the ##CODE field below.
You may also use information obtained during the discussion.
The output of this class diagram must focus on a single class.
The class of interest is specified in the ##Class field.

## Format
Markdown, Mermaid

## Class
Output Class diagram must include this class
* #####クラスの名前#####

## Include
* All members of the class of interest.
* Classes the class of interest relates directly.
* Classes which is not the class of interest should not have information on their members.

## Exclude
* Classes that are not directly related to the class of interest.



## CODE
*********************************************
*************ここにコードをコピペ*************
*********************************************
```

#### 結果例

```mermaid
classDiagram
    class MinesweeperGrid {
        -n_rows: int
        -n_cols: int
        -n_mines: int
        -cell_pixel_size: int
        -cells: List[List[Cell]]
        -initialized: bool
        +grid_width(): int
        +grid_height(): int
        +n_flagged(): int
        +in_bounds(row: int, col: int): bool
        +initialize_grid(first_click_row: int, first_click_col: int): None
        +setup_mines(exclude_row: int, exclude_col: int): None
        +setup_adjacent_mines(): None
        +open_cell(row: int, col: int): Tuple[bool, List[Cell]]
        +flag_cell(row: int, col: int): Cell
        +check_game_status(): str
        +print_opened_cells(): None
        +__str__(): str
    }
    MinesweeperGrid "1" *-- "*" Cell: cells
```

### クラスの関連を表す図

こちらはチャット内で、描くクラスのソース等がすでに入力されていることを前提にしています。
#### プロンプト
```markdown
# ClassDiagram
Please make the Class Diagram for our implementation we discussed until now.
The Class diagram you will make have all the classes the application has.
So, it will be the overall view of the application.

## Format
Markdown, Mermaid

## Classes
Output Class diagram must include these classes
#####含むべきクラスを列挙する以下は例#######
* "Cell"
* "Interface"
* "MinesweeperGrid"
* "ScoreBoard"
* "GameScreen"
* "Menu"
* "StartScreen"
* "Timer"
* "GameStatisticsWindow"

## Exclude
Exclude class members contained in above classes from this output.
Focus on describing the relationship between the classes.
Just depict class boxes and relate them.

## Relationship
There are types of relationship between classes like below.
When relating classes, use a relational expression such as the one below.

| Relationship | Mermaid Representation |
| --- | --- |
| Inheritance | ``` A <|-- B``` |
| Aggregation | ``` A o-- B``` |
| Composition | ``` A *-- B``` |
| Association | ``` A --> B``` |
| Link | ``` A -- B``` |
| Dependency | ``` A ..> B``` |
| Realization | ``` A ..|> B``` |
| Multiplicity | ```eg.) A "1" -- "1..*" B``` |



```

#### 結果例

```mermaid
classDiagram
    Cell
    Interface
    MinesweeperGrid
    ScoreBoard
    GameScreen
    Menu
    StartScreen
    Timer
    GameStatisticsWindow

    Interface *-- Menu
    Interface *-- StartScreen
    Interface *-- GameScreen
    Interface *-- GameStatisticsWindow
    Interface *-- ScoreBoard

    GameScreen *-- MinesweeperGrid
    GameScreen *-- Timer

    MinesweeperGrid *-- Cell

    GameStatisticsWindow *-- ScoreBoard
```

細かなところで誤りがあるので、自分で修正をする必要がありますが大まかな形は出してくれます。

---
このような機能がIDE等に統合され、もっと精度のよい図を書けるプロンプトを見つけて、ドキュメント↔ソースコードが常に連動するようなことが可能になれば、開発の生産性はこれまでのよりずっと高まるなと思います。