Is this a coding habit or is there another reason?

int mark = value1;
int position = value2;
public classType reset() {
    if (mark < 0)
        throw new Exception();
    position = mark;
    return this;
}

my thoughts:

  1. simply uses m to be more concise than mark
  2. prevents the value of mark from being modified before it is returned. (note: this class is not thread safe
  3. this is not a problem

clear friends, please give me some advice, thank you

May.09,2022

individuals tend to point 2.

it's not that thread safe just says that thread safety is not guaranteed, but it's good to make a little extra effort for security.

for example, it can improve the consistency of the internal state: although it is impossible to guarantee the complete consistency of mark and position, at least the mark when calling reset is consistent with the final position, so that the mark of call is greater than or equal to 0, but the final position becomes less than 0 .

although this consistency improvement does not enable the program to run under multiple threads, it has the following benefits:

  1. is good for debugging. As mentioned earlier, position always satisfies non-negative constraints, and under no circumstances will you receive a range exception about position. Otherwise, this constraint of position will not guarantee that there is no error when you reset, but you receive a range exception error when you call a method that uses position at some point in the future. It will take a lot of effort to find out where the position has changed
  2. .
  3. can improve security in multithreading. Even in many cases where competition is not fierce, even if multithreading is misused, it may not go wrong. It's like a simple password lock on a suitcase. It's not safe. If your suitcase is stolen, a thief can open it in a thousand ways; but it's relatively safe, at least no one can steal from you while you're away or taking a nap.
Menu