In some library implementations people use to create a lot of local variables that directly relate to instance variables. I don't really get the point. Maybe someone could shine some light in my darkness.
An example is the method tryEmitNext(T t)
of the EmitterProcessor
in Project Reactor core library. The code is:
@Override
public EmitResult tryEmitNext(T t) {
if (done) {
return Sinks.EmitResult.FAIL_TERMINATED;
}
Objects.requireNonNull(t, "onNext");
Queue<T> q = queue;
if (q == null) {
if (Operators.setOnce(S, this, Operators.emptySubscription())) {
q = Queues.<T>get(prefetch).get();
queue = q;
}
else {
for (; ; ) {
if (isCancelled()) {
return EmitResult.FAIL_CANCELLED;
}
q = queue;
if (q != null) {
break;
}
}
}
}
if (!q.offer(t)) {
return subscribers == EMPTY ? EmitResult.FAIL_ZERO_SUBSCRIBER : EmitResult.FAIL_OVERFLOW;
}
drain();
return EmitResult.OK;
}
My question here: Why the local q
? To my understanding, everything here could be implemented, just using queue
. For me it looks like, the local variable is just adding code and thus complexity without any benefit.
Note: The method Operator.setOnce(...)
tries to set a subscription, and returns false in case there is already one, or if the pipeline is cancelled already.