Skip to content

Naming Convention

Amin Zamani edited this page Nov 20, 2023 · 1 revision

When using FastAPI, following a consistent and clear naming convention for your models, schemas, and services can enhance code readability and maintainability. Here are some best practices:

Models:

  1. Use Singular Nouns:

    • Name your models with singular nouns to represent a single instance of the entity.
    class User(BaseModel):
        username: str
        email: str
  2. Be Descriptive:

    • Choose descriptive names that convey the purpose of the model.
    class Item(BaseModel):
        name: str
        description: str

Schemas:

  1. Use CamelCase:

    • CamelCase is a common convention for naming schemas.
    class ItemResponse(BaseModel):
        name: str
        description: str
  2. Include the Word "Response" or "Request" for Clarity:

    • Make it clear whether a schema is used for request or response purposes.
    class ItemCreateRequest(BaseModel):
        name: str
        description: str

Services:

  1. Use Singular Nouns for Service Classes:

    • Similar to models, use singular nouns for service classes.
    class UserService:
        def create_user(self, user: User):
            # implementation
  2. Be Explicit:

    • Choose names that clearly indicate the service's functionality.
    class EmailService:
        def send_email(self, to: str, subject: str, body: str):
            # implementation

CRUD Operations:

  1. Follow CRUD Naming Conventions:
    • For services that perform CRUD operations, follow naming conventions like get, create, update, and delete.
    class ItemService:
        def create_item(self, item: Item):
            # implementation
    
        def get_item(self, item_id: int):
            # implementation
    
        def update_item(self, item_id: int, item: Item):
            # implementation
    
        def delete_item(self, item_id: int):
            # implementation

Miscellaneous:

  1. Use Consistent Prefixes:

    • If needed, use consistent prefixes for related classes to group them visually.
    class UserModel:
        # ...
    
    class UserSchema:
        # ...
    
    class UserService:
        # ...
  2. Avoid Ambiguous Names:

    • Ensure that your names are not ambiguous and clearly convey the purpose of each class.

Remember that these are general guidelines, and the most important thing is to be consistent within your project. Choose a convention that fits your team's style and stick to it throughout your codebase.

Python

Python Essentials 1 (PCEP)

Introduction to Python and computer programming

Data types, variables, basic I/O operations, and basic operators

Boolean values, conditional execution, loops, lists and list processing, logical and bitwise operations

Clean Code

Algorithms

Django

Django Rest Framework

API

pip

SQLAlchemy

FastAPI

Pytest

TDD

Git

Linux

Docker

Python Testing

Interview Questions

Clone this wiki locally