Skip to content

Instantly share code, notes, and snippets.

@mausworks
Created December 1, 2020 09:23
Show Gist options
  • Save mausworks/5370a2e99dbd5c234ede5267105cc8fb to your computer and use it in GitHub Desktop.
Save mausworks/5370a2e99dbd5c234ede5267105cc8fb to your computer and use it in GitHub Desktop.
C# formatting parameters (or arguments)

The reasoning for formatting parameters this way is so that we can intepret the argument list as one continous thing, without having to spend time aligning things.

The short cases

DO THIS

public static string Stringify(
    SomeLongTypeName someLongTypeName, int repeat)
{
    // ...
}

NOT THIS

public string Stringify(SomeLongTypeName someLongTypeName,
   int repeat)
{
    // ...
}

The argument list should always "look as one succinct thing", the last example creates cognitive dissonance as your eyes have to jump from one indentation level to another, which breaks the reading flow.

DEFINITELY DO NOT DO THIS

public string Stringify(SomeLongTypeName someLongTypeName,
                        int repeat)
{
    // ...
}

This makes the argument list look like "one thing" but it forces the programmer to spend time aligning things and indenting things to just the right level. If another argument is added, this process has to be repeated.

The longer cases

Sometimes there are just "too many parameters" (for dependency-injection and similar cases)

DO THIS

public SomeController(
    ISomeService someService,
    ISomeOtherService someOtherService,
    IAThirdService aThirdService,
    IAndAResolverOfSorts aResolverOfSorts)
{
    // ...
}

DO NOT DO THIS

public SomeController(ISomeService someService,
    ISomeOtherService someOtherService,
    IAThirdService aThirdService,
    IAndAResolverOfSorts aResolverOfSorts)
{
    // ...
}

As in the short case, this forces your eyes to jump from one indentation level to another. The first argument gets special treatment and the argument list looks not as succinct.

DEFINITELY DO NOT DO THIS

public SomeController(ISomeService someService,
                      ISomeOtherService someOtherService,
                      IAThirdService aThirdService,
                      IAndAResolverOfSorts aResolverOfSorts)
{
    // ...
}

As in the short case, this forces the programmer to spend time aligning things, and once this service adds another dependency (which is pretty likely) this process has to be repeated again.

DEFINITELY, DEFINITELY DO NOT DO THIS

public SomeController(ISomeService someService, ISomeOtherService someOtherService,
    IAThirdService aThirdService, IAndAResolverOfSorts aResolverOfSorts)
{
    // ...
}

All rules are out the window at this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment