1

I have a method with two optional parameters. I want to shorten my code.
here is my code:

DataTable dtList;
if (!duration.ContainsKey("startDay") && duration.ContainsKey("endDay"))
{
    dtList = GetAllReservation();
}
else if (duration.ContainsKey("startDay") && !duration.ContainsKey("endDay"))
{
    dtList = GetAllReservation(duration["startDay"]);
}
else
{
    dtList = GetAllReservation(duration["startDay"], duration["endDay"]);
}

is there any way to shorten this code to something like this:

dtList = GetAllReservation(duration["startDay"], duration?["endDay"]);

this is my method GetAllReservation:

public static DataTable GetAllReservation(string start = "1397/01/01", string end = "1400/12/29") =>
    DataAccess.Select($"Exec ReservationList '{start}', '{end}'", ref _methodState);
5
  • Your shortened code is missing the ContainsKey check, and therefore will throw if key is not found. Commented Feb 25, 2019 at 6:43
  • can you show me the signature of GetAllReservation method? Commented Feb 25, 2019 at 6:47
  • Maybe a default value of 'null' for the params and the ?? operator in the SQL string to supply the real defaults? Commented Feb 25, 2019 at 6:50
  • @CoolBots Do you really think your edit made this post more readable? And what is Data Table? Commented Feb 25, 2019 at 6:54
  • @SeM the original message had no code formatting or syntax highlighting at all; so yes, my edit did make it more readable. My apologies for the "Data Table", phone autocorrect... Commented Feb 25, 2019 at 7:10

5 Answers 5

3

Do not define the parameter default to be the business default. Define the parameter default to be null, which signifies a lack of value. The method itself should check for the parameter default and substitute the business default as needed.

public static DataTable GetAllReservation(string start = null, string end = null) 
{
    if (start == null) start = _config.GetDefaultStartDate();
    if (end == null) end = _config.GetDefaultEndDate();

    DataAccess.Select($"Exec ReservationList '{start}', '{end}'", ref _methodState);
}

Also, you can write an extension method on Dictionary:

public static string GetStringOrNull(this Dictionary<string,string> source, string key)
{
    if (!source.ContainsKey(key)) return null;
    return source[key];
}

Which allows you to shorten your call to this:

GetAllReservation(duration.GetStringOrNull("startDay"), duration.GetStringOrNull("endDay"));
Sign up to request clarification or add additional context in comments.

Comments

1

Try this one,

 dtList = GetAllReservation(duration.ContainsKey("startDay")?duration["startDay"]:"1397/01/01",duration.ContainsKey("endDay")?duration["endDay"]:"1400/12/29");

Comments

1

you can do like this

string stDate = duration.ContainsKey("startDay") ? duration("startDay") : null;
string edDate = duration.ContainsKey("endDay") ? duration("endDay") : null;

dtList = GetAllReservation(stDate ,edDate );


public static DataTable GetAllReservation(string start = null, string end = null) 
{
    if (start == null) start = ""; //Set default value
    if (end == null) end = "";//Set default value

    //..... further code
}

2 Comments

This code will pass null instead of using the optional parameter if the key is missing
sorry I forgot to add updated "GetAllReservation" method .. now update the answer
0

In my opinion, good programming does not necessarily means shorter code. It may be of value to think of each case as a separate method. If each method has a specific function, the logic of each method becomes simpler. It also reduces coding errors and possible exceptions. In this case you have 3 methods. Each method does a slightly different job depending on the number of parameters, but at the end the return the same sort of result. This looks like a good case of method overloading. I believe that method overloading is good as long as the method(s) really do the same core function only with different parameters. Some think that method overloading is Evil.

Comments

0

No need of optional parameters here (if you really want shorter version of your code and may be more readable):

string startDayDuration = duration.ContainsKey("startDay") ? duration["startDay"] : "1397/01/01";
string endDayDuration = duration.ContainsKey("endDay") ? duration["endDay"] : "1400/12/29";

dtList = GetAllReservation(startDayDuration, endDayDuration);

then your method:

public static DataTable GetAllReservation(string start, string end) =>
    DataAccess.Select($"Exec ReservationList '{start}', '{end}'", ref _methodState);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.