0

I am struggling to understand the difference between using individual methods and optional parameters for accepting Get methods with query parameters for an ApiController.

I wrote my code in the browser so forgive any errors. I also left out other basic actions for brevity.

Here are my options for the given urls (I am planning on having many more query strings that can be optionally passed in):

GET http://myAppi.com/resources?color=red
GET http://myAppi.com/resources?color=red&shape=round

For my endpoints do I do the following:

[Route("")]
public IEnumerable<string> Get(string color)
{
     List<string> someList = new List<string>();
     //some selecting linq color logic here
     return someList;
}

[Route("")]
Public IEnumerable<string> Get(string color, string shape)
{
    List<string> someList = new List<string>();
    //some selecting linq color and shape logic here
    return someList;
}

Or do I use optional parameters...

[Route("")]
public IEnumerable<string> Get(string color, string shape = null)
{   
    List<string> someList = new List<string>();
    if (String.IsNullOrWhiteSpace(shape))
    {
        //some selecting linq color logic here
    }
    else
    {
        //some selecting linq color and shape logic here
    }
    return someList;
}

What's the difference?

S1r-Lanzelot
  • 2,206
  • 3
  • 31
  • 45
  • 1
    You're asking for opinions, which is off-topic. That said, I much prefer the first one. If the entire logic of the function changes based on an optional parameter, that function should be split into two methods. Also, some Web API documentation tools gripe about optional parameters like that. –  Dec 21 '17 at 22:26
  • @Amy Is there really no difference here? I am asking the question because I don't know. Perhaps there is a right and wrong answer. If these are truly equal and a matter of opinion, I'll delete the question. – S1r-Lanzelot Dec 21 '17 at 22:28
  • 1
    Well, yes, there's a difference, but that isn't the question that was asked. I recommend editing the question so you aren't asking which is better. –  Dec 21 '17 at 22:29
  • @Amy, updated... If you know, I would appreciate the share in knowledge :) Thank you. – S1r-Lanzelot Dec 21 '17 at 22:35
  • 1
    Not enough for a full answer: optional parameters are [sort of CLS-compliant](https://stackoverflow.com/questions/5456989/is-the-new-feature-of-c-sharp-4-0-optional-parameters-cls-compliant). This *probably* doesn't matter *at all* to you, but it's a difference between your code examples nonetheless. One is CLS-compliant while the other is *shrug*. –  Dec 21 '17 at 22:39

1 Answers1

2

While your first example could be viewed as more precise, and better suited (if you really have different functionality in each function), some would consider it verbose.

That being said, there are some dire problems that can occur at a later stage when you use optional parameters (in example 2). (Although I kinda like example 2)

See here, where it explains the pros and cons...But the gravest con being that the parameters are filled at compile time, not run time...So any interface changes, or changes in your optional param value may NOT be picked up by other libraries that call your function. So they may actually pass an older default value - if you decide to change the default value to something new.

Grantly
  • 2,546
  • 2
  • 21
  • 31