Basic Principles of Writing High-Quality Code
Clean/Readable
1. Meaningful Names.
Details
- Use Intention-Revealing Names
- Make Meaningful Distinctions
- Use Pronounceable Names
- Use Searchable Names
- Pick One Word per Concept
- Use Solution Domain Names
- Use Problem Domain Names
- Avoid Disinformation
- Avoid Encodings
- Avoid Mental Mapping
- Don’t Be Cute
- Don’t Pun
- Add Meaningful Context
- Don’t Add Gratuitous Context
2. Don’t use magic literals (numbers and strings). Using variables or constants with descriptive names. For example, int MAX_LEN = 100;
3. Every function does only one thing.
4. Class should be small.
5. Keep it simple, stupid. Don’t optimize prematurely.
Robust/Secure
Make a robust, well-developed solution. Do a detail design. First, solve the problem. Then, write the code.
Unit tests. Reduce potential errors.
Error handling.
Consider boundary values.
Check for null values.
Avoid hard coded database or other credentials in code. Using environment variables or a Dotenv file.
Maintainable/Extensible/Easy to modify
Make a maintainable and extensible system design and solution. For example:
- Designing a maintainable and extensible data model.
- Designing a extensible APIs (an extensible structure of parameters and return data of the API).
Reduce or eliminate duplicate code. Don’t Repeat Yourself.
Place global properties in the project configuration file, not in the source code.
Decoupling modules, class and functions.
SOLID design principles. (Single Responsibility Principle, Open-Closed Principle, Liskov Substitution Principle, Interface Segregation Principle, and Dependency Inversion Principle)
Writing documentation.
Web Frontend/Desktop/Mobile
Consistency
Using the same styles. The same form items in different pages use the same styles of UI components.
Compatibility
Client/web pages can be displayed normally in different devices/browsers and with different resolutions.