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?

Greg Smalter
  • 6,571
  • 9
  • 42
  • 63
  • 7
    @CaffGeek Without any context, I think jumping on the quantity of parameters is misplaced. – ErikE Jul 11 '14 at 17:49

3 Answers3

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!

Dmitry Osinovskiy
  • 9,999
  • 1
  • 47
  • 38
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!

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • @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. – frenchie May 25 '12 at 21:04
  • 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. – Sam Rueby Jun 04 '12 at 17:56
  • 2
    And how many arguments does the constructor for this new class take? Just askin? – Johannes Egger Aug 19 '16 at 06:19
  • 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 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. – Tim Sparkles Jan 20 '17 at 06:01
  • This doesn't answer the question. – kovac Jan 09 '19 at 01:49
  • @frenchie - 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. ;-) – mike Feb 27 '19 at 02:45
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.

Matt Beckman
  • 5,022
  • 4
  • 29
  • 42