0% found this document useful (0 votes)
13 views5 pages

Coding Standards

The document outlines general coding guidelines including indentation, line length, and braces placement, emphasizing readability and consistency. It also covers naming conventions for variables, functions, and constants, as well as the importance of code comments and documentation. Additionally, it addresses code structure, error handling, memory management, code reusability, and best practices for file handling.

Uploaded by

adeivasundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Coding Standards

The document outlines general coding guidelines including indentation, line length, and braces placement, emphasizing readability and consistency. It also covers naming conventions for variables, functions, and constants, as well as the importance of code comments and documentation. Additionally, it addresses code structure, error handling, memory management, code reusability, and best practices for file handling.

Uploaded by

adeivasundaram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

General Coding Guidelines

 Indentation: Use consistent indentation. Typically, 4 spaces per indentation level is the
most common convention. Avoid mixing spaces and tabs.

o Example:

void function() {

if (condition) {

// Code here

 Line Length: Keep lines of code under 80 characters to improve readability. Break long lines
into multiple lines where necessary.

o Example:

printf("This is a very long string that should be broken down "

"into multiple lines to enhance readability.\n");

 Braces Placement: Always use braces {} to define the body of loops, conditionals, and
functions, even if they contain only one statement.

o Example:

if (x > 0) {

printf("Positive number\n");

2. Naming Conventions

 Variable Names: Use meaningful and descriptive names for variables. Stick to camelCase
or snake_case based on the team's preference.

o Example:

int totalAmount;

float item_price;

 Function Names: Use camelCase or snake_case for function names, starting with a
lowercase letter.

o Example:

c
void calculateTotalAmount();

int getItemPrice();

 Constants: Use UPPERCASE with underscores for constants and macros.

o Example:

#define MAX_USERS 100

const int MAX_LIMIT = 50;

 Structure Names: Capitalize the first letter of structure names and use camelCase.

o Example:

typedef struct {

int productID;

char productName[50];

} Product;

3. Code Comments

 General Commenting: Write clear comments for each function, especially for complex
logic. Use comments to explain "why" something is done, not "what" is done, since the
latter should be obvious from the code.

o Example:

// This function calculates the total amount based on item prices

void calculateTotalAmount() {

// Code logic

 Inline Comments: Use inline comments sparingly and only when the code's purpose is not
immediately clear.

o Example:

int totalAmount = itemPrice * quantity; // Calculate total amount

 Function Documentation: Each function should begin with a comment describing its
purpose, parameters, and return value.

o Example:

// Function to calculate total price

// Parameters:
// - itemPrice: Price of a single item

// - quantity: Number of items

// Return: Total price (itemPrice * quantity)

float calculateTotalPrice(float itemPrice, int quantity) {

return itemPrice * quantity;

4. Code Structure and Organization

 File Structure: Break the project into multiple files where appropriate. Typically, the project
should have a .c file for each functional module and a .h file for function declarations.

o Example:

 inventory.c - Contains the logic for inventory management.

 inventory.h - Contains function declarations and structure definitions for


inventory.

 main.c - Contains the main function and application flow.

 Header Files: Use header files for declarations of functions and structure definitions that
will be shared between multiple files. Include guards should be used in header files to
prevent multiple inclusions.

o Example:

#ifndef INVENTORY_H

#define INVENTORY_H

// Function prototypes and structure definitions

#endif

 Main Function: Keep the main() function minimal and focus on orchestrating the flow of the
program. Delegate the logic to functions/modules.

o Example:

int main() {

// Call di erent functions here

return 0;
}

5. Error Handling

 Check Return Values: Always check the return value of system functions (like fopen(),
malloc(), etc.) and handle errors gracefully.

o Example:

FILE *file = fopen("data.txt", "r");

if (file == NULL) {

fprintf(stderr, "Error opening file.\n");

return 1;

 Input Validation: Validate user inputs to prevent incorrect or unexpected behavior.

o Example:

int num;

printf("Enter a number: ");

if (scanf("%d", &num) != 1) {

printf("Invalid input\n");

return;

6. Memory Management

 Dynamic Memory Allocation: When using malloc(), calloc(), or realloc(), always check if
memory allocation was successful and free the memory after usage.

o Example:

int *arr = malloc(sizeof(int) * 10);

if (arr == NULL) {

printf("Memory allocation failed\n");

return;

// Use memory
free(arr);

 Avoid Memory Leaks: Ensure that all allocated memory is freed, especially when using
dynamic memory in a loop or a function that’s called multiple times.

7. Code Reusability

 Functions: Write small, reusable functions. Avoid duplicating code; instead, create
functions for repeated tasks.

o Example:

void printInvoice(Invoice *inv) {

// Logic to print invoice

void generateInvoice(Invoice *inv) {

// Logic to generate invoice

printInvoice(inv);

 Modularization: Organize code into logical modules (e.g., Inventory, Sales, Users, etc.).
Each module should handle a distinct part of the system.

8. Best Practices for File Handling

 File Operations: Always check the success of file operations (fopen(), fwrite(), fread(), etc.)
and handle errors gracefully.

 File Format: When storing records in a file (such as user data, product records), choose a
clear and structured format (like CSV for text files or binary format for complex data).

 Ensure File Closure: Always close files with fclose() after reading or writing to avoid file
corruption and resource leakage.

You might also like