Repository: microsoft/terminal
File: src/cascadia/TerminalConnection/AzureConnection.cpp (C++)

Code before:
            // if we got here, we didn't break out of the loop early and need to go 'round again
            _WriteStringWithNewline(_formatResWithColoredUserInputOptions(USES_RESOURCE(L"AzureInvalidStoreInput"), USES_RESOURCE(L"AzureUserEntry_Yes"), USES_RESOURCE(L"AzureUserEntry_No")));
        } while (true);

        _state = AzureState::TermConnecting;
    }

    // Method description:
    // - Helper function to parse the preferred shell type from user settings returned by cloud console API.
    // We need this function because the field might be missing in the settings
    // created with old versions of cloud console API.
    std::optional<utility::string_t> AzureConnection::_ParsePreferredShellType(const web::json::value& settingsResponse)
    {
        if (!settingsResponse.has_object_field(L"properties"))
        {
            return std::nullopt;
        }

        const auto userSettings = settingsResponse.at(L"properties");
        if (!userSettings.has_string_field(L"preferredShellType"))
        {
            return std::nullopt;
        }

        const auto preferredShellTypeValue = userSettings.at(L"preferredShellType");
        return std::optional(preferredShellTypeValue.as_string());
    }

    // Method description:
    // - helper function to connect the user to the Azure cloud shell
    void AzureConnection::_RunConnectState()
    {
        // Get user's cloud shell settings
        const auto settingsResponse = _GetCloudShellUserSettings();
        if (settingsResponse.has_field(L"error"))
        {
            _WriteStringWithNewline(RS_(L"AzureNoCloudAccount"));
            _transitionToState(ConnectionState::Failed);
            return;
        }

        const auto shellType = _ParsePreferredShellType(settingsResponse);
        if (!shellType.has_value())
        {
            _WriteStringWithNewline(RS_(L"AzureInvalidUserSettings"));
            _transitionToState(ConnectionState::Failed);
            return;
        }

        // Request for a cloud shell
        _WriteStringWithNewline(RS_(L"AzureRequestingCloud"));

Review comment (suggestion):
If I might suggest a little more telescoping to make this easier to read:

```suggestion
        if (settingsResponse.has_object_field(L"properties"))
        {
	        const auto userSettings = settingsResponse.at(L"properties");
	        if (userSettings.has_string_field(L"preferredShellType"))
	        {
	        	const auto preferredShellTypeValue = userSettings.at(L"preferredShellType");
	            return preferredShellTypeValue.as_string();
	        }
        }
        
        return std::nullopt;
```

You also shouldn't need to construct an optional directly and instead rely on the optional constructor to let you return a string directly.

If you _do_ need to construct the optional directly, prefer `return { blah.as_string() };` :smile: so you don't need to repeat the optional type.

Code after:
            // if we got here, we didn't break out of the loop early and need to go 'round again
            _WriteStringWithNewline(_formatResWithColoredUserInputOptions(USES_RESOURCE(L"AzureInvalidStoreInput"), USES_RESOURCE(L"AzureUserEntry_Yes"), USES_RESOURCE(L"AzureUserEntry_No")));
        } while (true);

        _state = AzureState::TermConnecting;
    }

    // Method description:
    // - Helper function to parse the preferred shell type from user settings returned by cloud console API.
    // We need this function because the field might be missing in the settings
    // created with old versions of cloud console API.
    std::optional<utility::string_t> AzureConnection::_ParsePreferredShellType(const web::json::value& settingsResponse)
    {
        if (settingsResponse.has_object_field(L"properties"))
        {
            const auto userSettings = settingsResponse.at(L"properties");
            if (userSettings.has_string_field(L"preferredShellType"))
            {
                const auto preferredShellTypeValue = userSettings.at(L"preferredShellType");
                return preferredShellTypeValue.as_string();
            }
        }

        return std::nullopt;
    }

    // Method description:
    // - helper function to connect the user to the Azure cloud shell
    void AzureConnection::_RunConnectState()
    {
        // Get user's cloud shell settings
        const auto settingsResponse = _GetCloudShellUserSettings();
        if (settingsResponse.has_field(L"error"))
        {
            _WriteStringWithNewline(RS_(L"AzureNoCloudAccount"));
            _transitionToState(ConnectionState::Failed);
            return;
        }

        // Request for a cloud shell
        _WriteStringWithNewline(RS_(L"AzureRequestingCloud"));
        _cloudShellUri = _GetCloudShell();
        _WriteStringWithNewline(RS_(L"AzureSuccess"));

        // Request for a terminal for said cloud shell
        const auto shellType = _ParsePreferredShellType(settingsResponse);
        _WriteStringWithNewline(RS_(L"AzureRequestingTerminal"));
        const auto socketUri = _GetTerminal(shellType.value_or(L"pwsh"));
        _TerminalOutputHandlers(L"\r\n");

        // Step 8: connecting to said terminal