Skip to content

Code Conventions & Patterns

Rust-First Architecture

Logic quan trọng phải ở Rust server, không phải frontend.

Server (Rust)Frontend (Vue)
Business logicYesNo
ValidationYesUI validation only
DatabaseYes (sqlx)No
AuthenticationYes (JWT)Lưu/gửi token
AI processingYesGọi API
UI stateNoYes (composables)
User inputNoYes (forms, events)

Quy tắc: Hỏi "Logic này có cần window không?" - Nếu không → đặt ở Rust.

Mobile-First Design

Nguyên tắcGiá trị
Font size tối thiểu16px body, 14px minimum
Tap targets>= 44x44px (Apple HIG)
LayoutSingle column, full-width
NavigationBottom tabs hoặc drawer
Spacing12-16px padding minimum
Design for375px width (iPhone SE)

Composable Patterns

Singleton State

ts
// Module-level ref - share giữa tất cả components
const data = ref<Type[]>([])

export function useXxx() {
  return { data, ... }
}

Proto Mapping

Composable boundary chuyển proto types sang internal types:

ts
// Proto camelCase → internal snake_case
config.value = {
  total_cash: Number(res.config.totalCash),
  daily_burn_rate: Number(res.config.dailyBurnRate),
}

After Mutation

Luôn reload list từ server sau mutation:

ts
async function createItem(...) {
  await client.create(...)
  await loadItems()  // Reload!
}

No Circular Imports

Composable A có thể import B, nhưng B không được import A.

Anti-patterns

  1. Đừng thêm complexity để fix complexity - Fix root cause, không patch symptom
  2. Đừng dùng workaround - Tìm cách đúng theo framework/library
  3. Minimal first - Default là không hiển thị/thêm, chỉ show khi cần
  4. Đừng duplicate logic - Nếu logic tồn tại ở đâu đó, gọi nó thay vì copy

Code Review Checklist

Tự hỏi trước khi commit:

  1. Logic này đã tồn tại ở đâu chưa? → Reuse thay vì duplicate
  2. Function có nhiều hơn 1 responsibility? → Split
  3. Đây có phải workaround? → Tìm cách đúng
  4. Nếu thay đổi logic gốc, bao nhiêu chỗ cần sửa? → Phải là 1

Tool Prioritization

Ưu tiênToolDùng cho
1LSPgoToDefinition, findReferences, hover
2GrepPattern search khi không biết symbol
3ReadChỉ khi LSP/Grep không đủ
TaskDùngKhông dùng
Package managerbunnpm
Proto codegen./generate.shmanual buf commands
Type check (FE)vue-tsc --noEmit-
Type check (BE)cargo clippy-