NewsCrawler.org

How to turn AI-written code into code you understand

How to turn AI-written code into code you understand
STEP 1 全体フローを把握する:起点から終点まで、どの処理がどの順番で動くかを整理する。コードの全体像と流れを把握するため。STEP 2 各処理の役割を理解する:フローの中の各処理が「なぜ必要か」を考える。コードの中で各処理が果たす役割を理解するため。STEP 3 仕組みを言語化して整理する:フローと各処理の役割を、言語化して理解を整理する。わかったつもりになっていないか確認するため。
1 検索キーワードを入力:利用者が検索入力欄に文字を入力する(SearchInput)。2 onChangeが発火:handleSearchChange(event)が呼び出される(イベント処理)。3 入力値を保持:入力された値をsearchQueryとして状態に保存する(UserList)。4 ユーザーを絞り込む:filterUsersがusersを検索条件で絞り込む(filterUsers)。5 ユーザー一覧を更新:絞り込まれたfilteredUsersを画面に表示する(画面表示)。
Image: gihyo.jp

A book excerpt argues that code which runs is not necessarily code the developer understands. Without understanding, a developer cannot explain how the code works, find where a bug comes from, or estimate the cost of a spec change. Such unexplained code builds up "comprehension debt[1]" that lowers the productivity of the whole project. The author notes that understanding can be skipped for urgent work, tight deadlines, or code used only once.

The article describes three steps, using an internal user-management tool where AI added a name search to the user list. The first step is to grasp the overall flow, from the input change event to the list update. The second step is to test hypotheses about each function's role, such as why the input is stored in searchQuery, and check them with AI. The third step is to explain the mechanism in your own words and consider which parts would change if the specification changed. The author suggests asking AI to present the flow as a flowchart or a sequence diagram[2].

In the example, the search filters a temporary result and does not rewrite the original user list. Because of this, the developer can change or clear the keyword and filter the original list again. The article concludes that a working result is the start of understanding, not the end of the work.

Read the original gihyo.jp

Sources

  • gihyo.jp JA

    AIが書いたコードを、「わかる」コードに変える

    1 more outlet

Published: First seen: Читать по-русски

Open as a page