Coding Conventions
These are the coding conventions I follow during development.
Core Principles
1. TDD (Test-Driven Development)
- Write tests first: Write test code before implementation.
- Scenario-based: Features should be immediately understandable from tests alone.
- Given-When-Then: All tests must include these comments.
2. SOLID Principles
- Single Responsibility Principle (SRP): A class should have only one responsibility.
- Open-Closed Principle (OCP): Open for extension, closed for modification.
- Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types.
- Interface Segregation Principle (ISP): Clients should not depend on interfaces they don’t use.
- Dependency Inversion Principle (DIP): Depend on abstractions, not concretions.
3. Documentation
- KDoc: Write KDoc for all public functions and classes.
- API Documentation: Use automatic documentation tools (Spring REST Docs, etc.) for REST APIs.
- Comments: Explain “why” not “what” the code does.
4. Database Rules
Audit Trail (Required 5 Fields)
All entities must include these 5 fields:
| |
Soft Delete (No Physical Deletion)
- Only logical deletion allowed: Use
deletedAtfield to mark deletion - Physical deletion forbidden: Never use
DELETEqueries
| |
Kotlin Coding Style
Basic Principles
- Follow Kotlin official coding conventions
- Use IntelliJ IDEA default formatter
- Readability first: Prioritize readability over performance, optimize when necessary
Naming Rules
| |
Logging Rules
Strictly Forbidden:
- ❌ No println (obviously): Never use
println() - ❌ No emojis: Do not use emojis in code, comments, or logs
Required:
- ✅ SLF4J Logger: Use SLF4J Logger for all logging
| |
Import Rules
No FQCN (Fully Qualified Class Name)
| |
Class Structure Order
| |
Function Writing Rules
| |
Null Safety
| |
Data Class Usage
| |
Spring Boot Rules
Layer Structure
controller/ # REST API endpoints
├── request/ # Request DTOs
└── response/ # Response DTOs
service/ # Business logic
domain/ # Domain models (Entity)
repository/ # Data access layerDependency Injection
| |
Transactions
| |
Code Review Checklist
Basic Principles
- TDD: Were tests written first?
- SOLID: Does it follow Single Responsibility Principle?
- Naming: Is it clear and meaningful?
- Documentation: Is KDoc written?
Security
- Sensitive Info: No hardcoded passwords/API keys?
- Input Validation: Are all external inputs validated?
- SQL Injection: Using Prepared Statement or Type-safe DSL?
- Password: Hashed with BCrypt before storing?
- Logging: No sensitive info (passwords, tokens) in logs?
Concurrency
- Thread-Safety: Is shared state properly synchronized?
- Immutable Objects: Using val and minimizing mutable state?
- Synchronized: Only minimal necessary scope synchronized?
- Coroutine: Using structured concurrency instead of GlobalScope?
Performance
- String Concatenation: Using StringBuilder in loops?
- Collection Initial Capacity: Set initial capacity when size is known?
- Object Creation: Avoiding unnecessary object creation?
- Sequence: Considered Sequence for large data processing?
Code Quality
- Null Safety: Using Elvis operator or let instead of !!?
- Exception Handling: No empty catch blocks, handling specific exceptions?
- Magic Numbers: All numeric literals defined as constants?
- Logging: Using SLF4J Logger? No println or emojis?
- Import: Not using FQCN?
Database
- Audit Trail: Do all entities have the 5 required fields?
- Soft Delete: Not using physical deletion?
- SELECT: Explicitly selecting only needed columns?
Security
1. Sensitive Information Handling
Strictly Forbidden:
- ❌ Hardcoded passwords/API keys: Never write directly in code
- ❌ Sensitive info as constants: Use environment variables or Secret Manager
- ❌ Sensitive info in logs: Never log passwords, tokens, personal information
| |
2. Input Validation
All external inputs must be validated:
| |
3. SQL Injection Prevention
| |
4. Password Handling
| |
Concurrency
1. Thread-Safe Code
| |
2. Immutable Objects Recommended
| |
3. Synchronized Usage Guide
| |
4. Coroutine Precautions
| |
Performance
1. String Concatenation
| |
2. Collection Initial Capacity
| |
3. Stream vs for-loop
| |
4. Avoid Unnecessary Object Creation
| |
5. Lazy Initialization
| |
Code Quality
1. Null Check Patterns
| |
2. Optional Usage Guide
| |
3. Exception Handling Rules
| |
4. No Magic Numbers
| |
Strictly Forbidden
Logging Related
- ❌ No println (obviously): Absolutely forbidden to use
println() - ❌ No emojis: Forbidden to use emojis in code, comments, logs
- ❌ No FQCN: Must use import statements
- ❌ No sensitive info in logs: Never log passwords, tokens, personal information
Security
- ❌ Hardcoded passwords/API keys: Use environment variables or Secret Manager
- ❌ SQL Injection vulnerabilities: Use Prepared Statement or Type-safe DSL
- ❌ Plain text password storage: Must hash with BCrypt, etc.
- ❌ Missing input validation: All external inputs must be validated
Code Quality
- ❌ Excessive
!!(non-null assertion): Use minimally - ❌
Anytype usage: Specify explicit types - ❌ Magic numbers: Define as constants
- ❌ Long functions: Consider refactoring if over 20 lines
- ❌ God class: A single class should not have too many responsibilities
- ❌ Empty catch blocks: Exceptions must be properly handled
Concurrency
- ❌ Thread-unsafe code: Shared state must be properly synchronized
- ❌ Using GlobalScope: Use structured concurrency
Performance
- ❌ String concatenation with + in loops: Use StringBuilder
- ❌ Overusing Stream on large collections: Consider using Sequence
- ❌ Unnecessary object creation: Declare reusable objects outside
Database
- ❌ Physical deletion forbidden: Never use
DELETEqueries - ❌ SELECT asterisk (*) forbidden: Explicitly select only needed columns
- ❌ Missing Audit Trail forbidden: All entities must have the 5 required fields