(눈_눈) 談談AI在開發中,生出來的程式碼

There's a new kind of coding I call "vibe coding".
(中略)
It's possible because the LLMs (e.g. Cursor Composer w Sonnet) are getting too good. Sometimes the LLMs can't fix a bug so I just work around it or ask for random changes until it goes away. It's not too bad for throwaway weekend projects, but still quite amusing.

—Andrej Karpathy, X(原 Twitter)

不要相信任何會自己思考,但你卻不知道它把腦袋藏在哪裡的東西。

——亞瑟.衛斯理,《哈利波特-消失的密室》第 18 章《多比的獎賞》

今年公司開始要求大家大量使用 AI

所以想寫一下自己這幾個月來,使用 AI 的感想

這次先來談談由與使用 AI 的人合作開發的狀況吧


總結感想,什麼「取代你的不是AI,而是使用AI的人(遠見)」、「AI不會取代人類,但使用AI的人會取代不用AI的人(哈佛商業評論)」,才不是哩!

我愈接觸 AI 就愈覺得AI才沒辦法取代人,反倒是 AI 用得愈久, 人的重要性就愈明顯


之前一項專案,和我合作開發的人是重度AI使用者

而且他就真的是 99% 的 vibe coding


然而, AI 給出來的程式碼品質真的參差不齊

偏偏對方在程式方面又是新手,所以對於 AI 產生的內容幾乎是 100% 接受

const fetchFooData = async (token) => {
  try {
    console.log('Fetching Foo data...');
    
    // Call the api endpoint - using GET method (assuming it's a GET request)
    const response = await fetch("{{ API 網址 }}", {
      method: 'GET',
      headers: {
        'Authorization': `Bearer ${token}`,
        'Content-Type': 'application/json'
      }
    });

    if (!response.ok) {
      console.error('Failed to fetch data:', response.status);
      return null;
    }

    const data = await response.json();
    console.log('data retrieved successfully:', data);

    // Store the data in localStorage with the structured format
    const fooDataStructure = {
      id: "api/foo",
      type: "data",
      hash: Date.now(),
      data: data
    };

    // Save to localStorage
    localStorage.setItem('api/foo', JSON.stringify(fooDataStructure));
    console.log('Foo data stored in localStorage as api/foo');
    
    return data;
  } catch (error) {
    console.error('Error fetching Foo data:', error);
    return null;
  }
};

以上面這段AI寫的程式碼為例

基本上算OK,但是有幾個問題

  1. 儲存 localStorage OK,但為什麼要存成這種複雜的格式(加上了 id、type、hash)
  2. 沒有處理 API 的錯誤資料,僅 console log 帶過
  3. 沒有符合程式規範(例如這系統不使用箭頭函數撰寫,但 AI 給的是箭頭函數)
  4. 程式碼的 commit 沒有照規範(這個從上面的 code 看不出來
    基本上公司都會用 Bug 管理工具,ㄧ

今天這個 function 的這些問題幾乎都是好改的

但僅僅一個簡單的 function 就有這些問題了,何況是牽扯到業務邏輯的 function

而且這些 AI 撰寫的 function寫多了,還會導致AI 後續以這種格式繼續撰寫新的 function

使得這個屎球愈滾愈大……


程式碼規範其實算是好修的,甚至現在要靠AI處理也行,不過這前提是有規範 AI

現在許多的開發用 AI 有提供規範的方法,例如:

AI 規範檔案
github Copilot .github/copilot-instructions.md.github/instructions 目錄的檔案
Cursor .cursorrules.cursor/rules/ 目錄的檔案
JetBrains AI Assistant .aiassistant/rules目錄的檔案
Amazon Q Developer .amazonq/rules資料夾的檔案
Continue CLI .continue/rules 資料夾的檔案
Replit replit.md

以上表格是參考 ChatGPT 給的解答,加上我自己實際去看過整理而來



有沒有規範,AI生出來的東西真的差很多

沒規範,那真的不如自己寫還來得輕鬆一些

更別提AI不保證給你的程式碼是100%正確的

我都要 code review 了,光是風格就想讓我退件,那還談什麼程式正確不正確?


而程式碼的正確性嘛……

不期不待,不受傷害。

有的時候,他給的答案就是你要的;有的時候,你得改,而且這改動幅度時多時少……

這時候就覺得人還比較好,光是人的穩定性就差很多。

更何況和他人合作,你通常知道對方給的 PR 是不是一個已經做好功能的;但 AI 就不是了……



沒有留言:

張貼留言

小提示:留言時,可以使用粗體(<b>)、斜體(<i>)、超連結(<a href="網址"> </a>)。另外,以「名稱/網址」留言時,網址可以留空的。