Coding

Best Practices 


Pillars of code quality

All the developers need to understand the five pillars of code quality are:

  1. Readability
  2. Maintainability
  3. Efficiency
  4. Reliability
  5. Reusability

 

Coding best practices

Coding best practices are essential to writing clean, maintainable, and efficient code. A few important practices include:

Consistent Naming Conventions

Write meaningful names for variables, functions, classes, components, modules, folders, files, git repositories and branches.

Use case styles

  1. PascalCase for classes, components, modules (e.g., UserAccount).
  2. camelCase for variables and functions (e.g., customerName, getUsers).
  3. snake_case for constant, global variables  (e.g., BASE_PATH).
  4. kebab-case for folders, files, git repositories and branches. (e.g., project-name).

Proper Commenting

  1. Inline Comments: Add comments to explain complex logic or important decisions within the code.
  2. Block Comments: Add block comments to describe the overall purpose of a code.
  3. Documentation Comments: Add comments to describe the purpose and usage of modules, classes, and functions.

Consistent Formatting

  1. Indentation: Use consistent indentation throughout your codebase (e.g., 4 spaces).
  2. Line Length: Keep lines reasonably short, typically around 80-120 characters.
  3. Spacing: Use spaces around operators and after commas to improve readability.

Readable Control Structures

  1. Clear Conditionals: Write conditionals that are easy to understand. (e.g., if count == 0 rather than if not count).
  2. Avoid Deep Nesting: Do not use deep nesting of loops and conditionals.

Modular Code

Small Functions and Methods: Break down your code into small, manageable functions or methods that do one thing well.

Avoid Hard-Coding

  1. Configuration Files: Use configuration files or environment variables for values that may change (e.g., API keys, database URLs).
  2. Constants: Define constants for fixed values and avoid using magic numbers or strings.

Follow to write optimization Code

  1. Avoid Over-Engineering: Do not use complex logic where a simpler solution will be there.
  2. Conditional Checks: Place conditions in a way that minimizes redundant checks.
  3. Re-use Computed Values: Store and reuse values that are computed multiple times.
  4. Minimize Loop: Reduce the number of calculations or function calls inside loops.
  5. Use Built-in Functions: Prefer to use built-in functions and libraries
  6. Avoid Repeated Concatenation: Avoid repeated string concatenation inside loops.
  7. Leverage Optimized Libraries: Use libraries and frameworks that are optimized for performance. 
  8. Stay Updated: Keep libraries and dependencies updated to benefit from performance improvements and take advantage of new benefits.
  9. Batch I/O Operations: Group input/output operations to reduce the overhead.
  10. Asynchronous I/O: Use asynchronous I/O operations where possible to improve performance.

Memory Management

  1. Avoid Memory Leaks: Ensure that resources are properly released after use.
  2. Use Efficient Data Types: Use appropriate data types (e.g., using byte instead of int when storing small numbers).
  3. Cache Results: Store the results of expensive computations and reuse them when needed.

Optimize Network Usage

  1. Reduce Data Transfer: Minimize the amount of data transferred over the network.
  2. Compression: Use data compression to reduce the size of data being transferred.

Error Handling

  1. Defensive Programming: Handle potential errors and edge cases.
  2. Validate Inputs: Check for valid input values and handle invalid inputs appropriately.
  3. Check Return Values: Always check the return values of functions and handle errors.
  4. Error Handling: Use try-catch blocks or equivalent error-handling mechanisms to catch and handle exceptions.
  5. Meaningful Error Messages: Provide clear and informative error messages that help diagnose the issue.
  6.  

Use plugins for perfect web development

  • Prettier: The Prettier extension for Visual Studio Code brings about a seamless integration between the code editor and Prettier, allowing you to easily format code using a keyboard shortcut, or immediately after saving the file.
  • ESLint: The ESLint extension for Visual Studio Code enables integration between ESLint and the code editor. This integration allows ESLint to notify you of problems right in the editor.
  • GitLens: GitLens displays views containing essential repository data and information on the current file, such as file history, commits, branches and remotes.
  • Code Spell Checker : Code Spell Checker” is an essential VS Code extension that enhances your coding experience. Seamlessly integrated into the editor, it diligently checks and corrects spelling errors, ensuring clean and error-free code.
  • Auto Rename Tag : The extension works for HTML, XML, PHP, and Javascript, and removes the need to change your tag names twice.

0