April 9, 2023
Certainly! Here are some basic do’s and don’ts for coding standards, along with sample code snippets to illustrate them:
1. Use clear and descriptive variable, function, and class names:
# Variable
customer_name = “John Smith”
total_amount = 100.50
# Function and Class
def calculate_total_amount(items):
pass
class Customer:
def __init__(self, name):
self.name = name
Proper names must be given to even Directories and Files
Directories – small case
Files – Camel Case
2. Follow proper indentation and formatting:
// indentation of 2 space
if (condition) {
statement1;
statement2;
} else {
statement3;
}
3. Comment your code to provide clarity and explanations:
// explain the purpose of the code at start
int result = calculateTotalAmount(items); // Calculate the total amount of items
// describe the behavior of a function or method
/**
* Returns the sum of the given array of numbers.
* @param numbers The array of numbers
* @return The sum of the numbers
*/
public int calculateSum(int[] numbers) {
// Code to calculate the sum
}
1. Avoid using magic numbers or hard-coded values:
// Magic number without explanation [Random]int result = calculateSomething(42);
// Use a named constant or variable
int defaultValue = 42;
int result = calculateSomething(defaultValue);
2. Don’t use excessive or unnecessary comments:
# Redundant comment
x = x + 1 # Increment x by 1
3. Avoid long and complex functions or methods:
// Long and complex function – Unnecessary increases line of code
function processOrder(order) {
// A lot of code…
// More code…
// Even more code…
// …
// Code continues…
}
© Techligious is Proudly Owned by Techligious