Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/Appearances.cpp (C++)

Code before:
    {
        if (!_hasPowerlineCharacters.has_value())
        {
            try
            {
                winrt::com_ptr<IDWriteFont> font;
                THROW_IF_FAILED(_family->GetFont(0, font.put()));
                BOOL exists{};
                // We're actually checking for the "Extended" PowerLine glyph set.
                // They're more fun.
                THROW_IF_FAILED(font->HasCharacter(0xE0B6, &exists));
                _hasPowerlineCharacters = (exists == TRUE);
            }
            catch (...)
            {
                _hasPowerlineCharacters = false;
            }
        }
        return _hasPowerlineCharacters.value_or(false);
    }

    Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> Font::FontAxesTagsAndNames()
    {
        if (!_fontAxesTagsAndNames)
        {
            _fontAxesTagsAndNames = winrt::single_threaded_map<winrt::hstring, winrt::hstring>();

            wil::com_ptr<IDWriteFont> font;
            THROW_IF_FAILED(_family->GetFont(0, font.put()));
            wil::com_ptr<IDWriteFontFace> fontFace;
            THROW_IF_FAILED(font->CreateFontFace(fontFace.put()));
            wil::com_ptr<IDWriteFontFace5> fontFace5;
            if (fontFace5 = fontFace.try_query<IDWriteFontFace5>())
            {
                wil::com_ptr<IDWriteFontResource> fontResource;
                THROW_IF_FAILED(fontFace5->GetFontResource(fontResource.put()));

                std::vector<DWRITE_FONT_AXIS_VALUE> axesVector;
                const auto axesCount = fontFace5->GetFontAxisValueCount();
                if (axesCount > 0)
                {
                    uint32_t localeIndex;
                    BOOL localeExists;
                    wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
                    const auto localeToTry = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) ? localeName : L"en-US";

                    axesVector.resize(axesCount);
                    fontFace5->GetFontAxisValues(axesVector.data(), axesCount);
                    for (uint32_t i = 0; i < axesCount; ++i)
                    {
                        const auto tagString = _axisTagToString(axesVector[i].axisTag);

Review comment (refactor):
nit: I believe the `Font::FontAxesTagsAndNames()` could still deserve some cleaning.

Abstracting the `_fontAxesTagsAndNames` refresh into its own function would allow for early returns and thus allow us to significantly reduce the nesting. Certain variables can still be moved into inner scopes to reduce the distance between their declaration and initialization. `_fontAxesTagsAndNames` should be constructed as a native map first and only then moved into the WinRT container.

Code after:
    {
        if (!_hasPowerlineCharacters.has_value())
        {
            try
            {
                winrt::com_ptr<IDWriteFont> font;
                THROW_IF_FAILED(_family->GetFont(0, font.put()));
                BOOL exists{};
                // We're actually checking for the "Extended" PowerLine glyph set.
                // They're more fun.
                THROW_IF_FAILED(font->HasCharacter(0xE0B6, &exists));
                _hasPowerlineCharacters = (exists == TRUE);
            }
            catch (...)
            {
                _hasPowerlineCharacters = false;
            }
        }
        return _hasPowerlineCharacters.value_or(false);
    }

    Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring> Font::FontAxesTagsAndNames()
    {
        if (!_fontAxesTagsAndNames)
        {
            wil::com_ptr<IDWriteFont> font;
            THROW_IF_FAILED(_family->GetFont(0, font.put()));
            wil::com_ptr<IDWriteFontFace> fontFace;
            THROW_IF_FAILED(font->CreateFontFace(fontFace.put()));
            wil::com_ptr<IDWriteFontFace5> fontFace5;
            if (fontFace5 = fontFace.try_query<IDWriteFontFace5>())
            {
                wil::com_ptr<IDWriteFontResource> fontResource;
                THROW_IF_FAILED(fontFace5->GetFontResource(fontResource.put()));

                const auto axesCount = fontFace5->GetFontAxisValueCount();
                if (axesCount > 0)
                {
                    std::vector<DWRITE_FONT_AXIS_VALUE> axesVector(axesCount);
                    fontFace5->GetFontAxisValues(axesVector.data(), axesCount);

                    uint32_t localeIndex;
                    BOOL localeExists;
                    wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
                    const auto localeToTry = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) ? localeName : L"en-US";

                    std::unordered_map<winrt::hstring, winrt::hstring> fontAxesTagsAndNames;
                    for (uint32_t i = 0; i < axesCount; ++i)
                    {
                        wil::com_ptr<IDWriteLocalizedStrings> names;
                        THROW_IF_FAILED(fontResource->GetAxisNames(i, names.put()));