eugenemind.com
← กลับไปที่บล็อก

21 พฤศจิกายน 2569 · 57 นาทีในการอ่าน

โค้ดขยะจาก AI คืออะไร และจะลดมันได้อย่างไร

#ai #ai-agents #code-review #architecture

ช่วงนี้ผมสังเกตเห็นลักษณะที่ไม่น่าพอใจอย่างหนึ่งของการทำงานกับเอเจนต์ AI บ่อยขึ้นเรื่อย ๆ การเขียนโค้ดง่ายขึ้นมาก บางครั้งใช้เวลาไม่กี่นาทีก็ได้สิ่งที่เมื่อก่อนต้องใช้เวลาทั้งวัน

แต่การอ่าน ตรวจสอบ และดูแลโค้ดแบบนี้ไม่ได้ง่ายขึ้นเลย

เอเจนต์อาจสร้างคลาสหลายตัว เพิ่มชั้นนามธรรม เขียนการทดสอบครอบคลุมทุกอย่าง และบิลด์โปรเจกต์ผ่าน งานเสร็จแล้ว จากนั้นพอเปิดดูส่วนที่เปลี่ยนแปลง ก็พบว่าแทนที่จะได้การปรับแก้เล็ก ๆ กลับได้ระบบทั้งระบบที่ยังต้องนั่งทำความเข้าใจอีก

บางครั้งมันดูเรียบร้อยดี ชื่อดี ฟังก์ชันสั้น มีโปรโตคอล แบ่งหน้าที่ชัดเจน แต่ละบรรทัดแทบหาที่ติไม่ได้ แต่โครงสร้างทั้งหมดนี้มีไว้ทำไม

อะไรนับเป็นโค้ดขยะจาก AI

ในบทความนี้ โค้ดขยะจาก AI (AI slop) หมายถึงโค้ดจากเอเจนต์ที่ทำงานได้ แต่เข้ากับโปรเจกต์ได้ไม่ดี การทำความเข้าใจ ตรวจสอบ และแก้ไขมันมีต้นทุนสูง โดยที่ตัวงานไม่ได้จำเป็นต้องเป็นแบบนั้น โดยมากมันมักปรากฏในสามรูปแบบ

โค้ดขยะจาก AIโค้ดจากเอเจนต์ทำงานได้ แต่เข้ากับโปรเจกต์ได้ไม่ดี
  • การออกแบบเกินจำเป็นชั้นและนามธรรมส่วนเกิน "เผื่ออนาคต"เช่น แฟกทอรีสำหรับตัวเลือกเดียว หรือไทป์ที่ทำแค่ส่งต่อการเรียก
  • วิธีแก้ของตัวเองโปรเจกต์เคยแก้ปัญหานี้แล้ว แต่เอเจนต์ทำในแบบของตัวเองเช่น ไคลเอนต์เครือข่ายตัวที่สอง หรือวิธีจัดการข้อผิดพลาดแบบของตัวเอง
  • คนอ่านยากเอเจนต์เข้าใจ แต่คนอ่านแล้วหนักเช่น แนวคิดเดียวมีสามชื่อ หรือตรรกะง่าย ๆ ที่กระจายอยู่หลายไฟล์
ทุกรูปแบบมีต้นทุนเหมือนกัน โค้ดแบบนี้ทำความเข้าใจ ตรวจสอบ และแก้ไขได้ยาก

การออกแบบเกินจำเป็น เป็นรูปแบบที่เห็นชัดที่สุด เอเจนต์เพิ่มชั้นและนามธรรมส่วนเกิน “เผื่ออนาคต” บางครั้งก็แนบเนียนกว่านั้น การทำงานง่าย ๆ ถูกกระจายไปหลายไทป์ซึ่งทำหน้าที่แค่ส่งต่อการเรียกไปข้างหน้า ตรรกะการจัดการข้อผิดพลาดแยกตัวออกจากส่วนของแอปที่เป็นผู้ตัดสินใจจริง ๆ ว่าจะทำอย่างไรเมื่อเกิดความล้มเหลว

วิธีแก้ของตัวเอง โปรเจกต์เคยแก้ปัญหานั้นไปแล้ว แต่เอเจนต์กลับทำในแบบของตัวเอง เช่น โปรเจกต์มีแนวทางการจัดการข้อผิดพลาดที่ตกลงกันไว้แล้ว แต่เอเจนต์กลับคิดแนวทางของตัวเองขึ้นมา โค้ดทำงานได้ แต่แตกต่างจากส่วนอื่นของโปรเจกต์โดยไม่จำเป็น ตรงนี้เอกสารแยกที่รวมตัวอย่างสำคัญและบอกว่าควรไปดูที่ไหนน่าจะช่วยได้ แต่เอเจนต์ก็อาจเพิกเฉยต่อเอกสารนั้นได้เหมือนกัน

คนอ่านยาก สำหรับผม การที่คนอ่านโค้ดเข้าใจเป็นเรื่องสำคัญ โค้ดควรเข้าใจได้ไม่ใช่แค่สำหรับเอเจนต์ แต่สำหรับคนด้วย บางครั้งโค้ดทำงานได้และไม่ได้ซับซ้อนเกินจำเป็น แต่คนกลับอ่านยาก ศัพท์ทางธุรกิจคำเดียวมีชื่อเรียกสามแบบ ชื่อต่าง ๆ ไม่ตรงกับคำที่ทีมใช้พูดถึงงาน และตรรกะง่าย ๆ ต้องไล่ปะติดปะต่อจากหลายไฟล์ ลินเตอร์แทบวัดเรื่องนี้ไม่ได้ อภิธานศัพท์ที่รวบรวมศัพท์ของโดเมนและชื่อหน้าจอช่วยได้บางส่วน อย่างน้อยโค้ดก็จะพูดภาษาเดียวกับตัวงาน

โปรโตคอล เมธอดยาว ๆ หรือแอคเตอร์ที่แยกออกมา ไม่ได้พิสูจน์อะไรในตัวมันเอง สิ่งที่สำคัญคือข้อจำกัดของงานและวิธีแก้ที่มีอยู่แล้ว บางครั้งชั้นเพิ่มเติมก็จำเป็นเพื่อความปลอดภัย เพื่อให้ทดสอบได้ หรือเพื่อความเข้ากันได้ของ API ผมจึงไม่ค่อยเชื่อในรายการตรวจสอบสำเร็จรูปที่ตัดสินว่าสถาปัตยกรรมไม่ดีจากจำนวนไทป์หรือจำนวนบรรทัด

เพราะเหตุนี้ ทุกวันนี้จึงตัดสินได้ยากว่าสิ่งที่เห็นอยู่เป็นโค้ดขยะจาก AI หรือไม่ ถ้าไม่มีประสบการณ์ในโดเมนและไม่เข้าใจงานดีพอ ก็ง่ายที่จะมองชั้นที่เกินมาว่าจำเป็น และมองชั้นที่จำเป็นว่าเกินมา

ผมแบ่งวิธีแก้ที่ดูคล้ายกันไว้คร่าว ๆ แบบนี้

ดูเหมือนโค้ดขยะจาก AIอาจมีเหตุผลรองรับ
แฟกทอรีและสแตรทิจีสำหรับสถานการณ์เดียวโปรโตคอลที่ขอบเขตของโมดูล เพื่อสลับสิ่งที่พึ่งพาในการทดสอบ
ชั้นเครือข่ายชั้นที่สองข้างชั้นเดิมชั้นแยกเพื่อความปลอดภัยหรือความเข้ากันได้ของ API
แคชในหน้าจอที่ไม่มีใครขอคิวการบันทึก เพื่อไม่ให้การเขียนไฟล์ทับกันเอง
ไทป์ที่แค่ส่งต่อการเรียกไปข้างหน้าการย้ายรูปแบบข้อมูลระหว่างเวอร์ชัน

เผื่อไว้สำหรับอนาคต หรือโค้ดขยะจาก AI

เอเจนต์แทบจะอ้างอนาคตเพื่ออธิบายชั้นที่เกินมาเสมอ ผมตรวจการเผื่อไว้แบบนี้ด้วยคำถามไม่กี่ข้อ

คำถามน่าจะเป็นสถาปัตยกรรมที่จำเป็นน่าจะเป็นโค้ดขยะจาก AI
ตอนนี้มีกรณีที่สองอยู่แล้วหรือไม่การทำงานจริงสองแบบ เช่น การเก็บข้อมูลในไฟล์และในหน่วยความจำสำหรับการทดสอบแฟกทอรีที่มีตัวเลือกเดียว
ถ้าเพิ่มทีหลังจะมีต้นทุนสูงไหมรูปแบบของข้อมูลที่บันทึกไว้ API สาธารณะ และเวอร์ชันของสคีมาคลาสภายในที่แยกออกมาได้ง่ายเมื่อถึงเวลาที่ต้องใช้
งานต้องการสิ่งนี้หรือไม่การย้ายข้อมูลที่งานกำหนดไว้การซิงโครไนซ์ ทั้งที่ในงานเขียนไว้ชัดเจนว่าไม่มีแบ็กเอนด์
สิ่งนี้ปกป้องขอบเขตหรือไม่การสลับนาฬิกา เพื่อให้การทดสอบไม่ขึ้นกับเวลาจริงโปรโตคอลที่ไม่มีใครสลับ
ใครเป็นคนตัดสินใจการตัดสินใจของทีมที่บันทึกไว้ใน ADR หรือในงานความคิดเห็นของเอเจนต์ว่า “เผื่ออนาคต”

สำหรับผม คำถามที่สำคัญที่สุดคือข้อที่สอง โค้ดภายในทำให้ซับซ้อนขึ้นทีหลังได้ง่าย เมื่อมีความต้องการจริงเกิดขึ้น แต่รูปแบบข้อมูลที่อยู่ในเครื่องของผู้ใช้แล้ว หรือ API สาธารณะ เปลี่ยนทีหลังได้ยากและมีต้นทุนสูง การเผื่อไว้แบบนี้จึงควรทำตั้งแต่แรก ในการทดลองด้านล่าง ผู้รีวิวคนหนึ่งพลาดตรงนี้พอดี เขาเรียกเวอร์ชันของสคีมาข้อมูลว่าเป็นการเผื่อที่เกินจำเป็น ทั้งที่มีรูปแบบข้อมูลอยู่แล้วสองแบบและการย้ายข้อมูลเป็นสิ่งที่ต้องทำ

ปัญหาเหล่านี้คุ้นเคยกันดี แต่ความเร็วในการสะสมเปลี่ยนไปแล้ว นักพัฒนาที่รู้จักฟีเจอร์ดีอาจยอมรับส่วนที่เปลี่ยนแปลงขนาดใหญ่โดยยังไม่ทันอ่านอย่างละเอียด ส่วนคนที่มาจากสายเทคโนโลยีอื่นตอนนี้ก็แก้โมดูล Swift ได้แม้แทบไม่รู้ประวัติของมัน ในทั้งสองกรณี ผลลัพธ์ที่คอมไพล์ผ่านมาถึงเร็วกว่าความมั่นใจว่าจะดูแลต่อได้สะดวก

อีกด้านหนึ่งก็มีเหมือนกัน คนเราไม่ได้แยกออกเสมอไปว่าตรงหน้าเป็นโค้ดขยะจาก AI หรือไม่ ถ้าไม่ใช่โดเมนของเขา หรือเขาไม่เคยเขียนโค้ดเลย ส่วนที่เปลี่ยนแปลงที่ดูเรียบร้อยก็ดูน่าเชื่อถือ จะเข้าใจก็ต่อเมื่อต้องแก้ฟีเจอร์นั้น หรือเมื่อส่วนนั้นของแอปเริ่มมีบั๊กมากขึ้น

ตอนแรกผมลองแก้ด้วยสกิลเดียว

ไอเดียแรกคือ anti-neuroslop สำหรับ Swift สกิลในที่นี้หมายถึงชุดคำแนะนำที่เอเจนต์โหลดเข้ามาใช้สำหรับงานเฉพาะอย่าง ผมรวบรวมกฎที่ตัวเองอยากเห็นตอนรีวิว เช่น ให้หาการทำงานแบบเดียวกันที่มีอยู่ก่อนจะสร้างใหม่ ใช้ชื่อตามโดเมน ไม่เขียนฟังก์ชันขนาดใหญ่ หลีกเลี่ยงการซ้อนกันที่ยุ่งเหยิงและการจัดการข้อผิดพลาดที่เกินจำเป็น และเพิ่มข้อจำกัดเรื่องชั้นนามธรรมและสถานะใหม่ ๆ

แต่พอพยายามทำให้รายการนี้ใช้ได้ทั่วไป ทุกอย่างก็เริ่มปนกัน ความยาวของเมธอดและกฎการจัดรูปแบบบางส่วน SwiftLint ตรวจอยู่แล้ว พฤติกรรมที่ห้ามพังควรปกป้องด้วยการทดสอบ ส่วนกฎ “อย่าสร้างโปรโตคอลเกินจำเป็น” ในโปรเจกต์จริงก็กลายเป็นเรื่องรสนิยมส่วนตัวอย่างรวดเร็ว บางครั้งโปรโตคอลที่มีการทำงานเพียงแบบเดียวก็ปกป้องขอบเขตที่สำคัญอยู่

ถ้าแยกรายการเดิมของผมออกมาดูทีละข้อ ลักษณะบางส่วนก็ปิดได้ด้วยเครื่องมือ ส่วนที่เหลือยังต้องอาศัยการรีวิวอยู่ดี

สิ่งที่ผมนับเป็นโค้ดขยะจาก AIจำกัดได้ด้วยอะไรสิ่งที่ยังเหลือให้การรีวิว
การซ้อนกันของเงื่อนไขและไทป์ที่ลึกเกินไปSwiftLint cyclomatic_complexity, nestingเงื่อนไขที่ซับซ้อนนั้นมีเหตุผลรองรับจากตัวตรรกะเองหรือไม่
ฟังก์ชันและไทป์ขนาดมหึมาfunction_body_length, type_body_length, file_lengthฟังก์ชันถูกหั่นเป็นชิ้น ๆ ที่แค่ส่งต่อการเรียกหรือเปล่า
do/catch ที่เกินจำเป็นและข้อผิดพลาดที่ถูกกลืนหายไปcustom_rules สำหรับ catch ที่ว่างเปล่าและ try? ในโมดูลที่เลือกไว้จริง ๆ แล้วควรตัดสินชะตาของข้อผิดพลาดที่จุดไหน
ไคลเอนต์เครือข่ายตัวที่สอง การเรียก URLSession.shared โดยตรงcustom_rules, ขอบเขตของทาร์เก็ตใน Swift Packageทำไมเอเจนต์จึงไม่พบไคลเอนต์ที่มีอยู่แล้ว
โค้ดที่คัดลอกซ้ำตัวตรวจจับโค้ดซ้ำ เช่น PMD CPDเป็นกฎเดียวกันที่กระจายไปสองที่ หรือเป็นสองกฎที่ต่างกัน
แฟกทอรีสำหรับสแตรทิจีเดียว ชั้นที่แค่ส่งผ่านแทบทำอัตโนมัติไม่ได้เลยการรีวิวแบบอิสระและความเข้าใจในตัวงาน
โค้ดที่คนอ่านยากอภิธานศัพท์และกฎการตั้งชื่อ ช่วยได้บางส่วนคนที่ไม่ได้เขียนโค้ดนี้อ่านแล้วเข้าใจหรือไม่
ศัพท์คำเดียวมีสามชื่ออภิธานศัพท์ในรีโพซิทอรีการเทียบกับอภิธานศัพท์ตอนรีวิว

ที่ยากเป็นพิเศษคือการเขียนข้อห้ามเรื่องการออกแบบเกินจำเป็น เราขอให้เอเจนต์เลือกวิธีที่ง่ายที่สุดได้ แต่มันก็อธิบายได้อย่างน่าเชื่อว่าทำไมต้องมีตัวจัดการใหม่ทุกตัว ถ้าไม่รู้งานตั้งต้นและโค้ดข้างเคียง ก็ตรวจสอบการประเมินแบบนี้ไม่ได้

ผมเก็บส่วนของไอเดียแรกที่ทำให้เป็นกฎตายตัวอย่างน่าเชื่อถือไม่ได้ไว้ แล้วไปหาวิธีอื่นที่จะลดการเกิดความซับซ้อนเกินจำเป็น

แนวทางที่ผมพบจากคนอื่น

ผมดูการบรรยายหลายเรื่องจากงานประชุม AI Engineer CODE 2026 นอกจากนี้ยังอ่านเนื้อหาของ Matt Pocock และโพสต์ของ Robert C. Martin แยกต่างหาก แนวทางของแต่ละคนค่อนข้างต่างกัน

การบรรยายจาก AI Engineer CODE 2026

ผู้เขียนทำอะไรกับปัญหานี้
Lauren Tanเปลี่ยนกฎที่ซ้ำ ๆ ให้เป็นข้อจำกัดใน API และกราฟการพึ่งพา รวมถึงการตรวจสอบอัตโนมัติ
Dmitry Buykinทำความสะอาดระบบที่ขยายตัวขึ้นอย่างสม่ำเสมอ และตรวจการเปลี่ยนแปลงกับสถานการณ์จริง
Brian Douglasเก็บข้อสังเกตของเพื่อนร่วมงานที่มีประสบการณ์ไว้ เพื่อให้เอเจนต์รุ่นถัดไปไม่ทำผิดซ้ำ

การบรรยายของ Lauren Tan ชื่อ “All Code Is Slop (Except Mine)” แปลว่า “โค้ดทุกชิ้นเป็นขยะ ยกเว้นของฉัน” มุกนี้ตรงมาก โค้ดของตัวเองมักดูเข้าใจง่ายเสมอ ส่วนโค้ดของคนอื่น โดยเฉพาะที่เอเจนต์เขียน มักทำให้อยากเขียนอะไรบางอย่างใหม่

จากการบรรยายของ Lauren Tan ผมจำกรณีของ React useEffect ได้ เอเจนต์ใช้มันเป็นประจำในที่ที่ทำให้เกิดปัญหา ทีมจึงจำกัดการใช้ useEffect โดยตรงในโค้ดของผลิตภัณฑ์ที่สร้างบนเฟรมเวิร์กของพวกเขา และเก็บกรณีที่จำเป็นไว้ภายใน API ที่ควบคุมได้

การนำเข้าที่ผิดระหว่างโพรเซสหลักกับโพรเซสเรนเดอร์ของ Electron ก็เริ่มถูกจับได้ด้วยตัววิเคราะห์การพึ่งพาและตัวตรวจโค้ด (linter) ข้อสังเกตที่ซ้ำ ๆ จึงไม่ต้องพึ่งความละเอียดของผู้รีวิวคนถัดไปอีก

เธอยังมีอีกความคิดหนึ่งที่ดี โค้ดเบสก็เหมือนสวน เอเจนต์หาแบบอย่างจากโค้ดที่มีอยู่ และนอกจากวิธีแก้ที่ดีแล้วก็คัดลอกทางเลี่ยงเก่า ๆ ไปด้วย ถ้าในรีโพซิทอรีมีห้าวิธีที่ทำงานเดียวกัน เอเจนต์ตัวถัดไปก็อาจเพิ่มวิธีที่หกเข้าไปได้ ตอนนี้การดูแลโค้ดเบสส่งผลถึงสิ่งที่เอเจนต์ตัวต่อ ๆ ไปจะสร้างขึ้นด้วย

เธอยังแสดงพีระมิดคว่ำด้วย แรงส่วนใหญ่ควรลงไปที่ชั้นที่ให้ผลแน่นอน คือตัวโค้ด ไทป์ ตัวตรวจโค้ด และการทดสอบ ส่วนที่ควรลงแรงน้อยที่สุดคือกฎที่เป็นข้อความ เมื่อต้องคอยแก้เอเจนต์ การเปลี่ยนข้อสังเกตนั้นให้เป็นการตรวจสอบดีกว่าการเพิ่มอีกบรรทัดลงใน AGENTS.md ผมจัดโปรเจกต์ iOS ทั่วไปลงในพีระมิดนี้

  1. โค้ดAPI ที่เข้าใจง่าย, มีวิธีเดียวในการทำแต่ละงาน, ขอบเขตของทาร์เก็ตใน Swift Package
  2. คอมไพเลอร์และตัวตรวจโค้ดการตรวจที่เข้มงวดของคอมไพเลอร์, SwiftLint, การห้ามการพึ่งพาที่ไม่จำเป็นระหว่างโมดูล
  3. การทดสอบพฤติกรรม, สถานการณ์การใช้งานของผู้ใช้, XCUITest
  4. การรันซิมูเลเตอร์, ภาพหน้าจอ, การบันทึกหน้าจอ
  5. อภิธานศัพท์ศัพท์ของโดเมนและชื่อหน้าจอ
  6. ไฟล์ MDAGENTS.md, สกิล, กฎ
  7. การรีวิวเอเจนต์อิสระ แล้วจึงเป็นคน
ยิ่งชั้นกว้างเท่าไร ก็ยิ่งควรลงแรงกับชั้นนั้นมากเท่านั้น ลำดับนำมาจาก Lauren Tan ส่วนเนื้อหาสำหรับ iOS เป็นของผม

กฎที่เป็นข้อความก็ไม่ได้ราบรื่นสำหรับผมเหมือนกัน เอเจนต์มักเพิกเฉยต่อ AGENTS.md มันจึงไม่ใช่หลักประกัน Lauren Tan สังเกตเห็นผลอีกอย่างด้วย ในเอกสาร ความคิดเห็นในโค้ด และสกิล เอเจนต์เขียนข้อแก้ตัวว่าทำไมไม่ได้ทำอย่างที่ควร และเปลี่ยนการแก้เล็ก ๆ ให้กลายเป็นกฎเคร่งครัด

ทีมของเธอจึงห้ามความคิดเห็นในโค้ดเกือบทั้งหมดในที่สุด มันล้าสมัยเร็ว และเปิดช่องให้เอเจนต์เขียนข้อแก้ตัว ผมยังไม่แน่ใจว่าพร้อมจะไปไกลขนาดนั้น

Buykin อธิบายปัญหาคล้ายกันจากอีกมุมหนึ่ง เขาเขียนระบบคำสั่งทางธุรกิจที่เอเจนต์ดูแลขึ้นใหม่เป็นส่วนใหญ่ แต่ผ่านไปสักพักความซับซ้อนก็กลับมา เพราะกระบวนการที่ทำให้มันเกิดขึ้นยังเหมือนเดิม เขาจึงหันมาทำความสะอาดเล็ก ๆ อย่างสม่ำเสมอและตรวจกับสถานการณ์จริง

Matt Pocock และ Robert Martin

ผู้เขียนทำอะไรกับปัญหานี้
Matt Pocockทำงานกับคลังศัพท์ของโดเมน ตัวอย่างด้านสถาปัตยกรรม และแยกการตรวจตามข้อกำหนดออกจากการตรวจตามมาตรฐานของโปรเจกต์
Robert C. Martinไม่อ่านโค้ดของเอเจนต์ ล้อมพวกมันไว้ด้วยการทดสอบ การทดสอบแบบมิวเทชัน และตัวชี้วัดคุณภาพ

Matt Pocock เขียนไว้ ว่าตัวโค้ดเบสเองส่งผลต่อผลลัพธ์มากกว่าคำสั่งและ AGENTS.md นี่คือความคิดเดียวกับเรื่องสวนของ Lauren Tan ใน README ของสกิลของเขา เขียนไว้ว่าเอเจนต์ไม่ได้เร่งแค่การเขียนโค้ด แต่เร่งเอนโทรปีด้วย เขาระมัดระวังกับการรีวิวโดย AI ข้อสรุปของซับเอเจนต์ผู้รีวิว เขาเสนอให้ถือเป็นสมมติฐาน ส่วนการรีวิวในบริบทเดียวกันเขาเรียกว่า “confirmation bias with a slash command” คือการยืนยันความเชื่อเดิมของตัวเองด้วยคำสั่งสแลช

ผมจะเริ่มจากอะไรในโปรเจกต์ iOS

ในโปรเจกต์ iOS ผมจะเริ่มจากเรื่องที่ติดดินพอสมควร ใน AGENTS.md ผมจะเหลือไว้แค่คำสั่งบิลด์และคำสั่งทดสอบ กับลิงก์ไปยังตัวอย่างการทำงานที่ดี แทนการเล่าสถาปัตยกรรมทั้งหมดซ้ำ ถ้าแอปแบ่งเป็นทาร์เก็ตใน Swift Package อยู่แล้ว ผมจะใช้ขอบเขตการพึ่งพาระหว่างโมดูล การละเมิดที่เกิดซ้ำ ๆ เช่น การเรียก URLSession.shared โดยตรงจากชั้นฟีเจอร์ สามารถจับได้ด้วย SwiftLint ถ้าโปรเจกต์มีกฎแบบนั้นอยู่จริง

ถัดมาคือการทดสอบพฤติกรรม การทดสอบกำหนดไว้ชัดว่าหน้าจอต้องทำอะไร แล้วการลบชั้นที่เกินจำเป็นก็ไม่น่ากลัวอีกต่อไป ถ้าไม่มีการทดสอบ คนจะกลัวแตะต้องมัน และมันก็จะอยู่ไปตลอด ถึงอย่างนั้น การทดสอบที่ผ่านทั้งหมดก็ไม่ได้บอกอะไรเลยว่าสถาปัตยกรรมมีเหตุผลรองรับหรือไม่

หลังการตรวจเหล่านี้ก็ยังเหลือคำถามที่ตัวตรวจโค้ดตอบไม่ได้ จำเป็นต้องมีเซอร์วิสใหม่จริงหรือ มันซ้ำกับตัวที่อยู่ข้าง ๆ หรือเปล่า การปรับแก้ครั้งต่อไปจะต้องใช้แรงแค่ไหน สำหรับคำถามเหล่านี้ ผมตัดสินใจลองการรีวิวสถาปัตยกรรมแบบอิสระ

การทดลองรีวิวแบบอิสระ

ผมทำ independent-feature-review ขึ้นมา ในนั้นเอเจนต์ตัวใหม่จะศึกษาฟีเจอร์ที่เสร็จแล้วด้วยตัวเอง ไล่ตามการเรียก ดูการทำงานที่อยู่ข้างเคียง และอธิบายว่าความซับซ้อนที่พบนั้นสร้างปัญหาอะไรอย่างเจาะจง ในเวอร์ชันแรก มันสำรวจโค้ดก่อนโดยไม่รู้งานตั้งต้น จากนั้นจึงได้รับข้อกำหนดแบบย่อ เหตุผลของเอเจนต์ผู้เขียนโค้ดไม่ได้ถูกส่งให้มัน

ข้อความเต็มของสกิลและคำสั่งสำหรับการรันอยู่ด้านล่าง คัดลอกไปใช้ได้ทันทีโดยไม่ต้องแก้ ในการทดลอง สกิลยังมีอีกส่วนหนึ่งที่เป็นการตรวจสำหรับ Swift และ iOS ที่นี่ผมแทนที่ส่วนนั้นด้วยกฎทั่วไป เพื่อให้สกิลใช้ได้กับชุดเทคโนโลยีใดก็ได้ นี่เป็นแค่การทดลองส่วนตัวของผม และไม่ได้อ้างว่าเป็นความจริงแท้

SKILL.md
---
name: independent-feature-review
description: Independently review an implemented feature for unjustified architecture, overengineering, shallow abstractions, duplicate mechanisms, coupling, and maintenance cost. Inspect the actual repository in a fresh context, then evaluate against a short original task brief. Read-only.
---

# Independent Feature Architecture Review

## Mission
Review the COMPLETE feature like a maintainer encountering it for the first time. Decide whether its structure is proportionate to its actual requirements. Do not act as its author or justify the existing design by default. This is an architectural review, not a formatting, lint, generic bug, or security audit.

## Independence
- Use a fresh reviewer session. Do not consume the implementing agent's chat history, design plan, self-review, or explanation of why the implementation is good.
- User provides feature entry point(s), module, screen, or main symbols. Optionally a base commit to identify new versus existing debt.
- Inspect repository code, nearby modules, callers, tests, project `AGENTS.md`/`CLAUDE.md`, glossary, ADRs, and relevant framework constraints yourself. Project documents are evidence, not unquestionable truth.
- Inspect the entire relevant feature, not only a diff. Expand scope to real callers and similar implementations; avoid scanning unrelated code.
- READ-ONLY. Do not edit files, run formatters, perform refactors, or create files in the repo. Only run safe commands when they add evidence.
- Branch / MR target: read the code at the head revision and compare against the given base with `git diff <base>...<head>` and `git show <base>:<path>`. Do NOT read the MR/PR description, review comments or commit-message rationale before Pass 1 is done; they are the author's reasoning.

## Scope control for large features
- Trace the hot path first (entry -> decisions -> effects -> output), then widen to error handling, tests, debug/test-only code and callers.
- If the feature is large (roughly 40+ files or ~3000+ lines), say up front which parts you will skim and which you will read fully. Never imply coverage you do not have.
- Count tests and snapshots as evidence about maintainability, not as files to read line by line; read the ones that guard the riskiest behavior.

## Pass 1 — blind repository inspection
Do this BEFORE receiving or reading the original task brief or the implementing agent's reasoning.
1. Locate the user's interaction/API entry point and trace: entry -> state and decisions -> effects/persistence/network -> output.
2. Identify key types, protocols, factories, managers, adapters, state owners, error transformations, caches, and public API boundaries.
3. For each extra layer, ask what distinct responsibility it owns, which implementation details it hides, and which real callers depend on it.
4. Inspect existing tests, local conventions, and 1–2 closest analogues in the repository. The old code is evidence, not automatically a good model.
5. Produce a concise feature map and up to five *provisional* friction hypotheses with locations. If any boundary cannot be traced, say so. Do not issue final overengineering verdicts yet.

## Pass 2 — read the minimum original task brief
Now obtain only (a) requested observable behavior, (b) mandatory constraints, and (c) acceptance criteria. Do not take the implementing agent's architecture proposal or PR self-assessment as the original task.
6. Separate necessary complexity (real contracts, concurrency, compatibility, security, offline needs, domain requirements, proven shared variation) from complexity that lacks a demonstrated reason.
7. Challenge every suspected overengineering problem with the strongest argument for keeping it: real independent responsibility, shared consumer, extension contract, isolation boundary, test seam, lifecycle ownership, compatibility, or ADR.
8. Compare the current shape with the SMALLEST behavior-preserving alternative. Specify precisely which types, links or states could disappear, what would remain, and possible downsides.
9. If a diff base is available, distinguish problems introduced or worsened by the change from existing technical debt.

## Investigate these as hypotheses — NEVER automatic violations
- Pass-through wrappers, manager layers or factories that only forward calls.
- Protocols, strategies, generics or plugin systems justified only by imagined future callers. A single conformance or one consumer does NOT automatically make an abstraction bad.
- New networking, persistence, caching, navigation, retry, error or dependency mechanisms parallel to a working shared solution.
- Excessive dependency hops to understand one operation; a small change touches many unrelated files (change amplification).
- Business rules leaking across module boundaries, unnecessary public surface area, inconsistent domain terminology that duplicates concepts.
- Overly nested control flow, redundant error layers, unnecessary state ownership, pre-emptive optimizations or speculative offline sync.
- Unhelpful decomposition into tiny methods hiding a straightforward flow, or excessively broad objects owning unrelated decisions.
- Tests mirroring implementation rather than guarding observable behavior, when this affects maintainability.

## Platform and framework checks, when applicable
- Follow the layer and state boundaries THIS project already uses; do not impose a generic MVVM/Clean Architecture template.
- Examine module or package boundaries and dependency direction, access control, the concurrency and isolation model, state lifecycle, cancellation, and what the platform or framework already provides.
- Ask whether existing framework features, structured concurrency, cancellation, existing services and platform APIs already handle the problem before accepting a new mechanism. Do not assume that cancelling a task alone prevents every stale update.
- Preserve real interfaces for test seams, SDK contracts or isolation boundaries even if there is one implementation.
- Do not recommend combining mutable state, caches or services across feature or concurrency boundaries without verifying safety.
- Linter and compiler warnings are useful evidence, not a substitute for architectural judgement.

## Evidence and false-positive discipline
Report a finding ONLY if you can show current maintenance cost: unnecessary state or failure modes, duplicated policy, hard-to-trace call flow, coupling, or amplification of a concrete change. File length, number of types, unfamiliar architecture or stylistic preference are not evidence by themselves.

For each finding provide:
- **Where:** exact file and symbol/line, plus relevant callers.
- **Cost today:** the concrete behavior/change that is harder.
- **Evidence:** code references, tests, existing solution, constraints or contracts.
- **Best counterargument:** why the structure might be justified.
- **Minimal safe improvement:** what can change without losing behavior; what must remain.
- **Impact + confidence:** separate high/medium/low judgements.
- **Origin:** introduced/worsened versus pre-existing (if comparison provided).

Put uncertain observations in `Questions/unknowns`, not in actionable findings. A finding that is both low impact and low confidence belongs in `Questions/unknowns`. Zero findings is valid. Do not invent an architecture refactor just to produce output.

Duplication counts as a finding only if you can name the concrete change that must then be made twice, or the divergence that already exists. Debug/test-only code on a production path counts only if it widens the production surface or sits in a security-critical spot; verify how it is compiled out before claiming a leak.

## Outside the architecture lens
While reading you may notice a correctness, security, compatibility or flag-off/rollback risk. Do not turn it into an architecture finding and do not audit for more. List it in one line each under `Outside the architecture lens`, mark it `unverified` unless you traced it, and say how to verify it. These are often worth more than the findings, so do not drop them.

## Final report (concise, in user's language)
1. **Feature map:** entry points, flow, actual boundaries (one short paragraph).
2. **Coverage:** read fully / skimmed or grep only / not inspected. Mandatory.
3. **Verdict:** `proportionate / some unnecessary complexity / significant unnecessary complexity / insufficient evidence`.
4. **Findings:** zero to five, ordered by practical impact using evidence format above.
5. **Keep:** noteworthy complexity that is justified, if relevant.
6. **Minimal alternative:** optional before/after flow showing fewer levels/decisions without dropping requirements; avoid sweeping rewrites.
7. **Outside the architecture lens:** see above, or `none noticed`.
8. **Handoff for implementer:** see below, or `No change recommended`.
9. **Questions/unknowns.**

## Handoff for implementer
A block that a DIFFERENT agent or engineer can paste into a fresh implementation session without reading the rest of the report. For each finding worth acting on:
- **Action class:** `safe now` (behavior-preserving and covered by existing tests) / `needs decision` (product, backend or owner input required; say who and what) / `leave alone`.
- **Change:** the smallest edit, by file and symbol.
- **Must not change:** behavior, public API, test seams, flags, wire formats and tests that must stay green.
- **Verify:** the narrowest tests/commands to run, and any test to add first.
- **Stop and ask if:** the signal that the assumption was wrong.
Order items by safety, safest first. Never mark `safe now` something you could not trace end to end. Do not perform the changes yourself.

No code edits without a separate user request.
PROMPTS.md
# Running an independent feature review

Use a NEW chat/session on the same source revision, with read access to the real repository. Do not supply the implementing agent's conversation, architecture plan, self-review, or PR pitch. For a truly blind first pass use these TWO messages in order.

## Message A — send first

Use the `independent-feature-review` skill in read-only mode. You are a separate architecture reviewer, not the implementer. Review the COMPLETE feature, not just a recent diff. Read the relevant repository code, conventions, ADRs, callers and tests yourself.

Feature entry point(s): `<path / View / feature module / public API>`
Optional comparison base: `<branch or commit>`

Perform ONLY Pass 1: reconstruct actual behavior and architecture, identify up to five preliminary suspects with locations, and explain what each component appears to own. Do not propose refactors or issue final overengineering findings until I send the original task. No file changes.

## Message B — send after Pass 1

Original behavior requested by the product/task:
`<short description of the user-visible result>`

Mandatory constraints:
`<business, security, performance, compatibility, offline and concurrency constraints, or 'none known'>`

Acceptance criteria:
`<observable behavior to preserve>`

Perform Pass 2 of `independent-feature-review`. Evaluate whether the current architecture is proportionate. Pay special attention to unjustified abstractions, unnecessary state, duplicate mechanisms, overly wide responsibilities and change amplification. For every finding provide code evidence, the strongest reason to keep the design, and the smallest safe alternative. If no meaningful issue exists, say so. Do not edit files.

## One-message version (faster, less blind)

Use `independent-feature-review` to review feature `<path/symbol>`. Original behavior: `<short brief>`. Inspect the repository independently (not the implementation agent's plan), trace the complete feature and find evidence-backed unnecessary architecture. No code changes. Report only actionable findings; zero is fine.

## Branch / MR version

Use `independent-feature-review` read-only. Repository: `<path to checkout>` at `<head branch>`; comparison base `<base branch>`. Do NOT read the MR description or comments. Review the COMPLETE feature starting from `<entry points>`, separating what the change introduced from what was already there. Original behavior: `<brief>`. Mandatory constraints: `<list>`. Include Coverage, Outside the architecture lens and Handoff for implementer.

## Handoff to the implementer (second session)

Take the `Handoff for implementer` block of the report, paste it into a fresh implementer session, and add: apply only `safe now` items, one at a time, run the listed tests after each, and ask me about every `needs decision` item. Do not give the implementer the reviewer's prose, only the block.

## Context to give and withhold

Give: actual repository, feature locator, original small behavior brief, real mandatory constraints, and optional comparison base.

Withhold: author reasoning, implementation chat, author’s PR summary as a substitute for original requirements, original design proposal, previous positive AI-review. Independence is NOT isolation from the real repo or the task.

สำหรับการตรวจ ผมใช้ Apple Food Truck ซึ่งเปิดเป็นสาธารณะ นี่คือโปรเจกต์ตัวอย่าง SwiftUI ของ Apple ที่ผมไม่ถือว่าเป็นตัวอย่างของโค้ดที่สร้างโดย AI ในฟีเจอร์คำสั่งซื้อต้นฉบับ ผู้รีวิวแบบปกติและผู้รีวิวที่มีสกิลเห็นความไม่สอดคล้องในการจัดการสถานะชุดเดียวกัน แต่ในนั้นไม่มีการออกแบบเกินจำเป็นที่ชัดเจน และการรันแบบนี้ก็แทบไม่ได้บอกอะไรเลยเกี่ยวกับประโยชน์ของตัวสกิล

จากนั้นผมจึงสร้างแบรนช์ทดสอบที่รู้คำตอบอยู่ก่อนแล้ว งานนั้นเรียบง่าย คำสั่งซื้อต้องไม่หายไปเมื่อปิดแล้วเปิดแอปใหม่ และคำสั่งซื้อที่เสร็จแล้วต้องเก็บเวลาที่เสร็จไว้ด้วย ทุกอย่างเก็บไว้ในเครื่อง ไม่มีแบ็กเอนด์และไม่มีการซิงโครไนซ์ เอเจนต์เป็นผู้สร้างแบรนช์เหล่านี้ และมีการจงใจใส่ความซับซ้อนเกินจำเป็นลงไปเพื่อให้มีอะไรให้หา ข้าง ๆ กันยังเหลือความซับซ้อนที่มีเหตุผลรองรับไว้ด้วย เช่น การเขียนไฟล์แบบอะตอมมิก การย้ายรูปแบบข้อมูลเก่า คิวการบันทึก และการสลับนาฬิกาในการทดสอบ ผู้รีวิวไม่เห็นว่าอะไรถูกเพิ่มเข้ามาเพื่อการทดลองโดยเฉพาะ

ในแบรนช์แรก การออกแบบเกินจำเป็นเห็นได้ชัด บางครั้งมีความคิดเห็นในโค้ดแบบ “เผื่ออนาคต” ด้วย ในแบรนช์ที่สอง ความซับซ้อนละเอียดกว่านั้นและไม่มีความคิดเห็นแบบนี้

รวมแล้วได้การรีวิว 23 ครั้งจากโมเดล Claude สี่ตัว ทั้งแบบมีสกิลและไม่มีสกิล ในแบรนช์แรก แต่ละเงื่อนไขรันด้วย Fable, Opus, Sonnet และ Haiku อย่างละครั้ง ในแบรนช์ที่สองเพิ่มการรัน Sonnet ซ้ำอีกสองครั้ง และการรัน Sonnet อีกสามครั้งด้วยคำสั่งปกติที่เพิ่มบรรทัดเรื่องความเสี่ยงเข้าไปหนึ่งบรรทัด การรันที่มีสกิลใช้คำสั่งแบบขั้นตอนเดียว ไม่ใช่ขั้นตอนแบบสองรอบที่อธิบายไว้ในสกิล สิ่งที่พบจะนับก็ต่อเมื่อรายงานระบุปัญหาและเสนอให้ตัดส่วนที่เกินออก การพูดถึงผ่าน ๆ หรือการแนะนำให้คงชั้นนั้นไว้ไม่นับ คอลัมน์สุดท้ายคือการประเมินของผมว่าส่วนนี้จะทำให้การปรับแก้ครั้งต่อ ๆ ไปยากขึ้นแค่ไหน

สิ่งที่เพิ่มเข้าไปมีสกิลไม่มีสกิลขัดขวางฟีเจอร์ใหม่
สแตรทิจีและแฟกทอรีที่มีตัวเลือกเดียว4 จาก 44 จาก 4มาก
โมเดลคำสั่งซื้อตัวที่สอง “ตั๋วครัว”3 จาก 42 จาก 4มาก
การซิงโครไนซ์ ทั้งที่ไม่มีแบ็กเอนด์3 จาก 44 จาก 4มาก
แคชซ้อนบนข้อมูลที่อยู่ในหน่วยความจำอยู่แล้ว4 จาก 44 จาก 4มาก
ข้อผิดพลาดสามชั้น4 จาก 44 จาก 4ชัดเจน
กลไกค้นหาตัวที่สอง4 จาก 44 จาก 4ชัดเจน
รีโพซิทอรีที่แค่ส่งผ่าน3 จาก 43 จาก 4ปานกลาง
คลาสที่แค่ส่งผ่าน แบรนช์ที่สอง5 จาก 62 จาก 6น้อย
โปรโตคอลเพื่อไทป์เดียว แบรนช์ที่สอง5 จาก 65 จาก 6น้อย

โค้ดของแต่ละตัวอย่างเปิดดูได้ด้านล่าง โค้ดถูกตัดให้สั้นลง แต่ไม่ได้เขียนใหม่

สแตรทิจีและแฟกทอรีที่มีตัวเลือกเดียว
/// Different trucks may run different workflows (for example, catering orders
/// that require a pickup confirmation), so the rule is injected rather than
/// hard-coded in the model.
public protocol OrderStatusTransitionStrategy {
    func complete(_ ticket: KitchenTicket, at date: Date) throws -> KitchenTicket
}

public struct DefaultOrderStatusTransitionStrategy: OrderStatusTransitionStrategy {
    public func complete(_ ticket: KitchenTicket, at date: Date) throws -> KitchenTicket {
        guard !ticket.isClosed else {
            throw OrderDomainError.invalidTransition
        }
        var completed = ticket
        completed.stage = .handedOff
        completed.closedAt = date
        return completed
    }
}

public enum OrderWorkflow: String, CaseIterable, Sendable {
    case standard
}

public enum OrderStatusTransitionStrategyFactory {
    public static func makeStrategy(for workflow: OrderWorkflow = .standard) -> OrderStatusTransitionStrategy {
        switch workflow {
        case .standard:
            return DefaultOrderStatusTransitionStrategy()
        }
    }
}

เหตุผลเขียนไว้ตรง ๆ ในความคิดเห็นในโค้ด ว่ารถขายอาหารแต่ละคันอาจทำงานตามขั้นตอนที่ต่างกัน แต่รถแบบนั้นไม่มีอยู่จริง ตัวกฎการปิดคำสั่งซื้อยาวแค่บรรทัดเดียว แต่ตอนนี้การเปลี่ยนกฎนี้ทุกครั้งต้องผ่านโปรโตคอล ตัวที่ทำตามโปรโตคอล และแฟกทอรี

โมเดลคำสั่งซื้อตัวที่สอง "ตั๋วครัว"
public struct KitchenTicket: Identifiable, Equatable {
    public enum Stage: Int, Comparable {
        case queued
        case inProgress
        case awaitingPickup
        case handedOff
        // ...
    }

    public var id: Order.ID
    public var stage: Stage
    public var itemCount: Int
    public var openedAt: Date
    public var closedAt: Date?
    // ...
}

public enum OrderTicketMapper {
    public static func apply(_ ticket: KitchenTicket, to order: inout Order) {
        switch ticket.stage {
        case .queued:
            order.status = .placed
        case .inProgress:
            order.markAsPreparing()
        case .awaitingPickup:
            order.status = .ready
        case .handedOff:
            order.markAsComplete()
        }
    }

    static func stage(for status: OrderStatus) -> KitchenTicket.Stage {
        switch status {
        case .placed:
            return .queued
        case .preparing:
            return .inProgress
        case .ready:
            return .awaitingPickup
        case .completed:
            return .handedOff
        }
    }
    // ...
}

// FoodTruckModel.markOrderAsCompleted
let ticket = OrderTicketMapper.ticket(from: order)
let completedTicket = try orderTransitionStrategy.complete(ticket, at: .now)
OrderTicketMapper.apply(completedTicket, to: &orders[index])
orderCache.invalidate()

เพื่อปิดคำสั่งซื้อ โมเดลแปลงมันเป็นตั๋ว ส่งผ่านสแตรทิจี แล้วแปลงกลับ ที่จริงตรงนี้ต้องการแค่การเรียก markAsComplete() ครั้งเดียว สถานะคำสั่งซื้อใหม่จะต้องเพิ่มทั้งใน OrderStatus ใน KitchenTicket.Stage และใน switch ทั้งสองตัวของตัวแปลง คำว่า “ตั๋ว” ไม่มีที่ไหนในโปรเจกต์อีกเลย

การซิงโครไนซ์ ทั้งที่ไม่มีแบ็กเอนด์
/// Coordinates pending order changes and flushes them to the repository.
///
/// Changes are queued and coalesced, then written in the background. Failed
/// writes are retried with exponential backoff so a transient failure (for
/// example, the device being low on space for a moment) doesn't drop data, and
/// the queue keeps working offline until a write succeeds.
public actor OrderSyncCoordinator {
    public enum SyncState: Equatable {
        case idle
        case syncing
        case waitingToRetry(attempt: Int)
        case failed(OrderDomainError)
    }

    public private(set) var state: SyncState = .idle
    private var pendingChanges: [PendingChange] = []
    // ...

    private func write(_ change: PendingChange) async -> Bool {
        var attempt = 0
        while true {
            do {
                try await repository.save(change.orders)
                lastSyncDate = .now
                return true
            } catch {
                attempt += 1
                guard attempt <= maxRetryCount else {
                    state = .failed(OrderDomainError(error))
                    return false
                }
                state = .waitingToRetry(attempt: attempt)
                let delay = baseRetryDelay * pow(2, Double(attempt - 1))
                try? await Task.sleep(nanoseconds: .secondsToNanoseconds(delay))
            }
        }
    }
}

ในงานเขียนไว้ชัดเจนว่าไม่มีแบ็กเอนด์และไม่มีการซิงโครไนซ์ แต่การบันทึกทั้งหมดกลับผ่านคิวที่มีการลองใหม่และสถานะสี่แบบ ถ้าจะเปลี่ยนอะไรในการบันทึก ต้องเข้าใจก่อนว่าคิวอยู่ในสถานะไหน และจะเขียนซ้ำเมื่อไร

แคชซ้อนบนข้อมูลที่อยู่ในหน่วยความจำอยู่แล้ว
@MainActor
public final class OrderCache {
    public let timeToLive: TimeInterval
    private var entries: [Order.ID: Order] = [:]
    private var orderedIDs: [Order.ID] = []
    private var lastRefresh: Date?
    // ...

    public func orders(refreshingFrom source: () -> [Order]) -> [Order] {
        if !isFresh {
            store(source())
        }
        return orderedIDs.compactMap { entries[$0] }
    }

    public func invalidate() {
        entries.removeAll()
        orderedIDs.removeAll()
        lastRefresh = nil
    }
}

// FoodTruckModel, where orders already live in memory
@Published public var orders: [Order] = []
private let orderCache = OrderCache()  // timeToLive defaults to 30 seconds

public func order(id: Order.ID) -> Order? {
    orderCache.order(id: id) { orders }
}

public var incompleteOrders: [Order] {
    orderCache.orders { orders }.filter { $0.status != .completed }
}

คำสั่งซื้ออยู่ในหน่วยความจำใน orders อยู่แล้ว แคชเก็บซ้ำอีกชุดและอัปเดตทุก 30 วินาที คำสั่งซื้อใหม่จะปรากฏใน incompleteOrders ก็ต่อเมื่อแคชหมดอายุแล้ว ทุกเส้นทางใหม่ของการอ่านหรือการเขียนต้องไม่ลืมเรียก invalidate() ไม่อย่างนั้นหน้าจอจะแสดงข้อมูลเก่า

ข้อผิดพลาดสามชั้น
public enum OrderStoreError: Error, Equatable {
    case readFailed
    case writeFailed
    case corruptedData
    case unknown
}

public enum OrderRepositoryError: Error, Equatable {
    case failed
    case unknown
}

// DefaultOrderRepository
public func fetchOrders() async throws -> [Order] {
    do {
        return try await store.loadOrders()
    } catch {
        throw OrderRepositoryError.failed
    }
}

public enum OrderDomainError: Error, Equatable {
    case invalidTransition
    case persistenceFailed
    case unknown

    public init(_ error: Error) {
        switch error {
        case let error as OrderDomainError:
            self = error
        case is OrderRepositoryError:
            self = .persistenceFailed
        default:
            self = .unknown
        }
    }
}

แต่ละชั้นจับข้อผิดพลาดแล้วแทนด้วยข้อผิดพลาดของตัวเอง โดยไม่เก็บสาเหตุเดิมไว้ ไฟล์เสียและการเขียนที่ล้มเหลวไปถึงหน้าจอในรูปเดียวกัน คือเป็น persistenceFailed ข้อผิดพลาดชนิดใหม่จะต้องส่งผ่านไทป์ข้อผิดพลาดสามตัว และตอนหาสาเหตุของบั๊ก สาเหตุก็หายไปแล้ว

กลไกค้นหาตัวที่สอง
// Order, already in the project
public func matches(searchText: String) -> Bool {
    if id.localizedCaseInsensitiveContains(searchText) {
        return true
    }
    // Search donuts...
    return false
}

// OrdersView, the orders list
order.matches(searchText: searchText) || order.donuts.contains(where: { $0.matches(searchText: searchText) })

// Added next to it, used only by the orders table
public struct OrderSearchIndex {
    private var tokensByOrderID: [Order.ID: Set<String>] = [:]
    // ...

    public func matches(orderID id: Order.ID, query: String) -> Bool {
        let queryTokens = Self.tokenize(query)
        guard !queryTokens.isEmpty else {
            return true
        }
        guard let tokens = tokensByOrderID[id] else {
            return false
        }
        return queryTokens.allSatisfy { queryToken in
            tokens.contains { $0.hasPrefix(queryToken) }
        }
    }
}

// OrdersTable
let searchIndex = OrderSearchIndex(orders: model.orders)

รายการคำสั่งซื้อค้นหาผ่าน matches ตัวเดิม ส่วนตารางค้นหาผ่านดัชนีใหม่ ดัชนีแยกตัวพิมพ์เล็กกับพิมพ์ใหญ่และค้นเฉพาะต้นคำ คำค้นเดียวกันจึงให้ผลต่างกันในรายการกับในตาราง ช่องข้อมูลใหม่สำหรับการค้นหาจะต้องเพิ่มในสองที่

รีโพซิทอรีที่แค่ส่งผ่าน
public protocol OrderRepository {
    func fetchOrders() async throws -> [Order]
    func save(_ orders: [Order]) async throws
    func deleteAll() async throws
}

public struct DefaultOrderRepository: OrderRepository {
    private let store: OrderPersisting

    public func fetchOrders() async throws -> [Order] {
        do {
            return try await store.loadOrders()
        } catch {
            throw OrderRepositoryError.failed
        }
    }

    // save and deleteAll repeat the same pattern
}

ทุกเมธอดแค่เรียกที่เก็บข้อมูลแล้วเปลี่ยนข้อผิดพลาด การทำงานใหม่กับที่เก็บข้อมูลต้องเพิ่มอีกสองที่ คือในโปรโตคอลและในตัวที่ทำตามโปรโตคอล

คลาสที่แค่ส่งผ่าน แบรนช์ที่สอง
public final class OrdersPersistenceController: Sendable {
    private let store: any OrderPersisting
    private let saveQueue: OrderSaveQueue
    private let migrator: LegacyOrdersMigrator?
    // init omitted

    @discardableResult
    public func migrateLegacyOrdersIfNeeded() async throws -> LegacyOrdersMigrator.Outcome {
        try await migrator?.migrateIfNeeded() ?? .nothingToMigrate
    }

    public func load() async throws -> [Order] {
        try await store.all()
    }

    @discardableResult
    public func save(_ orders: [Order]) -> Task<Void, Never> {
        saveQueue.enqueue(orders)
    }
}

คลาสนี้ไม่มีตรรกะของตัวเอง เป็นแค่ขั้นตอนเกินระหว่างโมเดลกับที่เก็บข้อมูล ผู้รีวิวที่ไม่มีสกิลมักบอกว่ามันแค่ส่งต่อการเรียก แต่แนะนำให้คงไว้

โปรโตคอลเพื่อไทป์เดียว แบรนช์ที่สอง
public protocol Repository {
    associatedtype Entity
    func all() async throws -> [Entity]
    func replaceAll(_ entities: [Entity]) async throws
}

public protocol OrderPersisting: Repository, Sendable where Entity == Order {}

การทำให้เป็นแบบทั่วไปนี้ใช้กับคำสั่งซื้อเท่านั้น ขัดขวางไม่มาก แค่เพิ่มชั้นนามธรรมอีกชั้นเวลาอ่านโค้ด

ถ้าดูจากต้นทุนของการปรับแก้ สิ่งที่ขัดขวางมากที่สุดคือตั๋วกับสแตรทิจี การซิงโครไนซ์ และแคช กฎสถานะใหม่จะต้องส่งผ่านไทป์สี่ตัว ส่วนแคชทำให้เสี่ยงได้ข้อมูลเก่าทุกครั้งที่ปรับแก้ ผู้รีวิวพบส่วนเหล่านี้แทบทุกครั้ง ความต่างเมื่อมีสกิลเห็นได้แค่ที่คลาสที่แค่ส่งผ่านในแบรนช์ที่สอง ซึ่งแทบไม่ขัดขวางฟีเจอร์ใหม่ ด้วยการรันเพียงหกครั้ง เรื่องนี้แทบพิสูจน์อะไรไม่ได้

ในแบรนช์ที่สองยังมีกฎ “วันนี้” สองแบบที่ต่างกัน แบบหนึ่งนับตาม UTC อีกแบบนับตามเวลาท้องถิ่น ความขัดแย้งนี้ถูกพบในการรันทั้ง 15 ครั้งของแบรนช์ที่สอง การชี้ปัญหาที่ไม่ใช่ปัญหาจริงแทบไม่มีเลย มีผู้รีวิวที่ไม่มีสกิลเพียงคนเดียวที่เรียกเวอร์ชันของสคีมาข้อมูลว่าเกินจำเป็น ทั้งที่มีรูปแบบข้อมูลอยู่แล้วสองแบบและการย้ายข้อมูลเป็นสิ่งที่ต้องทำ

ระหว่างนั้นผู้รีวิวยังพบบั๊กธรรมดาที่เอเจนต์ใส่เข้ามาโดยไม่ตั้งใจตอนเตรียมแบรนช์ด้วย นี่เป็นอีกปัญหาหนึ่ง ไม่ใช่โค้ดขยะจาก AI ผมจึงไม่ลงรายละเอียดเรื่องนี้ตรงนี้

การทดลองนี้มีข้อจำกัดหลักอยู่ข้อหนึ่ง โค้ดขยะจาก AI เกือบทั้งหมดในแบรนช์ทดสอบถูกใส่เข้าไปโดยตั้งใจ และรู้คำตอบอยู่ก่อนแล้ว ความไม่สอดคล้องในโค้ดต้นฉบับของ Apple เป็นของจริง แต่ไม่ใช่โค้ดขยะจาก AI ปัญหาที่ไม่ได้ตั้งใจใส่มีแค่บั๊ก ไม่ใช่สถาปัตยกรรมที่เกินจำเป็น การทดลองนี้จึงไม่ได้บอกว่าผู้รีวิวจะรับมือกับโค้ดขยะจาก AI ที่เอเจนต์เขียนขึ้นเองในโปรเจกต์จริงได้แค่ไหน

คะแนนให้โดยโมเดล Claude ตัวอื่น ข้อสังเกตบางส่วนยืนยันจากซอร์สโค้ด ไม่ได้รันแอปบน iOS จริงอย่างเต็มรูปแบบ ด้วยรีโพซิทอรีเดียวและจำนวนการรันซ้ำเท่านี้ พิสูจน์ไม่ได้ว่าสกิลดีกว่า

แต่อย่างน้อยผมก็เข้าใจชัดขึ้นว่าต้องการอะไรจากผู้รีวิวอิสระ มันต้องปกป้องความซับซ้อนที่มีเหตุผลรองรับได้ บอกผลกระทบที่เจาะจงของชั้นนามธรรมที่เกินจำเป็น และบอกตรง ๆ ว่าอะไรที่ยังไม่ได้ตรวจ

ยังต้องมีคนในการรีวิวไหม

เรื่องคนในการรีวิวเป็นจุดที่ความเห็นแตกต่างกันมากที่สุด

คนไม่อ่านโค้ด
  • Robert C. Martinตรวจการทดสอบ การพึ่งพา และตัวชี้วัดความซับซ้อน
  • Lauren Tanดูวิดีโอ ภาพหน้าจอ และแผนภาพ
คนอ่านโค้ด
  • Brian Douglasเพื่อนร่วมงานที่มีประสบการณ์เจอสิ่งที่การรีวิวโดย AI มองข้ามไป
  • มุมมองของผมยังต้องมีคนสำหรับข้อตกลง กฎทางธุรกิจ และขอบเขตด้านสถาปัตยกรรม

Robert C. Martin เขียนว่า เขาไม่รีวิวโค้ดของเอเจนต์ เขาดูความครอบคลุมของการทดสอบ โครงสร้างการพึ่งพา ความซับซ้อนแบบไซโคลแมติก ขนาดของโมดูล และการทดสอบแบบมิวเทชัน ส่วนตัวโค้ดเขาปล่อยให้ AI ถึงอย่างนั้น ตามที่เขาบอก ความรับผิดชอบทั้งหมดยังอยู่ที่วิศวกร และคนที่ตรวจสถาปัตยกรรมก็คือพวกเขา

สำหรับ Lauren Tan การรีวิวลดเหลือเพียงการดูหลักฐานจากการตรวจสอบ เช่น วิดีโอ ภาพหน้าจอ และแผนภาพ

Brian Douglas เล่าว่าเอเจนต์เขียนโค้ด Rust ที่คอมไพล์ผ่านและได้ผลรีวิวจาก AI ในแง่บวก แต่เพื่อนร่วมงานที่มีประสบการณ์กลับพบว่ามันละเมิดแนวปฏิบัติของทีม

หลายคนอยากได้ระบบอัตโนมัติให้มากที่สุด และผมก็เข้าใจ แต่ผมยังคิดว่าตอนนี้การรีวิวยังต้องมีคนอยู่ โดยเฉพาะกับข้อตกลงที่สำคัญ กฎทางธุรกิจที่ซับซ้อน และขอบเขตด้านสถาปัตยกรรม ผมยอมรับว่าส่วนหนึ่งอาจเป็นความต้องการส่วนตัวที่อยากควบคุมโค้ดเอง

ผมมองว่าการรีวิวโดยคนยังมีประโยชน์ในฐานะแหล่งความรู้เกี่ยวกับโปรเจกต์ด้วย ข้อสังเกตของเพื่อนร่วมงานที่มีประสบการณ์อธิบายว่าทำไมที่นี่ไม่ทำแบบนี้ และความรู้นี้หายไปได้ง่ายใน Pull Request ที่ปิดไปแล้ว Brian Douglas จึงทำเครื่องหมายเซสชันที่คนไม่เห็นด้วยกับเอเจนต์ ผมก็จะรวบรวมข้อสังเกตแบบนี้ไว้ในที่เดียวเหมือนกัน ข้อที่เกิดซ้ำก็เปลี่ยนเป็นการตรวจสอบได้ในภายหลัง ส่วนที่เหลือจะเป็นบริบทที่มีประโยชน์สำหรับผู้รีวิวคนถัดไป

ผมได้ข้อสรุปอะไร

ผมจะเริ่มรับมือกับโค้ดขยะจาก AI จากปัญหาที่เกิดซ้ำในโปรเจกต์นั้น ๆ ถ้าเอเจนต์สร้างไคลเอนต์เครือข่ายตัวที่สองอยู่เรื่อย ๆ ก็ควรหาให้เจอว่าทำไมมันไม่พบตัวแรก แก้ที่สาเหตุ และถ้าทำได้ก็เพิ่มการตรวจสอบเข้าไป

จากการทดลองของผม สกิลแยกต่างหากน่าจะไม่จำเป็น การออกแบบเกินจำเป็นที่เห็นได้ชัด การรีวิวแบบปกติก็หาเจอ ส่วนความต่างเมื่อมีสกิลเห็นได้แค่ในเรื่องเล็ก ๆ น้อย ๆ กลุ่มตัวอย่างเล็ก นี่จึงยังไม่ใช่คำตอบสุดท้าย ข้อความของสกิลและคำสั่งต่าง ๆ อยู่ด้านบนในส่วนของการทดลอง และยังรวมไว้ในไฟล์บีบอัดไฟล์เดียว

ผมคิดว่าการกำจัดโค้ดขยะจาก AI ให้หมดไปเลยคงเป็นไปไม่ได้ โค้ดที่เข้าใจยากจะเกิดขึ้นเสมอ และถ้าเลิกคอยดูแล โค้ดขยะจาก AI ก็จะค่อย ๆ เป็นฝ่ายชนะ การลดมันจึงกลายเป็นงานดูแลโปรเจกต์ตามปกติ ซึ่งในแต่ละขั้นก็ทำให้เป็นอัตโนมัติได้มากขึ้นอีกนิด สิ่งที่เกิดซ้ำจะถูกรับไปโดยตัวตรวจโค้ด การทดสอบ ข้อจำกัดการพึ่งพา และการทำความสะอาดโค้ดเก่า ส่วนการตัดสินใจที่ยังถกเถียงกันได้ ก็ยังเหลือการรีวิวแบบอิสระและคน

เหมือนกับการป้องกันวิศวกรรมย้อนกลับ โค้ดขยะจาก AI AI ชนะเสมอ เราทำได้แค่ทำให้งานของมันยากขึ้น

บทความที่เกี่ยวข้อง