/**
- * The receiver that the user requests to reset the password
- */
- Function requestResetPassword (){
- // Check whether the user exists
- If (! CheckUserExists ($ _ GET ['userid']) {
- Exit ('sorry, the user does not exist. please confirm the user account. ');
- }
- ResetPassword ($ _ GET ['userid']);
- // Finally, send an email to the user
- SendEmail ($ _ GET ['userid'], 'password reset successfully', and 'New password is xxx ');
- Exit ('the new password has been sent to your mailbox. ');
- }
/**
- * Helping users reset their passwords
- */
- Function resetPassword ($ userid ){
- // Check whether the user exists
- If (! CheckUserExists ($ userid )){
- Return false;
- }
// Reset the user password
- // Omitted...
- Return true;
- }
/**
- * Send an email to the user
- */
- Function sendEmail ($ userid, $ title, $ content ){
- // Check whether the user exists
- If (! CheckUserExists ($ userid )){
- Return false;
- }
// Send email
- // Omitted...
- Return true;
- }
/**
- * Check whether a user exists
- */
- Function checkUserExists ($ userid ){
- $ User = getUserInfo ($ userid );
- Return! Empty ($ user );
- }
/**
- * Obtain the data of a user.
- */
- Function getUserInfo ($ userid ){
- // Assume that I have a query function used to query the database and return data.
- $ User = query ("SELECT * FROM 'user' WHERE 'uid' =". intval ($ userid ));
- Return is_array ($ user )? $ User: array ();
- }
Problem: all three functions use the checkUserExists function to check whether the user does not exist. the database queries three times, resulting in some additional overhead. It seems possible to remove any checkUserExists between the three. However, if some functions require calling resetPassword or sendEmail, an error may occur if the user does not exist. Another solution is to write the resetPassword logic to requestResetPassword. in another step, write the sendEmail logic. In this way, the number of function calls is reduced, and the database query becomes one time, thus improving the performance. However, the password reset and email sending functions cannot be reused and violate the single responsibility principle, and the code complexity is also improved. However, because function separation and reusability are both good, if the actual performance is affected, you may consider using the cache method to reduce database queries and change the checkUserExists functions they share:
/**
- * Check whether a user exists
- */
- Function checkUserExists ($ userid ){
- // Add a cache to record the user's results
- Static $ cache = array ();
// Check whether the current user has checked once
- If (isset ($ cache [$ userid]) {
- Return $ cache [$ userid];
- }
$ User = getUserInfo ($ userid );
- // Record the result to the cache
- $ Cache [$ userid] =! Empty ($ user );
Return $ cache [$ userid];
- }
You can also use the same method to change the getUserInfo function. When code reusability is improved, it is very easy to improve the performance, and the performance bottleneck is easily discovered and modified. Consider the impact on program performance. for example, when traversing data, traversal may be encapsulated into a function for reuse and used multiple times. These expenses have little impact on the project. Therefore, the focus can be on improving code reusability and maintainability, rather than focusing on wasting more performance. If the actual performance does not meet the requirements, you can also increase the hardware configuration. |