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

·

🧭 Dev Naming Guide — แนวทางการตั้งชื่อไฟล์ ตัวแปร และโครงสร้างโปรเจคสำหรับทีม Dev

"โค้ดที่ดี คือโค้ดที่อ่านออกเสียงได้" — เคล็ดลับเล็กๆ ที่สร้างทีม Dev ให้โหดขึ้นเยอะ

🎯 ทำไมการตั้งชื่อจึงสำคัญ

  • ช่วยให้ทีมอ่านโค้ดได้ง่าย
  • ลดความสับสนในโครงสร้างโปรเจค
  • เพิ่ม productivity และลด bug
  • ใช้ร่วมกับ Git ได้ดี เช่น branch, commit message
  • รองรับการ scale ระบบได้ในระยะยาว

🧱 โครงสร้างและการตั้งชื่อแบบ Dev-friendly

📁 โฟลเดอร์ (Folder Naming)

กรณีชื่อที่แนะนำ
ทั่วไปuser-profile/, product-list/
ใช้ React/Vue/Nextfeatures/user/, shared/components/
หลีกเลี่ยงUserProfile/, user profile/

📌 ใช้: kebab-case, สื่อถึงสิ่งที่อยู่ภายใน

📄 ไฟล์ (File Naming)

ประเภทตัวอย่าง
TypeScript / JSuser-card.tsx
React ComponentUserCard.tsx
Markdown Docsgetting-started.md

📌 PascalCase สำหรับ Component และ kebab-case สำหรับทั่วไป

🧠 ตัวแปร และ ฟังก์ชัน (Variables & Functions)

ประเภทตัวอย่าง
ตัวแปรทั่วไปuserName, isAuthenticated
ฟังก์ชันfetchData(), handleSubmit()

📌 ใช้ camelCase ทั้งหมด

🎨 CSS Class / Utility

  • ถ้าใช้ BEM:
    <div class="card card--active">
      <div class="card__title">Title</div>
    </div>
    
  • ถ้าใช้ Tailwind CSS:
    <div class="bg-gray-100 p-4 rounded-lg">Content</div>
    

🌿 Git Branch Naming

ชนิดตัวอย่าง
Featurefeature/login-page
Bugfixbugfix/fix-header-overlap
Hotfixhotfix/crash-on-submit

📌 ใช้ kebab-case และ prefix ที่ชัดเจน

🧾 Git Commit Message (Conventional Commits)

feat: add login functionality
fix: resolve header issue on mobile
refactor: cleanup auth service
docs: update README.md

📌 ใช้ prefix: feat, fix, refactor, docs, test, chore

📡 API Path Naming

GET /api/users
POST /api/auth/login
DELETE /api/products/:id

📌 ใช้ lowercase และ kebab-case เท่านั้น

🗄️ Database Table & Column Naming

สิ่งที่ตั้งชื่อรูปแบบที่แนะนำ
Table namesnake_case พหูพจน์ เช่น users, order_items
Column namesnake_case เช่น user_id, created_at
Foreign Key${table}_id เช่น user_id, product_id

📌 ชื่อตารางควรใช้พหูพจน์เสมอ เช่น users, products, transactions 📌 หลีกเลี่ยงการใช้ตัวย่อ เช่น usr, prd ที่อาจทำให้ทีมใหม่เข้าใจผิด

📦 ตัวอย่างโครงสร้างโปรเจคแบบเต็ม

my-app/
├── public/
│   └── index.html
├── src/
│   ├── features/
│   │   └── auth/
│   │       ├── LoginForm.tsx
│   │       └── authAPI.ts
│   ├── shared/
│   │   ├── components/
│   │   │   └── Button.tsx
│   │   └── utils/
│   │       └── validators.ts
│   └── app/
│       ├── App.tsx
│       └── routes.ts
├── README.md
├── .gitignore
├── package.json
└── tsconfig.json

✅ สรุป: Naming Checklist

สิ่งที่ต้องตั้งชื่อรูปแบบที่แนะนำ
โฟลเดอร์kebab-case
ไฟล์kebab-case, PascalCase (สำหรับ Component)
ตัวแปรcamelCase
ฟังก์ชันcamelCase
CSS ClassBEM หรือ Tailwind
Git Branchfeature/x, bugfix/x
Commit MessageConventional Commits
API Endpointkebab-case
Table/Column DBsnake_case, ใช้ชื่อเต็ม

🚀 พร้อมใช้ในทีมคุณ

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