It turns out that to say that the code is clean and the system is designed correctly, easy reading of the code is not enough. It should have other qualities as well:
- The code is easy to modify. With the right design and architecture, extending your code can be done without much time and expense. The entities of the code should not be closely related to each other; the code should be somewhat abstract and self-sufficient. Each entity that we operate during the development should be responsible only for its own part of the functionality.
- The code must be stable, predictable, secure, and reliable. No matter how simple the code is to read, it should be covered by tests. Good code and tests are always there. Moreover, it is not only the number of tests that is important, it is also their quality. With this code, there are no problems when running and debugging, it does not cause changes in the environment.
- Protected code. When writing any code, you must not forget about the overall security of the product. We recommend that you get to know the basic principles of security and stick to them. If we’re talking about web projects, we recommend OWASP.
- A good code is a code that isn’t there. This does not mean that all the code should be written on one line, and you should definitely be proud of subtle methods. This means that the code should not be duplicated, and some of the common things should remain at the abstraction level. In theory, simplifying your code should lead to fewer defects.
- Reading the code itself is also important. Each developer has their own writing style, and the level of reading depends on our experience. We all want to write a simple, beautiful and concise code.
To get away from the subjective assessment of the quality of your code, in 1961, the term “code smells” or “code with a smell” was introduced. This is a group of rules and guidelines that clearly define whether it’s time to refactor. Odor leads to code decay, and developers should always strive to eliminate it. The need for refactoring directly follows from the presence of a code smell, and by preventing the cause, we can avoid the consequence. You can find more information on the key signs that it’s time for you to refactor in Martin Fowler book Refactoring: Improving the Design of Existing Code.
According to experts from SETA International, clean code means:
- Being easy to read
- Having simple logic
- Variable name, method and function names should be fully worded/ Avoid acronyms.
- Capability of inheriting and reusing
- One method/ function should have <100-line code lengths
- Declaration the variable closest to where it is used
- Absolutely not using the loop that does not determine the ending-point
Query optimization is the overall process of choosing the most efficient means of executing a SQL statement. SQL is a nonprocedural language, so the optimizer is free to merge, reorganize, and process in any order. The database optimizes each SQL statement based on statistics collected about the accessed data.
Some rules to have a good query:
- Capitalizing all key words (SELECT, INSERT, UPDATE, DELETE, GETDATE, CASE, WHEN, etc.)
- Not using * in query select, but specifically list each column name in the query
- In the WHERE clause, sorting the filters by priority as follows: Primary Key, Numeric, Flag, TextData, Date Time.
- If you must use a nested SELECT to filter, then add TOP 1 to that query
- Always adding NOLOCK to all select tables query
- Always using the ISNULL function for items that can have NULL values for comparison
- Using block comment /**/ instead of line comment —
- Never using or naming stored procedures that start with sp_….
Ways to find queries that slow down the system
- Waiting for QA/Customer to detect & complain about slow functionality
- Judging by experience: any function that writes too long is the slowest
- Adding log-time response into API(s)
- Adding log store query
Ways to optimize query
- Adding index to the table (should not create more than 2 indexes for 1 table)
- Re-optimizing the storage or relation of the table
- Reducing the number of returned records
- Avoiding the use of Trigger in table