Coding Princiles To Write Better Code
Coding Principles
Introduction
This serves as some of the tips I use when writing code, even when working by myself.
Coding Principles can be broken up into two parts: Coding Standards and Coding Best Practices.
Coding Standards are a set of guidelines and rules that define how something should be written within a particular organisation. This can be enforced through tools such as linters or code reviews.
Coding Best Practices are a set of recommended practices that are generally accepted as being the most effective or efficient way to write code. They are not necessarily enforced like coding standards, but they are widely used and recommended to improve the quality of code.
For example, a coding standard might require all functions to have a specific naming convention, while coding best practice might recommend using functions that are small and focused, with a single responsibility.
Overall, your code should be easy to read and understand, both for yourself and for others who may need to work with it in the future. This means using descriptive variable and function names, commenting your code where necessary, and following consistent formatting and style conventions.
Coding Standards
These help make the code more readable, consistent and maintainable.
- Naming conventions: Names of variables, functions, and classes should be meaningful and follow a consistent naming convention. For example, use camelCase for variable names and PascalCase for class names.
- Indentation: Code should be properly indented to make it easier to read and understand.
- Commenting: Code should be adequately commented to explain its purpose and how it works. Comments are a great way of explaining your thought process and why you are doing it this way. It can also be used to provide additional relevant information. Avoid obvious comments such as explaining an if statement if it is obvious what it's doing.
- Error handling: Code should handle errors gracefully and have appropriate error messages.
- Formatting: Code should be formatted consistently, such as using a consistent number of spaces for indentation or using consistent line breaks.
- Performance consideration: Code should be written with performance in mind, such as avoiding unnecessary loops and optimising data structures.
- Security considerations: Code should be written with security in mind, such as avoiding SQL injection attacks and properly handling user authentication.
- Version control: Code should be properly managed using version control tools.
Coding Best Practices
DRY
DRY follows the principle of Don’t Repeat Yourself (DRY). This advises developers to minimise the duplication of code in their projects. This can be achieved by creating reusable functions of components that can be utilised in multiple sections of the codebase. By following this approach, developers can increase efficiency and reduce errors in their code. Use the rule of 3: if you copy and paste 3 times or more, use a function instead.
Code Formatting
Use Consistent formatting Prettier VS Code. This helps keep the code cleaner and easier to read. Use our predefined modular, we don’t want one person using 2-tab space and another using 4. With inconsistent tab space errors, it can be a nightmare to keep track of code changes because committing a single line could reformat the entire document.
Functions
Break code into small, manageable functions. Functions should do one thing and do it well and should be as small as possible to make them easier to read, understand and test.
Documentation
Document as you Code (DAC) This will help other programmers understand your code and make it easier to maintain.
Version Control
This keeps track of changes to your code, and it will also make it easy to revert to a previous version if a mistake is made. A general rule for what goes into Version Control: anything made by humans such as code, images or small data (Big amount of data go into Databases). Anything that gets generated (sometimes in a folder prefixed with “.” such as “.next” ,”.vs” “.firebase”), gets ignored from being tracked by version control.
Specific Coding Standards
Conclusion
Coding best practices are essential for writing high-quality code that is effective, efficient, and maintainable. While it may take more time and effort to write code that adheres to these best practices, the benefits in terms of code quality, maintainability, and team productivity are well worth it. By making a commitment to coding best practices, developers can create code that is reliable, scalable, and future proof.