Sample

int-8e7996dde7b3-I13

Lower every eligible classic and enhanced for loop to a while loop

Matched original source

Lines167

Scores

MethodScoreScale
Posnett00–1; higher is more readable
Scalabrino0.8310–1; higher is more readable
Dorn0.1440–1; higher is more readable
Mi0.0850–1; higher is more readable
LLOC53Logical lines; fewer is more readable
DeepSeek V4 Pro15.670–20; mean of three runs
GPT-6.1 Sol16.330–20; mean of three runs
BTTF0.5760–1; higher is more readable

Views

Source code

package io.netty.util.concurrent; import io.netty.util.internal.ObjectUtil; /** * <p>A promise combiner monitors the outcome of a number of discrete futures, then notifies a final, aggregate promise * when all of the combined futures are finished. The aggregate promise will succeed if and only if all of the combined * futures succeed. If any of the combined futures fail, the aggregate promise will fail. The cause failure for the * aggregate promise will be the failure for one of the failed combined futures; if more than one of the combined * futures fails, exactly which cause of failure will be assigned to the aggregate promise is undefined.</p> * * <p>Callers may populate a promise combiner with any number of futures to be combined via the * {@link PromiseCombiner#add(Future)} and {@link PromiseCombiner#addAll(Future[])} methods. When all futures to be * combined have been added, callers must provide an aggregate promise to be notified when all combined promises have * finished via the {@link PromiseCombiner#finish(Promise)} method.</p> * * <p>This implementation is <strong>NOT</strong> thread-safe and all methods must be called * from the {@link EventExecutor} thread.</p> */ public final class PromiseCombiner { private int expectedCount; private int doneCount; private Promise<Void> aggregatePromise; private Throwable cause; private final GenericFutureListener<Future<?>> listener = new GenericFutureListener<Future<?>>() { @Override public void operationComplete(final Future<?> future) { if (executor.inEventLoop()) { operationComplete0(future); } else { executor.execute(new Runnable() { @Override public void run() { operationComplete0(future); } }); } } private void operationComplete0(Future<?> future) { assert executor.inEventLoop(); ++doneCount; if (!future.isSuccess() && cause == null) { cause = future.cause(); } if (doneCount == expectedCount && aggregatePromise != null) { tryPromise(); } } }; private final EventExecutor executor; /** * Deprecated use {@link PromiseCombiner#PromiseCombiner(EventExecutor)}. */ @Deprecated public PromiseCombiner() { this(ImmediateEventExecutor.INSTANCE); } /** * The {@link EventExecutor} to use for notifications. You must call {@link #add(Future)}, {@link #addAll(Future[])} * and {@link #finish(Promise)} from within the {@link EventExecutor} thread. * * @param executor the {@link EventExecutor} to use for notifications. */ public PromiseCombiner(EventExecutor executor) { this.executor = ObjectUtil.checkNotNull(executor, "executor"); } /** * Adds a new promise to be combined. New promises may be added until an aggregate promise is added via the * {@link PromiseCombiner#finish(Promise)} method. * * @param promise the promise to add to this promise combiner * * @deprecated Replaced by {@link PromiseCombiner#add(Future)}. */ @Deprecated public void add(Promise promise) { add((Future) promise); } /** * Adds a new future to be combined. New futures may be added until an aggregate promise is added via the * {@link PromiseCombiner#finish(Promise)} method. * * @param future the future to add to this promise combiner */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void add(Future future) { checkAddAllowed(); checkInEventLoop(); ++expectedCount; future.addListener(listener); } /** * Adds new promises to be combined. New promises may be added until an aggregate promise is added via the * {@link PromiseCombiner#finish(Promise)} method. * * @param promises the promises to add to this promise combiner * * @deprecated Replaced by {@link PromiseCombiner#addAll(Future[])} */ @Deprecated public void addAll(Promise... promises) { addAll((Future[]) promises); } /** * Adds new futures to be combined. New futures may be added until an aggregate promise is added via the * {@link PromiseCombiner#finish(Promise)} method. * * @param futures the futures to add to this promise combiner */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void addAll(Future... futures) { { Future[] loopArray = futures; int loopIndex = 0; while (loopIndex < loopArray.length) { Future future = loopArray[loopIndex++]; this.add(future); } } } /** * <p>Sets the promise to be notified when all combined futures have finished. If all combined futures succeed, * then the aggregate promise will succeed. If one or more combined futures fails, then the aggregate promise will * fail with the cause of one of the failed futures. If more than one combined future fails, then exactly which * failure will be assigned to the aggregate promise is undefined.</p> * * <p>After this method is called, no more futures may be added via the {@link PromiseCombiner#add(Future)} or * {@link PromiseCombiner#addAll(Future[])} methods.</p> * * @param aggregatePromise the promise to notify when all combined futures have finished */ public void finish(Promise<Void> aggregatePromise) { ObjectUtil.checkNotNull(aggregatePromise, "aggregatePromise"); checkInEventLoop(); if (this.aggregatePromise != null) { throw new IllegalStateException("Already finished"); } this.aggregatePromise = aggregatePromise; if (doneCount == expectedCount) { tryPromise(); } } private void checkInEventLoop() { if (!executor.inEventLoop()) { throw new IllegalStateException("Must be called from EventExecutor thread"); } } private boolean tryPromise() { return (cause == null) ? aggregatePromise.trySuccess(null) : aggregatePromise.tryFailure(cause); } private void checkAddAllowed() { if (aggregatePromise != null) { throw new IllegalStateException("Adding promises is not allowed after finished adding"); } } }

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.3258.648−107.535
Source linesThe number of non-empty source lines.167+66.8
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.572−6.858

Intercept 8.87 + feature contributions = -38.723 (log-odds).
Score = sigmoid(-38.723) = 0.

Score0.831

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.168−4.912
Indentation frequencyHow rapidly indentation changes from line to line.112+3.137
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.112−2.149
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.166.86+1.923
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.119.988−1.417
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.116.464+1.075
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.047+0.488

Intercept 0.07 + feature contributions = -1.786 (log-odds).
Score = sigmoid(-1.786) = 0.144.

Score0.085
Score53
Run 1

The code is generally well-structured with clear separation of concerns and good documentation. However, it has some readability issues. The anonymous listener class contains nested logic (operationComplete -> operationComplete0) and a Runnable, increasing nesting depth. The addAll method uses an unnecessarily verbose while-loop with manual array indexing instead of an enhanced for-loop, adding noise. Some methods like 'add' perform multiple actions (checkAddAllowed, checkInEventLoop, increment count, add listener), reducing action granularity. The 'tryPromise' method uses a compact ternary that somewhat obscures intent. Field declarations are scattered between methods rather than grouped. The extensive Javadoc, while helpful, increases overall code length. Naming is generally clear, and the logic flow is understandable, but minor structural adjustments could improve readability.

Run 2

The code is well-structured with clear class-level documentation, logical method ordering, and mostly one action per line (add, finish). Nesting is shallow except for the listener's operationComplete method, which uses a callback, an if-else for event loop handling, and a nested Runnable; although readable, it adds some depth. Clarity of intent is strong—names like 'expectedCount', 'doneCount', 'tryPromise', and 'checkAddAllowed' make the state flow explicit. Code length is moderate; the JavaDoc is detailed but not excessive. Reading flow is mostly natural: fields, constructors, public API, private helpers. Minor drawbacks are the verbose loop in addAll (replacing a foreach) and the ternary's low readability due to nested method calls. Overall, a solid, maintainable utility class with only small improvements possible.

Run 3

The code is well-structured and readable overall. The class has a clear purpose, documented thoroughly with Javadoc, and methods are logically organized. Nesting is moderate, with a few inner classes but not excessively deep. Clarity of intent is good, with descriptive method names and comments explaining the non-obvious logic. Code length is appropriate for the functionality. Action granularity is mostly one action per line, though some lines could be broken up (e.g., the ternary in tryPromise). Reading flow is natural, with fields grouped together and methods ordered logically. Minor readability drawbacks include the deprecated methods with cast/SuppressWarnings annotations, the oddly formatted block in addAll (extra braces and manual loop instead of a foreach), and the boolean-returning tryPromise method whose return value is never used. Overall, it maintains Netty’s typical high standard for clarity.

Run 1

Code Structure: Well-organized class with focused methods, explicit state, and separate validation helpers. Nesting: Most methods are shallow, but the anonymous listener and nested Runnable add visual depth; addAll also introduces an unnecessary block. Clarity of intent: Descriptive names and comprehensive documentation clearly explain aggregation, failure handling, and threading requirements, though tryPromise is somewhat vague. Code length: Implementation is reasonably concise, but repetitive documentation, deprecated overloads, and the manual indexed loop add bulk. Action granularity: Nearly every statement performs one action; accessing the array while incrementing loopIndex is a minor exception. Reading flow: Lifecycle methods and completion handling are easy to follow overall, but inconsistent indentation in addAll, raw generic types, and placing executor after the listener slightly interrupt the flow.

Run 2

Code Structure: Well-organized class with focused methods, separate validation helpers, and documented public APIs. Nesting: Mostly shallow, although the anonymous listener and nested Runnable introduce visual depth, and addAll contains an unnecessary extra block. Clarity of intent: Descriptive field names and thorough documentation explain aggregation and event-loop requirements; operationComplete0 and tryPromise are less expressive names. Code length: Implementation methods are concise, but repeated documentation, deprecated overloads, and the expanded indexed loop add bulk. Action granularity: Generally one action per line; retrieving an array element while incrementing loopIndex combines actions unnecessarily. Reading flow: Completion handling, registration, and finalization are understandable, but inconsistent indentation in addAll and placing executor after the listener slightly disrupt scanning.

Run 3

Code Structure: Cohesive class with small methods and extracted validation helpers; the listener preceding the executor field slightly weakens organization. Nesting: Most methods are shallow, but nested anonymous listener/Runnable classes and an unnecessary block in addAll add complexity. Clarity of intent: Descriptive names and thorough documentation explain aggregation and threading requirements; operationComplete0 and tryPromise are less informative. Code length: Implementation is reasonably concise, though repetitive documentation, deprecated overloads, and the manual indexed loop introduce verbosity. Action granularity: Generally one action per line; indexing and incrementing together in loopArray[loopIndex++] slightly reduce clarity. Reading flow: Public operations and private helpers follow a mostly natural sequence, but inconsistent loop indentation and asynchronous callback indirection interrupt the flow.