clang-format: constructor initializers are aligned in an hard-to-read way #5068

issue laanwj opened this issue on October 9, 2014
  1. laanwj commented at 12:04 PM on October 9, 2014: member

    For example:

    -NetworkStyle::NetworkStyle(const QString &appName, const QString &appIcon, const char *titleAddText, const QString &splashImage):
    -    appName(appName),
    -    appIcon(appIcon),
    -    titleAddText(qApp->translate("SplashScreen", titleAddText)),
    -    splashImage(splashImage)
    +NetworkStyle::NetworkStyle(const QString& appName, const QString& appIcon, const char* titleAddText, const QString& splashImage) : appName(appName),
    +                                                                                                                                   appIcon(appIcon),
    +                                                                                                                                   titleAddText(qApp->translate("SplashScreen", titleAddText)),
    +                                                                                                                                   splashImage(splashImage)
    

    Aligning the arguments all the way to the right doesn't look too good in the case of a long constructor. I'd prefer if they started on the next line instead, and have the normal indentation. @sipa Is this possible with clang-format?

    This is not a fluke; I've noted this before (https://github.com/bitcoin/bitcoin/pull/4933#issuecomment-56338845) as well.

  2. laanwj added the label Docs and Output on Oct 9, 2014
  3. paveljanik commented at 7:56 PM on October 1, 2015: contributor

    Looks like there is an option for this (http://clang.llvm.org/docs/ClangFormatStyleOptions.html):

    ConstructorInitializerIndentWidth (unsigned)
    The number of characters to use for indentation of constructor initializer lists.
    
  4. jonasschnelli commented at 8:00 PM on October 1, 2015: contributor

    clang-format is not fully deterministic between versions and platforms,.. also it would produce relatively high noise (diffs).

    A better way would be to provide a simple script in /contrib that would call clang-format before git commit (hook) or something similar. Most code formatting nits then would be gone (and more space for the important discussions).

  5. paveljanik commented at 2:02 PM on October 10, 2015: contributor

    After

    -ColumnLimit:     0
    +ColumnLimit:     80
    

    the clang-format indents the constructor nicely:

    NetworkStyle::NetworkStyle(const QString& appName,
                               const int iconColorHueShift,
                               const int iconColorSaturationReduction,
                               const char* titleAddText)
        : appName(appName),
          titleAddText(qApp->translate("SplashScreen", titleAddText))
    {
    

    Is it what you expect?

  6. MarcoFalke commented at 2:06 PM on October 11, 2015: member

    Yes, but ColumnLimit:80 will also introduce diff in all lines longer than 80 chars which is not wanted, imo.

  7. dcousens commented at 1:35 AM on October 13, 2015: contributor

    @MarcoFalke IMHO the only reason to ask for the alignment is if you have a problem with the ColumnLimit anyway. I agree with clang's attempt at consistency here, either you have the limit or you don't.

    If there was a limit, 120 would be my vote.

  8. MarcoFalke commented at 2:56 PM on October 18, 2015: member

    The nearest I could get:

    If 59a83686fe483130d2f383e3e1cdf347810775df fixes this problem to everyone's satisfaction, I am happy to add the commit to #6839.

    Example from OP:

    -NetworkStyle::NetworkStyle(const QString &appName, const QString &appIcon, const char *titleAddText, const QString &splashImage):
    -    appName(appName),
    -    appIcon(appIcon),
    -    titleAddText(qApp->translate(SplashScreen, titleAddText)),
    -    splashImage(splashImage)
    +NetworkStyle::NetworkStyle(const QString& appName, const QString& appIcon, const char* titleAddText, const QString& splashImage)
    +    : appName(appName)
    +    , appIcon(appIcon)
    +    , titleAddText(qApp->translate(SplashScreen, titleAddText))
    +    , splashImage(splashImage)
    
  9. dcousens commented at 12:10 AM on October 19, 2015: contributor

    @MarcoFalke what if 120 was chosen as the column limit? Or even 160? I feel like both would solve this issue and hopefully not introduce too many changes.

  10. MarcoFalke commented at 12:15 PM on October 19, 2015: member

    As I said, this will combine/break a lot of lines (also strings). Moreover, I think it's bad to hardcode the line width; Usually the developer theirself or the developer's software takes care of properly displaying (or breaking) long lines.

    • 120 (1224eeb58a95aecb0ae6bb238402c8cd2ed0e954)
    -NetworkStyle::NetworkStyle(const QString &appName, const QString &appIcon, const char *titleAddText, const QString &splashImage):
    -    appName(appName),
    -    appIcon(appIcon),
    -    titleAddText(qApp->translate(SplashScreen, titleAddText)),
    -    splashImage(splashImage)
    +NetworkStyle::NetworkStyle(const QString& appName,
    +    const QString& appIcon,
    +    const char* titleAddText,
    +    const QString& splashImage)
    +    : appName(appName), appIcon(appIcon), titleAddText(qApp->translate(SplashScreen, titleAddText)),
    +      splashImage(splashImage)
    
    • 160 (ccebf2511134371146ae5329629ac5984660e593)
    -NetworkStyle::NetworkStyle(const QString& appName,
    -    const QString& appIcon,
    -    const char* titleAddText,
    -    const QString& splashImage)
    -    : appName(appName), appIcon(appIcon), titleAddText(qApp->translate(SplashScreen, titleAddText)),
    -      splashImage(splashImage)
    +NetworkStyle::NetworkStyle(const QString& appName, const QString& appIcon, const char* titleAddText, const QString& splashImage)
    +    : appName(appName), appIcon(appIcon), titleAddText(qApp->translate(SplashScreen, titleAddText)), splashImage(splashImage)
    
  11. sipa commented at 12:49 PM on October 20, 2015: member

    I woupd personally like to have an enforced max-line length rule eventually, whether it's 80, 100, or 120 characters. I think that 160 is excessive, though.

    I don't think that should happen immediately, as it will cause large amounts of conflicting changes in the code base, though.

  12. laanwj closed this on Feb 16, 2016

  13. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me