Whether the intermediate variables are as detailed as possible

whether to write code is whether the more intermediate variables are written, the better
such as a point:

public class Point{
    int x; // x
    int y; // y
    
    int isExist;  // x,y
    int quadrant; //  x,y 
    
    
}

is it necessary to create intermediate variables such as isExist, quadrant, or for such an example, the more detailed the description, the better


to take into account the nature of a state,
like isExist obviously depends on x and y, and is suitable to be written as a public method, so that the state can be obtained through p.isExist (), and the correct state will be obtained after coordinate modification.
at this time, it is not recommended to have a variable representing isExist, because this variable is only used in the isExist () method and returns a value


if used frequently. You can create intermediate variables, that is, trade space for time


if intermediate variables are used very frequently, you can consider using them to achieve better efficiency, and if the frequency of use is very low, you can consider temporary calculation for the method.
in addition, if you can get multiple intermediate variables at once, it is more advantageous to use intermediate variables than to use them multiple times (even if you use them a little less frequently).


this practice is discouraged, which results in multiple copies of the same data state, which introduces additional maintenance costs and can easily lead to state inconsistencies and cause a lot of trouble.

you don't need to do this kind of optimization to write code at first.
doing this optimization at the expense of code maintainability and complexity ahead of time can do more harm than good.
because it will definitely bring some disadvantages, but it is not sure whether it will bring some benefits or not.

you think it will improve performance, but in theory, how much it will improve and how much revenue it will gain in practice.
has a lot to do with compilation optimization, business processes, and hardware platforms.

later, if you really need to optimize the code, you can use performance analysis tools to find the bottleneck and targeted optimization.
and the way to optimize is not necessarily to add a few "intermediate variables".

Menu