How do I format so method parameters are stacked vertically, one per line?
Asked Answered
C

3

21

I have a method that I want formatted like this:

public static IQueryable<ThingRequest> GetThings( this EntityContext one
                                                , int? two = null
                                                , int? three = null
                                                , int? four = null
                                                , int? five = null
                                                , String six = null 
                                                , IEnumerable<String> seven = null) {

Basically, if the method definition is going to exceed the length of line line, I'd like there to be one parameter per line. I'm not too concerned about the commas (if they appear at the end of each line instead, that's fine).

But R# formats it like this, instead:

public static IQueryable<ThingRequest> GetThings( this EntityContext one, int? two = null, int? three = null, int? four = null, int? five = null,
                                                  String six = null, IEnumerable<String> seven = null ) {

... so, it lines them up, but there are several parameters per line and it's just hard to pick out any one parameter.

Incidentally, when calling methods, it stacks arguments one-per-line if the max line length is exceed (even though, in that case, I'd almost prefer it didn't).

I've gone into R# options and explored there wide array of check boxes available, but I don't see how to improve my situation. Ideas?

Cacoepy answered 25/5, 2012 at 20:37 Comment(1)
@CaffGeek Without any context, I think jumping on the quantity of parameters is misplaced.Misuse
O
22

Try changing option from this path:

ReSharper | Options -> 
Code Editing | C# | Formatting style | Line breaks and Wrapping -> 
Line wrapping | Wrap formal parameters

to Chop always. I don't know if it is possible to place comma the way you want, but at least there would be one parameter per line. Good luck!

Oaks answered 2/6, 2012 at 10:8 Comment(0)
D
2

Why not wrap these in an object and pass the object. Create a class! And then you're passing only one parameter.

public class MyParam
{
   public EntityContext one { get; set; }
   public Nullable<int> two { get; set; }
   .....
}

public static IQueryable<ThingRequest> GetThings(MyParam TheParameters) {...}

That way, later on, you could also add a method that validates parameters for instance.

And if you really wanted to be clever, you could add the GetThings method to this class and now you're talking OOP!

Defoe answered 25/5, 2012 at 20:48 Comment(6)
@Sam.Rueby: au contraire! I think the OP's real problem is not that the parameters don't fit in one line; the real problem is the way he's not encapsulating these and not using OOP principles. And that's the reason he's bumping in the apparent "my parameters don't fit in one line" problem. So I think my answer will solve his problem. Feel free to add your answer as well.Defoe
I understand. I just don't think it's realistic at all to create a new class every time a new method is created that takes more than a few parameters: that will just end up really messy. I also don't think it's realistic for people to suggest that a method should never be created that takes ~7 parameters. Yes, 20 is probably in poor-design-land. But 7 is more than legitimate, especially if following more functional-programming principals instead of OOP. I'm sure he knows he could create a class to encapsulate parameters; the real question here is regarding R# and its abilities.Excurrent
And how many arguments does the constructor for this new class take? Just askin?Kelbee
Make the class a POD with public properties and use object initializer syntax. Can't format in comments, but the declaration would look like IQueryable<Thing> GetThings(EntityContext context, ThingOptions options) and the call site like ThingGetter.GetThings(context, new ThingOptions { two = foo, three = bar, six = "" }); Obviously you want a constructor when parameters are required, but clearly that is not the case in this example with all its optional parameters.Runesmith
This doesn't answer the question.Lindalindahl
@Defoe - Use that approach with caution please. :-) Explicitly specifying parameters necessary to complete a function is the MOST IMPORTANT part of a function definition, and encourages good encapsulation. I had a boss (and very good friend) who tended to make giant structs, and pass them around, by reference, as parameters because it was quicker for him to write. It was always a huge guessing game about which parameters were used/altered by any particular function. ;-)Cambric
G
1

If you are having trouble picking out "any one parameter", then you should seriously consider adjusting how you should design this method.

Uncle Bob Martin ("Clean Code") recommends that you have 2-3 parameters max. If you are using more than that, then chances are you may not have the cleanest design possible, and it should be a mental hint to revisit why you want to design it like this.

Also, I realize this is not a direct answer to your question, but it might be an answer that makes your original question moot (if you decide you want to reduce the number of parameters). However, it's your code, so it's ultimately up to what you prefer.

Graduate answered 25/5, 2012 at 20:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.