A single responsibility interpretation is a module that is responsible for completing only one responsibility or function, mainly to improve the maintainability and reusability of the method. Here’s an example:
Obviously, KMP implementation is more difficult, readable, and maintainable than the first one. But if there is a performance bottleneck in a particular scenario, such as when dealing with large text strings, the second can be N times more efficient than the first. To solve difficult problems such as performance, it is necessary to use a more complex method, so it does not violate the Kiss principle.
1. Don’t overly “show off”, if KMP is only used to deal with small strings used in ordinary work, then it is also contrary to the Kiss principle.
The above two methods to verify the username and password, they implement the same logic, but the function semantics are not the same, if you combine them into a method “checkUserNameOrPassWord”, a method does two things, violates a single principle, and in the future with the development of the business, if the rules of the password are changed, then it will affect the verification of the username logic, and need to be re-split.
2. Increase maintenance costs: If it is necessary to change the logic of verifying IP at that time, it is likely that one has been changed and another has been changed, resulting in system failure.
The above code calls the “checkUserExisted” method twice, resulting in duplicate executions and violating DRY principles. In general, for execution duplication, you can adjust the code order and unify the specifications to avoid this problem. For example, for business verification, we can put it in the Manager layer to handle it uniformly.
1. Use context to reduce words, such as getUserName, in the context of the User class, can be reduced to getName.
2. Replace it with abbreviations, such as to=>2, business=>biz, DailyActiveUse=> DAU.
When the method parameter is greater than or equal to 4, the parameter is a bit too much, mainly affecting the readability and ease of use of the code, we can look forward to the framework source code, such as Spring definition method input parameters rarely exceed three. If there are too many parameters, there are generally two ways:
2. Encapsulate multiple parameters as objects.
1.Whether null makes business sense. For example, when updating a table field, null means that it is not updated, and null means that there is no data when querying data. The above scenario is more suitable for Integer.
2. Whether the parameter is required, if it is required, it is more appropriate to use int.
Remove nesting levels that are too deep
2. Remove excess if-else
3. Extract part of the nesting logic and encapsulate it as a method
1. Don’t rely too much on comments, this will make you think about the bottom line and ignore the readability in the code.
1.Learn and understand architecture design from business development
2. The art of code annotation, does good code really not need comments?
3. What makes a 20-line piece of code 10x faster
GTS Cloud Rider Essay Contest is online!
Yunqiao is the landing of the concept of “assembled application”, which helps everyone improve the delivery speed, improve the delivery quality, and reduce labor costs. Participate in the Yunqiao Essay Contest, not only can the professional tutor group evaluate, but also 888 yuan cat super card and Tmall Genie Sound wait for you to come ~ the top 100 participants can get 38 yuan Tmall supermarket card Oh, first come, first served!
Click to read the original article for details.