A function returns the object status value and modifies the object status.Create two different functions, one for query and the other for modification.
Motivation: If a function only provides you with a value without any side effects, it is very valuable. You can call this function at will, or move the call Action to another part of the function. It is a good idea to clearly show the difference between the two functions "with side effects" and "without side effects. No function with returned values should have any side effects. SomeProgramMembers even use this as a required rule.
If you encounter a function that has both returned values and side effects, you should try to split the query action from the modification action.
There is a common optimization method: cache the query results in a field, so that subsequent repeated queries can greatly speed up. Although this method changes the object state, this modification is imperceptible, because you always get the same results regardless of any query.
Practice: 1. Create a New query and make it return the same value as the original function.
2Modify the original function to call the query function and return the obtained result.
3, Compilation, and testing.
4, Will call the original functionCodeCall the query function, and add the call to the original function before calling the query function. Compile and test each modification.
5Change the return value of the original function to void, and delete all return statements.