Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 458
Greg Smalter Avatar asked May 25 '12 20:05

Greg Smalter


3 Answers

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!

like image 125
Dmitry Osinovskiy Avatar answered Nov 15 '22 03:11

Dmitry Osinovskiy


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!

like image 30
frenchie Avatar answered Nov 15 '22 04:11

frenchie


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.

like image 1
Matt Beckman Avatar answered Nov 15 '22 04:11

Matt Beckman