Coding Guidelines - OmegaOoh/ku-tangtee GitHub Wiki
- 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;
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];
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.
/**
* 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;
}
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
- Run JSHint by using configuration file
.jshintrc
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
- We will have the github workflow about this to maintain the code quality.
- Run flake8 with setup configuration in
.flake8
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)
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")
- Run mypy with configuration file
mypy.ini
- We will have the github workflow about this.
- Run coverage with configuration file
.coveragerc
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.
- No direct commit to main.
- Pull requests have to be approved and reviewed before merging.