Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

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!

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top