Coding Convention and Standards ‐ Template - JUCSE49-Mavericks/Smart-Class-Routine-Management-System GitHub Wiki

Presented by Umma Sumaiya Jahan


Coding Conventions and Standards

1. File Naming Conventions

  • Use meaningful and descriptive names.
  • Files should use lowercase letters and underscores (_) to separate words (e.g., user_profile.py).
  • Extensions should match the file type (e.g., .py for Python, .js for JavaScript).

2. Folder Structure

  • Keep a consistent folder structure across the project.
  • Use lowercase letters with underscores or hyphens for folder names (e.g., assets, utils, components).
  • Separate test files from production files in a clear directory (e.g., tests/).

3. Indentation and Whitespace

  • Use 4 spaces for indentation (or a specific standard for the language, e.g., 2 spaces in some JavaScript projects).
  • Avoid mixing tabs and spaces.
  • Ensure code blocks are properly indented for readability.
  • Leave a single blank line between methods and functions.
  • Limit lines to 80-120 characters to ensure readability.

4. Commenting

  • Use clear and concise comments to explain complex logic or code.
  • Single-line comments should begin with // (for JavaScript) or # (for Python).
  • Multi-line comments should be used for function or class explanations.

Example:

# This function calculates the factorial of a number
def factorial(n):
    """Returns the factorial of a given number."""
    pass

5. Variable and Function Naming

  • Use camelCase for variable and function names (e.g., getUserData(), calculateTotal).
  • Use descriptive and meaningful names (e.g., totalAmount instead of tAmount).
  • Constant variables should be in UPPERCASE (e.g., MAX_RETRY_COUNT).

Example:

MAX_RETRY_COUNT = 5
userName = "Akila"
def calculateTotal(price, quantity):
    return price * quantity

6. Function Length and Modularity

  • Keep functions short and focused on doing one task.
  • Break down large functions into smaller, reusable ones.
  • Limit function length to 40-50 lines where possible.

7. Code Structure and Readability

  • Follow the Single Responsibility Principle: each function or class should do one thing.
  • Keep related code together.
  • Use meaningful spacing to make code blocks easily readable.

Example:

# Good structure
def calculate_total(price, quantity):
    total = price * quantity
    return total

def apply_discount(total, discount):
    return total * (1 - discount)

8. Error Handling

  • Use try-except blocks (Python) or try-catch blocks (JavaScript) to handle exceptions.
  • Log errors with clear and informative messages.

Example:

try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"Error: {e}")

9. Code Reviews and Testing

  • Always ensure code passes all tests before merging.
  • Perform peer code reviews to catch bugs and ensure readability.
  • Write unit tests for all critical functionality and ensure 100% coverage.

10. Version Control

  • Use Git or any version control system for tracking changes.
  • Follow consistent commit message patterns (e.g., feat:, fix:, refactor:, docs:).
  • Create branches for new features, bug fixes, or any significant changes (e.g., feature/new-feature-name).

11. Documentation

  • Ensure all classes and functions are documented using docstrings or comments.
  • Maintain an updated README.md with setup instructions and important project information.

Example:

def multiply(a, b):
    """
    Multiply two numbers and return the result.
    :param a: First number
    :param b: Second number
    :return: Multiplication result
    """
    return a * b