skipLink.label

Quest 36 - Code Migration Assistant

Quest 36: Code Migration Assistant

hard 25 minutes

🎯 Learning Objectives

  • ✅ เข้าใจว่า AI ช่วยแปลงโค้ดระหว่างภาษาได้ยังไง
  • ✅ ระบุ semantic equivalence — โค้ดที่ต่าง syntax แต่ logic เดียวกัน
  • ✅ จัดการ idioms ที่ต่างกันระหว่างภาษา
  • ✅ เรียนรู้ว่า code migration ไม่ใช่แค่ syntax translation

📖 Concept: Code Migration ด้วย AI

Code migration คือการแปลงโค้ดจากภาษาหนึ่งไปอีกภาษาหนึ่ง — เช่น Python → JavaScript, Java → TypeScript

นี่คือหนึ่งใน use case ที่ AI ช่วยได้ดีมาก เพราะ AI สามารถจับ semantic meaning ของโค้ดได้ — ไม่ใช่แค่แปล syntax แต่เข้าใจว่าโค้ด ทำอะไร

เหมือนนักพากย์หนัง — ไม่ได้แปล word-for-word แต่ถ่ายทอด “ความหมาย” และ “อารมณ์” ไปอีกภาษาหนึ่ง


⚙️ How It Works

ขั้นตอนของ Code Migration

1. PARSE — วิเคราะห์ source code
↓
2. UNDERSTAND — จับ logic และ data flow
↓
3. MAP — หา equivalent ใน target language
↓
4. TRANSLATE — แปลงโค้ด
↓
5. VERIFY — ทดสอบว่า logic เหมือนเดิม

ความท้าทายที่ AI ต้องจัดการ

ความท้าทายตัวอย่าง
Data typesPython list → JS Array
Error handlingPython try/except → JS try/catch
String formattingPython f-string → JS template literal
Null handlingPython None → JS null / undefined
Async patternsPython async/await → JS Promise

💡 Example: Python → JavaScript

Python ต้นฉบับ:

def greet_users(users):
results = []
for user in users:
if user.get('active', False):
name = user['name'].strip().title()
results.append(f"Hello, {name}!")
return results

JavaScript ที่ AI แปลง:

function greetUsers(users) {
const results = [];
for (const user of users) {
if (user.active === true) {
const name = user.name.trim().replace(/\b\w/g, c => c.toUpperCase());
results.push(`Hello, ${name}!`);
}
}
return results;
}

สิ่งที่ AI จัดการได้ดี:

  • for...in → for...of (idiomatic loop)
  • f-string → template literal
  • .get('active', False) → user.active === true
  • .title() → manual implementation (JS ไม่มี .title())

สิ่งที่ AI อาจพลาด:

  • Edge case: user.name เป็น undefined
  • .title() behavior ต่างกันเล็กน้อยกับ regex replacement

⚠️ Common Mistakes

Mistake 1: 信任 AI 100% โดยไม่ test

“AI แปลงเสร็จแล้ว น่าจะใช้ได้” → ทดสอบ logic เทียบกับต้นฉบับเสมอ — โดยเฉพาะ edge cases

Mistake 2: ไม่รู้ target language idioms

“แปลง syntax ตรงๆ ก็พอ” → โค้ดที่ syntax ถูกแต่ไม่ idiomatic จะ maintain ยาก — เช่น ใช้ for loop ทั้งที่ใช้ .map() ได้

Mistake 3: ไม่จัดการ error handling

“logic หลักถูกก็พอ” → Error handling ต่างกันมาก giữa languages — ต้อง convert ด้วย

Mistake 4: ไม่ verify data types

“Variable ชื่อเดียวกัน น่าจะ type เดียวกัน” → Type systems ต่างกัน — Python duck typing ≠ TypeScript strict types


📝 Knowledge Check

📝 Knowledge Check

Q1:Code migration ด้วย AI ควรเริ่มจากอะไร?

Q2:ความท้าทายไหนที่ AI มักจะพลาดในการ code migration?

Q3:หลัง AI แปลงโค้ดเสร็จ สิ่งสำคัญที่สุดที่ต้องทำคืออะไร?


🏋️ Quest: Code Migration Assistant

พร้อมแล้วแปลงโค้ด! ใช้ AI ช่วย migration แต่ verify ด้วยตัวเอง

  1. Download ไฟล์เริ่มต้นของ quest:

    Terminal window
    npx bluebeltdojo download quest-36-code-migration
    cd quest-36-code-migration
  2. เปิด problem.js ใน editor ของคุณพร้อมความช่วยเหลือของ AI

  3. Implement solve() ที่ convert code ตามที่ README อธิบาย

  4. ตรวจสอบ solution ของคุณ:

    Terminal window
    node test.js
  5. เมื่อ tests ผ่านทั้งหมด ส่งคำตอบ:

    Terminal window
    npx bluebeltdojo submit

💡 Tip: ลองนึกว่าถ้าคุณ migrate โค้ดจริง — คุณจะ test ยังไง? Approach เดียวกันใช้ได้ที่นี่


คำใบ้

  • อ่าน instructions ใน problem.js อย่างละเอียด
  • คิดถึง semantic equivalence — logic เดียวกัน ≠ syntax เดียวกัน
  • ถ้าติดขัด ลอง test กับ inputs หลายแบบ