Coding Standards - seiren2k/FoodieGo GitHub Wiki

Overview

Our Coding Standards are designed to ensure that our code remains consistent, readable, and easy to maintain. These standards focus on Separation of Concerns, Consistent Naming, Basic Formatting, and Comments.


1. Separation of Concerns in File Structure

Each file is dedicated to a specific purpose or functionality, which helps keep the code modular and organized.

Guidelines

  • Single Responsibility: Each file should handle one type of functionality or action. This separation ensures that each file is focused and easy to understand.
  • Naming for Clarity: File names should clearly describe their purpose, using lowercase with hyphens to separate words.

Examples

  • add-admin.php handles adding an admin.
  • delete-category.php is dedicated to deleting a category.
  • manage-orders.php is used for managing orders.

2. Consistent Naming

Clear, descriptive naming conventions make code easier to read and maintain.

Guidelines

  • Variables: Use lowercase with underscores (snake_case) for variable names to indicate purpose clearly.
  • Functions: Use verbs in function names to describe the action they perform.
  • Files: Use lowercase letters with hyphens to separate words in file names.

Examples

  • Variable:
    $user_name = "John";
    $total_amount = 250;
    
    
  • Function:
    function get_user_by_id($user_id) {
      // Code here...
    }
    
    
  • File:
    • add-admin.php
    • delete-category.php
    • manage-orders.php

3. Basic Formatting

Consistent formatting makes the code easy to read and reduces errors.

Guidelines

  • Indentation: Use 4 spaces per level. Avoid tabs to maintain consistency.
  • Spacing: Add spaces around operators (=, +, ==) to improve readability.
  • Braces: Place opening braces { on the same line as the statement.

Examples

  • Indentation:
    if ($is_active) {
        echo "User is active";
    }
    
    
  • Spacing:
    $a = 5;
    $b = 10;
    $sum = $a + $b;
    
    
  • Braces:
    function add_numbers($num1, $num2) {
        return $num1 + $num2;
    }
    
    

4. Comments

Comments help clarify the code without cluttering it. We use them to explain function purposes or complex sections only.

Guidelines

  • Function Comments: Use comments to briefly explain the purpose of a function and its parameters.
  • Inline Comments: Use inline comments to explain non-obvious lines of code.

Examples

  • Function Comment:
    // Calculate the total price of items in the cart
    function calculate_total_price($items) {
        return array_sum($items);
    }
    
    
  • Inline Comment:
    $discount = $price * 0.1;  // 10% discount calculation