Repeated work in loop (ARCH4072)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Key java.performance.repeated-work-in-loop · Severity info · Category performance
A computation whose inputs do not depend on the loop variable produces the same result every iteration. Such work should be hoisted out of the loop to avoid repeating it.
Incorrect
Section titled “Incorrect”for (Order order : orders) { if (order.getTotal() > config.getThreshold() * rate.getFactor()) { flag(order); }}Correct
Section titled “Correct”double limit = config.getThreshold() * rate.getFactor();for (Order order : orders) { if (order.getTotal() > limit) { flag(order); }}Move computations that do not depend on the loop variable into a variable outside the loop.