Back

General


Introduction

When writing code, it is important to keep in mind some general principles and standards that can help improve its quality and readability.

Naming Conventions

Class naming

Class Naming Example
Action action name with "Action" suffix CreateUserAction, DeleteProductAction, UpdateCategoryAction
Command command name with "Command" suffix GenerateReportCommand, ImportDataCommand, ExportDataCommand
Controller singular with "Controller" suffix UserController, ProductController, CategoryController
Data singular with "Data" suffix UserData, ProductData, CategoryData
Event event name with "Event" suffix UserCreatedEvent, OrderCreatedEvent, OrderShippedEvent
Exception singular with "Exception" suffix ValidationException, NotFoundException, DuplicateEntryException
Interface adjective/noun without suffix Loggable, Configurable, Exportable
Job job name with "Job" suffix SendEmailJob, ProcessPaymentJob, GenerateReportJob
Mail mail name without suffix InvoicePaid, OrderShipped, PasswordReset
Middleware middleware name without suffix Authentication, RateLimit, Cors
Model singular without suffix User, Product, Category
Notification notification name without suffix InvoicePaid, OrderShipped, PasswordReset
Observer singular model name with "Observer" suffix UserObserver, ProductObserver, CategoryObserver
Policy singular model name with "Policy" suffix UserPolicy, ProductPolicy, CategoryPolicy
Provider provider name with "Provider" suffix PaymentProvider, StorageProvider, EmailProvider
Request method name with singular model name with "Request" suffix StoreUserRequest, UpdateProductRequest, DestroyCategoryRequest
Rule rule name without suffix ValidPhoneNumber, ValidBankAccount, Uppercase
Scope scope name with "Scope" suffix ActiveScope, NewScope, TrendingScope
Support support name without suffix OpeningHours, Cart, Table
Trait adjective/prefix "with" without suffix Sortable, Searchable, Filterable, WithForm, WithSorting, WithFileUploads

Code naming

Entity Naming Example
Method camelCase store, massDestroy, run
Model Property snake_case is_active, created_at
Class Property camelCase $isActive, $createdAt
Variable camelCase $isActive, $createdAt
Route lowercase - plural users, products, categories
Route name snake_case - with 'dot' notation users.show, products.index, categories.create

Database naming

Entity Naming Example
Table snake_case - plural users, products, categories
Pivot table snake_case - singular model names alphabetically category_product, order_user
Table column snake_case - without table prefix title, price, description
Primary key id id
Foreign key snake_case - singular model name with "_id" suffix user_id, product_id, category_id

Structure

Class structure

When organizing code in the class, it is best to follow this order:

  • Traits
  • Constants
  • Static properties
  • Properties
  • Static methods
  • Methods
  • Abstract methods

It is also important to order each category by level in the following way:

  • public
  • protected
  • private
1use Trait;
2 
3const FOO = 1;
4 
5public static int $a;
6 
7public int $b;
8protected int $c;
9private int $d;
10 
11public static function methodA() {}
12 
13public function methodB() {}
14protected function methodC() {}
15private function methodD() {}
16 
17abstract public function methodE();

Commenting

Well-written comments elevate the readability and understanding of code, serving as valuable documentation for future developers who may need to understand it or modify it.

However, don't forget that readeable and understandable code is important in general - comments serve as additional support and they boost quality to the next level.

Attributes

When declaring native attributes in Laravel, such as $fillable, you can omit comments and type hints since these properties will not be never accessed directly.

1protected $fillable = [];

Custom attributes should be accompanied by a type hint and also supplemented with comments when the name is not sufficiently self-explanatory.

1public int $clickCount = 0;
2 
3/**
4 * Count represents the number of clicks.
5 */
6public int $count = 0;

Methods

You should provide descriptive comments to every method in the codebase.

1/**
2 * Display a listing of the resource.
3 */
4public function index()
5{
6 //
7}

Code

You should provide descriptive comments to every part of the code that looks unclear, such as many statements.

Edit this page

We are hiring!