Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp (C++)

Code before:

    if (!source.Hidden())
    {
        _activeProfiles.Append(*duplicated);
    }

    winrt::hstring newName{ fmt::format(L"{} ({})", source.Name(), RS_(L"CopySuffix")) };

    // Check if this name already exists and if so, append a number
    for (uint32_t candidateIndex = 0; candidateIndex < _allProfiles.Size() + 1; ++candidateIndex)
    {
        if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))
        {
            break;
        }
        // There is a theoretical unsigned integer wraparound, which is OK
        newName = fmt::format(L"{} ({} {})", source.Name(), RS_(L"CopySuffix"), candidateIndex + 2);
    }
    duplicated->Name(winrt::hstring(newName));

#define DUPLICATE_SETTING_MACRO(settingName)                                                                                                   \
    if (source.Has##settingName() ||                                                                                                           \
        (source.##settingName##OverrideSource() != nullptr && source.##settingName##OverrideSource().Origin() != OriginTag::ProfilesDefaults)) \
    {                                                                                                                                          \
        duplicated->##settingName(source.##settingName());                                                                                     \
    }

#define DUPLICATE_FONT_SETTING_MACRO(settingName)                                                                                                                                    \
    if (source.FontInfo().Has##settingName() ||                                                                                                                                      \
        (source.FontInfo().##settingName##OverrideSource() != nullptr && source.FontInfo().##settingName##OverrideSource().SourceProfile().Origin() != OriginTag::ProfilesDefaults)) \
    {                                                                                                                                                                                \
        duplicated->FontInfo().##settingName(source.FontInfo().##settingName());                                                                                                     \
    }

#define DUPLICATE_APPEARANCE_SETTING_MACRO(settingName)                                                                                                                                                \
    if (source.DefaultAppearance().Has##settingName() ||                                                                                                                                               \
        (source.DefaultAppearance().##settingName##OverrideSource() != nullptr && source.DefaultAppearance().##settingName##OverrideSource().SourceProfile().Origin() != OriginTag::ProfilesDefaults)) \
    {                                                                                                                                                                                                  \
        duplicated->DefaultAppearance().##settingName(source.DefaultAppearance().##settingName());                                                                                                     \
    }

    DUPLICATE_SETTING_MACRO(Hidden);
    DUPLICATE_SETTING_MACRO(Icon);
    DUPLICATE_SETTING_MACRO(CloseOnExit);
    DUPLICATE_SETTING_MACRO(TabTitle);
    DUPLICATE_SETTING_MACRO(TabColor);
    DUPLICATE_SETTING_MACRO(SuppressApplicationTitle);
    DUPLICATE_SETTING_MACRO(UseAcrylic);
    DUPLICATE_SETTING_MACRO(AcrylicOpacity);
    DUPLICATE_SETTING_MACRO(ScrollState);
    DUPLICATE_SETTING_MACRO(Padding);

Review comment (suggestion):
BTW In classic C you ~often~ sometimes write:
(edit: Well you often write `do/while(0)` macros, but breaks are not as common. They're useful regardless though.)

```cpp
do {
    if (...) break;
    if (...) break;
    if (...) break;
    do_something();
} while (0)
```

It would allow you to prevent the repeated COM calls and reduce the line width.

Code after:

    // Check if this name already exists and if so, append a number
    for (uint32_t candidateIndex = 0; candidateIndex < _allProfiles.Size() + 1; ++candidateIndex)
    {
        if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))
        {
            break;
        }
        // There is a theoretical unsigned integer wraparound, which is OK
        newName = fmt::format(L"{} ({} {})", source.Name(), RS_(L"CopySuffix"), candidateIndex + 2);
    }
    duplicated->Name(winrt::hstring(newName));

#define DUPLICATE_SETTING_MACRO(settingName)                                                                                                   \
    if (source.Has##settingName() ||                                                                                                           \
        (source.##settingName##OverrideSource() != nullptr && source.##settingName##OverrideSource().Origin() != OriginTag::ProfilesDefaults)) \
    {                                                                                                                                          \
        duplicated->##settingName(source.##settingName());                                                                                     \
    }

#define DUPLICATE_FONT_SETTING_MACRO(settingName)                                                                                                                                    \
    if (source.FontInfo().Has##settingName() ||                                                                                                                                      \
        (source.FontInfo().##settingName##OverrideSource() != nullptr && source.FontInfo().##settingName##OverrideSource().SourceProfile().Origin() != OriginTag::ProfilesDefaults)) \
    {                                                                                                                                                                                \
        duplicated->FontInfo().##settingName(source.FontInfo().##settingName());                                                                                                     \
    }

#define DUPLICATE_APPEARANCE_SETTING_MACRO(settingName)                                                                                                                                                \
    if (source.DefaultAppearance().Has##settingName() ||                                                                                                                                               \
        (source.DefaultAppearance().##settingName##OverrideSource() != nullptr && source.DefaultAppearance().##settingName##OverrideSource().SourceProfile().Origin() != OriginTag::ProfilesDefaults)) \
    {                                                                                                                                                                                                  \
        duplicated->DefaultAppearance().##settingName(source.DefaultAppearance().##settingName());                                                                                                     \
    }

    DUPLICATE_SETTING_MACRO(Hidden);
    DUPLICATE_SETTING_MACRO(Icon);
    DUPLICATE_SETTING_MACRO(CloseOnExit);
    DUPLICATE_SETTING_MACRO(TabTitle);
    DUPLICATE_SETTING_MACRO(TabColor);
    DUPLICATE_SETTING_MACRO(SuppressApplicationTitle);
    DUPLICATE_SETTING_MACRO(UseAcrylic);
    DUPLICATE_SETTING_MACRO(AcrylicOpacity);
    DUPLICATE_SETTING_MACRO(ScrollState);
    DUPLICATE_SETTING_MACRO(Padding);
    DUPLICATE_SETTING_MACRO(Commandline);
    DUPLICATE_SETTING_MACRO(StartingDirectory);
    DUPLICATE_SETTING_MACRO(AntialiasingMode);
    DUPLICATE_SETTING_MACRO(ForceFullRepaintRendering);
    DUPLICATE_SETTING_MACRO(SoftwareRendering);
    DUPLICATE_SETTING_MACRO(HistorySize);
    DUPLICATE_SETTING_MACRO(SnapOnInput);