Function argument/parameter indentation

The point of having a whitespace-insignificant language is people are free to do whatever they want. Not sure why this is even in the style guide.

1 Like

Then I take it you won’t mind when we write code like this, @killerswan:

 unsafe  fn Foo( bar :bar ,baz:    BAZ
) -> () {
   if rand  ::      < bool
> {
          a( );
       b()} else
{c}}

Strong conventions are categorically a Good Thing.

3 Likes

No, this is already a solved problem: see here.

So, now that we have where clauses, how do you guys format your code today?

Something like this?

pub fn sample<D, R, T>(
    length: uint,
    distribution: &D,
    rng: &mut R
) -> Vec<T>
    where
        D: IndependentSample<T>,
        R: Rng,
{
    unimplemented!();
}

I would be going for the same visual or hanging indent as I was going for earlier, ending up with, in most spaced-out, vidual-indent form, this:

pub fn sample<D, R, T>
             (length: uint,
              distribution: &D,
              rng: &mut R)
             -> Vec<T>
             where D: IndependentSample<T>,
                   R: Rng {
    unimplemented!();
}

Or with hanging indent, something like this (though it’s somewhat negotiable; most notably, visual indent might be good for the where clause):

pub fn sample<D, R, T>(
        length: uint,
        distribution: &D,
        rng: &mut R)
        -> Vec<T>
        where D: IndependentSample<T>, R: Rng {
    unimplemented!();
}

Aw damn, where clauses put a big monkey wrench right in the middle of that style :frowning: Could stick the where on the same line as the return type to avoid that extra indent, although it seems a bit weird, but it is better than any alternative I can think of.

pub fn sample<D, R, T>(
    length: uint,
    distribution: &D,
    rng: &mut R
) -> Vec<T> where
    D: IndependentSample<T>,
    R: Rng,
{
    unimplemented!();
}
1 Like

One param per line, each ending with a comma. And, opening brace or where immediately following the return type declaration.

pub fn sample<D, R, T>(
    length: uint,
    distribution: &D,
    rng: &mut R,
) -> Vec<T> where
    D: IndependentSample<T>,
    R: Rng,
{
    unimplemented!();
}


pub fn simple(
    foo: Foo,
    bar: Bar,
    baz: Baz,
) -> Qux {
    unimplemented!();
}

Single indent on a new line after where works well, especially because the line with the where will often have a lot of other stuff:

fn long_function_name<T, U, V>(with: Args) -> Ret where
    Foo<T>: Something,
    U: Else,
{
    unimplemented!();
}

Parameter lists are just that: lists. That is to say they should read either purely vertically or purely horizontally, otherwise there is a lot of cognitive strain as the brain has to work out where the next parameter begins.

let x = foo(value_a, value_b, value_c);

fn foo(
        long_arg1: T,
        long_arg2: T,
        long_arg3: T
) -> LongType {
    /* body */
}

are both much easier to read as the next parameter is easy to find.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.