Coding Guidelines - OmegaOoh/ku-tangtee GitHub Wiki

Javascript

1. Naming conventions

  • Using all UPPERCASE in descriptive word for Constant.
  • Using Pascal case in Class name declaration.
  • Using Camel case in Variable and Function declaration.
// camelCase for Variable and Function
let myVariable = 10;
function calculateSquareArea(length){
    return length * length;
}

// PascalCase for Class name
class MyClass{
    constructor(name){
        this.name = name;
    }
}

// UPPERCASE for Constant
const MAXIMUM_LENGTH = 1000000;
const MINIMUM_LENGTH = 0;

2. Object and Arrays formatting

For Objects you should follow this example.

const car = {
    brand : 'Honda',
    license : 'BK2590'
}; 

For Arrays you should follow this example.

 const numbers = [1, 2, 3]; 

3. If / else statements formatting

Single if / else

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Nested if / else

if (condition1) {
    if (condition2) {
        // Code to execute if both if conditions are true
    } else {
        // Code to execute when outer if condition is true and the inner if condition is false
    }
} else {
    // Code to execute when outer if condition is false
}

Avoid many nested if conditions because it will lead to deep indentation and it will be hard to read.

4. Using JSDoc to write description

/**
 * Calculates the sum of two numbers.
 * @param {number} a The first number.
 * @param {number} b The second number.
 * @returns {number} The sum of the two numbers.
 */
function add(a, b) {
    return a + b;
}

5. String Quoting

We prefer to use single quotes than double quotes.

  • Correct Usage.
const greeting = 'Hello, World!'; // Correct: Single quotes
const message = 'It\'s a great day!'; // Correct: Escaped single quote
const htmlString = '<div>Hello!</div>'; // Correct: Single quotes used
  • Incorrect Usage.
const wrongGreeting = "Hello, World!"; // Incorrect: Prefer single quotes
const wrongMessage = "It's a great day!"; // Incorrect: Prefer single quotes or escape
const wrongHtmlString = "<div>Hello!</div>"; // Incorrect: Prefer single quotes

6. Run JSHint

Lint JavaScript with JSHint

  • Run JSHint by using configuration file .jshintrc

Python

1. Follow PEP8 convention

Naming convention is different from Javascript.

my_variable : str = "Hello World!" # Prefer lowercases and underscores for functions and variables

class MyClass : # Prefer using PascalCase for class names
    """Class for showing naming convention only """
    def __init__( self, name : str )-->None:
        """Receiving name into this object

        :param name: name of anything
        :return: not return anything
        """
        self.name = name

THOUSAND_BANKNOTE : int = 1000 # Prefer UPPERCASE for constants

2. Run Flake8 with Flake8-docstrings

Lint with flake8

  • We will have the github workflow about this to maintain the code quality.
  • Run flake8 with setup configuration in .flake8

3. Use type hints from typing

Type hints example

def calculate_rectangle_area( length : float, width : float )-->float:
    # Type hints are length : float, width : float and -->float which mean these values are float
    # No need to use typing if it already satisfies by using simple type hints
    return length * width

from typing import List

def find_summation( data : List[float] )->float:
    # Type hints are data : List[float] and -->float
    return sum(data)

4 Use sphinx style docstrings but no need to define types because we already did that

def calculate_rectangle_area( length : float, width : float )-->float:
    """A function that uses to calculate rectangle area.

    :param name: length of your rectangle area
    :param width: width of your rectangle area
    :raises TypeError: can not computer non numeric rectangle width or length
    :return: area of the rectangle
    """
    # The block between function name and this line is sphinx style docstring
    try:
        return length * width
    except TypeError:
        raise TypeError("Non numeric rectangle length or width received")

5. Use mypy to check your type hints

Type check with mypy

  • Run mypy with configuration file mypy.ini

6. Run the tests using either pytest or unittest (Included into coverage)

  • We will have the github workflow about this.

7. Check the coverage of tests

codecov

  • Run coverage with configuration file .coveragerc

8. Import only modules not stuffs inside except shortcuts

import math # Import only module

e = math.e # We import only math module not item inside module so that we can track the problem easily.
print(e)

from django.views import generic # In this case is an exception so we import the item inside the module.

Github rules

  • No direct commit to main.
  • Pull requests have to be approved and reviewed before merging.
⚠️ **GitHub.com Fallback** ⚠️