This is a piece of code discussed at the code review meeting last week. This code is used to calculate the maximum number of pages based on the specified number of records and page size.
Target code
ComputePages( totalRecords, total = size = temp = total / ( }
Code Problems
I didn't notice this code at the beginning of the Code review meeting. But after reading multiple code pages, I found that the "ComputePages" method was redefined multiple times and has the same functionality. It should have been copied and pasted by developers. I originally wanted to talk about this code for copying and pasting, but later I found more problems:
Reconstruction suggestions
If the problem is found, there will be some solutions:
- For code reuse, you can define this method in a business logic-related class, instead of being dispersed into different classes. However, this article will not discuss this issue in detail;
- For data type security and data type conversion, we should consider appropriate mathematical methods to solve these problems, rather than rigid programming language specifications;
- We must be careful when coding the theorem that the denominator cannot be zero! Never be lucky;
- For business logic problems, we must carefully think about and ponder over the problems to be solved and the possible consequences before coding;
Reconstruction result
To make it easier for everyone to understand, I have pasted some specific ideas and ideas about code refactoring into the code in the form of remarks. You can seriously observe and raise questions.
ComputePages( totalRecords, (totalRecords < ) (pageSize < ) ()Math.Ceiling(totalRecords*/ }
Conclusion
As the saying goes, Details determine success or failure. As a programmer, from the details of the code, we can see the level of a person's programming and the amount of development experience, which also determines your success and failure in a certain sense!
I have published many articles to guide new users from the humanistic perspective. Today, this article also illustrates the impact of humanistic factors on the Quality of programmers!
To do things, be a good person first. Responsible for your code is responsible for yourself, responsible for yourself is responsible for the team, responsible for the team is responsible for the interests of the enterprise, because we are the same team and have the same benefits!