Laravel Code Conventions

1. 🗂️ Project Structure & Organization


2. 🧠 Variable & Function Naming Rules


3. 📝 Naming Conventions

Models

Controllers

Migrations

Relationships


4. 🔍 Scope Naming Conventions

Mục đích Tiền tố Định dạng Ví dụ
Tìm kiếm theo trường cụ thể by scopeBy<Field> scopeById($id)
Kiểm tra trường boolean = true is scopeIs<Flag> scopeIsActive()
Kiểm tra trường boolean = false isNot scopeIsNot<Flag> scopeIsNotActive()
Kiểm tra sự tồn tại của mối quan hệ has scopeHas<Relation> scopeHasPosts()
Kiểm tra mối quan hệ không tồn tại hasNot scopeHasNot<Relation> scopeHasNotPosts()
Mối quan hệ không có without scopeWithout<Relation> scopeWithoutPlan()

5. ✔️ Validation Rules


6. 🔗 Eloquent & Relationships


7. 🧰 Artisan Commands


8. 📦 API Resource & Collections

Các Resources có thể định nghĩa phương thức toArray() để chuyển đổi dữ liệu thành một mảng, và nếu cần, bạn có thể thêm phương thức toJson() để trả về dữ liệu dưới dạng JSON.


9. 🎯 Code Style & Clean Code


10. 🧪 Testing

/** @test */
public function it_can_create_a_user()
{
    $response = $this->post('/register', [...]);
    $response->assertStatus(201);
}

11. 🔒 Security & Best Practices




JavaScript Code Conventions

1. 🗂️ Project Structure & Organization


2. 🧠 Variable & Function Naming Rules

const calculateTotal = (price, quantity) => price * quantity;

3. 📝 Naming Conventions

Variables

Functions

Classes

Arrays & Objects

Constants

Parameters

Boolean Variables

Private Variables (in Classes or Objects)

Getter/Setter Methods


4. 🔍 Function Naming Conventions

General Rules

Prefixes for Specific Purposes

Purpose Prefix Format Example
Get a value get get<Field> getUser()
Set a value set set<Field> setUserName()
Check a condition is is<Field> isUserActive()
Handle an action handle handle<Field> handleClick()
Initialize an object or class init init<Field> initUser()
Perform an action with side effect do do<Field> doSomething()
Perform an async action fetch fetch<Field> fetchUserData()

Async Functions

Event Handlers

Boolean Functions

Utility Functions


5. ✔️ Error Handling

General Guidelines

Custom Error Handling

Handling Asynchronous Errors

Logging Errors

Graceful Error Handling


6. 🔗 Best Practices

General Best Practices

Code Quality Best Practices

Performance Best Practices

Security Best Practices

Maintainability Best Practices


7. 🧪 Testing

General Guidelines

Types of Tests

Testing Frameworks

Writing Tests

Running Tests

Mocking & Stubbing

Test Coverage

Mocking API Calls

Best Practices


8. 📦 Modules & Imports

General Guidelines


9. 🎯 Code Style & Clean Code

General Guidelines

Formatting & Indentation

Function & Variable Naming

Commenting & Documentation

Error Handling

Refactoring

Consistency