Repository: dotnet/roslyn
File: src/LanguageServer/Protocol/Handler/WorkDoneProgress/WorkDoneProgressManager.cs (C#)

Code before:
    /// <summary>
    /// Tracks active work done progress reporters by their token.
    /// Required so we can cancel them when the client requests us to.
    /// Guarded by <see cref="_progressLock"/> 
    /// </summary>
    /// <remarks>
    /// A singe entry is added once to the dictionary when a new work done progress session is initiated.
    /// Multiple threads may create new sessions concurrently.  Additionally, a single entry may have
    /// have a concurrent request from the client to cancel while the server is disposing of it.
    /// </remarks>
    private readonly Dictionary<string, WorkDoneProgressReporter> _progressReporters = [];

    /// <summary>
    /// Initiates a new work done progress reporting session with the client.
    /// This sends the initial `window/workDoneProgress/create` request, but callers are responsible for sending the
    /// begin and end reports.
    /// In the case of server side cancellation, an end report will be sent automatically with a "Cancelled" message.
    /// </summary>
    /// <param name="serverCancellationToken">a cancellation token that signals when the server wants to cancel the operation</param>
    public async Task<IWorkDoneProgressReporter> CreateWorkDoneProgressAsync(bool reportProgressToClient, CancellationToken serverCancellationToken)
    {
        var token = Guid.NewGuid().ToString();
        IWorkDoneProgressReporter reporter;

        // Only report progress to the client if both the client advertised support for progress reporting and the caller requested it.
        var reportProgress = _initializeManager.GetClientCapabilities().Window?.WorkDoneProgress == true && reportProgressToClient;

        if (reportProgress)
        {
            var clientReporter = new WorkDoneProgressReporter(token, this, serverCancellationToken);
            await clientReporter.SendCreateRequestAsync().ConfigureAwait(false);
            lock (_progressLock)
            {
                _progressReporters[token] = clientReporter;
            }

            return clientReporter;
        }
        else
        {
            reporter = new NoOpProgressReporter(serverCancellationToken);
        }

        return reporter;
    }

    public void CancelWorkDoneProgress(string token)
    {
        lock (_progressLock)
        {
            // We may be handling a client cancellation request after the server already completed and disposed of the progress.

Review comment (suggestion):
```suggestion
        var reportProgress = reportProgressToClient && _initializeManager.GetClientCapabilities().Window?.WorkDoneProgress == true;
```

Makes the easy check more visible before the long one.

Code after:
    /// <summary>
    /// Tracks active work done progress reporters by their token.
    /// Required so we can cancel them when the client requests us to.
    /// Guarded by <see cref="_progressLock"/> 
    /// </summary>
    /// <remarks>
    /// A singe entry is added once to the dictionary when a new work done progress session is initiated.
    /// Multiple threads may create new sessions concurrently.  Additionally, a single entry may have
    /// have a concurrent request from the client to cancel while the server is disposing of it.
    /// </remarks>
    private readonly Dictionary<string, WorkDoneProgressReporter> _progressReporters = [];

    /// <summary>
    /// Initiates a new work done progress reporting session with the client.
    /// This sends the initial `window/workDoneProgress/create` request, but callers are responsible for sending the
    /// begin and end reports.
    /// In the case of server side cancellation, an end report will be sent automatically with a "Cancelled" message.
    /// </summary>
    /// <param name="serverCancellationToken">a cancellation token that signals when the server wants to cancel the operation</param>
    public async Task<IWorkDoneProgressReporter> CreateWorkDoneProgressAsync(bool reportProgressToClient, CancellationToken serverCancellationToken)
    {
        var token = Guid.NewGuid().ToString();
        IWorkDoneProgressReporter reporter;

        // Only report progress to the client if both the client advertised support for progress reporting and the caller requested it.
        var reportProgress = reportProgressToClient && _initializeManager.GetClientCapabilities().Window?.WorkDoneProgress == true;

        if (reportProgress)
        {
            var clientReporter = new WorkDoneProgressReporter(token, this, serverCancellationToken);
            await clientReporter.SendCreateRequestAsync().ConfigureAwait(false);
            lock (_progressLock)
            {
                _progressReporters[token] = clientReporter;
            }

            return clientReporter;
        }
        else
        {
            reporter = new NoOpProgressReporter(serverCancellationToken);
        }

        return reporter;
    }

    public void CancelWorkDoneProgress(string token)
    {
        lock (_progressLock)
        {
            // We may be handling a client cancellation request after the server already completed and disposed of the progress.