Anonymous method formatting, again.


A while back I asked for some feedback about how we should format anonymous methods.  Then I was asking whether the default should be to put the opening brace on the same line as the delegate keyword or not.  From your feedback, it looks like we’ll keep the default the same as it is now (on the next line).  So that brings me to another question.  Some people have requested the ability to have the anonymous method braces indented.  I’ll give some examples:

Currently, anonymous methods look something like this:

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

      { return thing.ShouldBeIncluded; }

      );

 

Or this:

 

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

      { return thing.ShouldBeIncluded; });

 

Or this:

 

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

      {

            return thing.ShouldBeIncluded;

      });

 

Depending on where you put newlines.

 

With the proposed option, you would be able to make those look like:

 

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

            { return thing.ShouldBeIncluded; }

      );

 

Or this:

 

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

            { return thing.ShouldBeIncluded; });

 

Or this:

 

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

            {

                  return thing.ShouldBeIncluded;

            });

 

Or even this:

 

      List<Thing> somethings = things.FindAll(delegate(Thing thing)

            {

                  return thing.ShouldBeIncluded;

            }

      );

 

 

So my question for you today is: Do you want the new option? If you do, what should the default be, indented, or not indented?

 

Please post and let me know.

 


Comments (18)

  1. Of your seven samples, i’d have to go with number 3.

    Although, to be honest, i’d probably prefer something like this:

    List<Thing> somethings = things.FindAll(

    delegate(Thing thing)

    {

    return thing.ShouldBeIncluded;

    });

    or:

    List<Thing> somethings = things.FindAll(

    delegate(Thing thing)

    {

    return thing.ShouldBeIncluded;

    }

    );

    But of course, that second one was really just to show that no matter what you think you can cover, we’ll all do it different anyway :)

    But my preference for this is to take the view that defining the delegate is basically defining a function – with the delegate keyword being the name of the function – so therefore i’d want to make it _look_ like a function, but with an extra level of indenting to show that it’s ‘inline’.

    Make sense?

  2. ok, it didnt’ keep in the spaces that i put there for the indenting.

    in my firtst sample, indent one tabstop lines 2, 3, and 5, and 2 tabstops line 4.

    in my second, indent one tabstop lines 2, 3 and 5, and two tapstops line 4, and leave line 6 in line with line one.

    :)

  3. Zirakzigil says:

    Of your seven samples, I vastly prefer #4 or #7 (both of which are indented). I find the unindented versions less clear.

  4. Hi Geoff,

    Actually, you can do that with the current options. If you introduce a newline in the middle of the statement (as you did before the delegate keyword), you can place it where you want, and that will be preserved (relative to the statement), then following lines will be relative to that line.

    Wow, that’s a pretty confusing way of saying, "You can do that without the option".

  5. Hi Zirakzigil,

    I should point out that with the option you would be able to do any of the 7 examples. Without the option, you would be able to do 1, 2, 3.

    I take it though, that you would like to have the option, and have it’s default value be true?

  6. Zk says:

    I think the indenting makes it a lot clearer. It’s really like night and day.

    So, to answer your question, I definitely think the new option should be included and should probably be the default option.

  7. ToddM says:

    Personally, I agree with Geoff’s formattting, and if we can do that already, I guess I don’t care what new options you offer. Normally I wouldn’t post this fact, because that seems like a useless answer. However, maybe it’s a useful response if enough people agree and you can theregore avoid putting in a new option.

    Now, I’m normally against limiting choice, but managers are always looking for features they can cut…so maybe my response will make a PM happy somewhere.

  8. Geoff,

    I’m was wrong, you can’t do that today, but I’m probably going to make it so that that will work, regardless.

  9. *laughs*

    Good to see you correct yourself :)

    Doesn’t matter to me what can and can’t be done, i was just telling you what i’d prefer.

    If you choose to bow down before me and accept my preferences, then that’s only the world working as it should.

    If you discover that you won’t/can’t make it happen, i’m sure the world is no worse off – so long as i can still do option 3.

    Although, whatever the formatting chosen, i think it’s pretty safe in assuming that people won’t really care so long as there’s indenting involved, and the indenting makes sense and is clearly showing what block it’s encapsulating.

    I prefer the option where you bow before my judgement however :)

  10. Hein Mulder says:

    As a newbe, i prefer option 7.

  11. I’d prefer having the option.

  12. Luc Cluitmans says:

    Like Geoff, I think that example 3 is the clearest, with example 6 coming in second.

    As far as the indent option goes: I think having more options is always better…

  13. Zk says:

    I have to sorta take back my previous answer. I didn’t notice the indenting on #3. Of all the choices, I actually like that one the best. But, if I was to do it all inline (ie, have the curly brackets on the same line as the method code, I’d do something like #5. I guess that’s two completely different options, and I’d be stuck formatting manually for one or the other.

    Although, except for abstract properties, I almost never have curly brackets on the same line as my block code, so I guess I’d actually end up using the existing option as my default.

  14. Don says:

    When I’m writing ‘for’ loops in VS2003, they look like your option #3. That’s how I like them to look. I’m not aware of ever having messed with curly-brace-formatting options in VS, but it’s certainly certainly possible that I did. Assuming there are VS2003 curly-brace-formatting options (I’m out of town with a laptop that doesn’t have VS on it), I’d want VS2005 to respect the curly-brace formatting options that I set in VS2003 (which in this case would cause anonymous methods to look like method #3 for my newlining behavior). If there are no such options in VS2003, I’d want VS2005 to have defaults that are as close to VS2003 behavior as possible (which again would be #3 for people like me who use a lot of newlines).

  15. Hi Don,

    VS2003 only allows one option controlling braces, which is whether or not to put them on the next line. However, anonymous methods are a new language feature, so there isn’t yet much usage to determine how to format them.

    I see your point about for loops, however, what I think there is something that distinguishes anonymous methods from for loops and the like. For loops are always a statement by themselves, whereas anonymous methods are always embedded inside another statement (like a method call, or an assignment). I think the people who are requesting this option want it because they want some way to show that the anonymous method is embedded inside the other statement.

    Does that make sense?

  16. Sean Chase says:

    I think this is the most readable:

    List<Thing> somethings = things.FindAl(delegate(Thing thing)

    {

    return thing.ShouldBeIncluded;

    }

    );

  17. Ornus says:

    I like #4 or #7 (depending on the length of the method body). Would prefer by default.

  18. Ron says:

    I’d like to see Geoff’s second formatting or your [Kevin] seventh formatting. Indenting is generally preferable to not having it. I’d also recommend having this as the default (indenting turned on).