Search code examples
c#outref

ref out default values


I'm still trying to learn and since I don't know many peers who have a good programming knowledge I told myself to start asking more questions about good programming practices if I can't find the correct answer on the internet.

I wonder what is the best approach for this scenario. I've had a function that was supposed to count a parameter based on some data that was also counted.

private float CountAvailability(DateTime startDate, DateTime endDate, string machine)
{
      float value=0;

      float machineUptime = _repostory.Select(machine);

      float machineDownTime = _repostory2.Select(machine);

      value = machineUptime *machineDownTime ;

      //some other magic here

      return value;
}

Of coruse this is a sample code and in reality it's much more complex.

Now I'm using this function in few other places in my code already and now there is a need to pass few other parameters from it as well. I'd prefer not to count them again in some other place and I don't want to repeat my code as well to create a copy of that function just for that purpose, so I thought of using ref or out.

private float CountAvailability(DateTime startDate, DateTime endDate, string machine,
                                ref float machineUptime , ref float machineDownTime )
{
      float value=0;

      float machineUptime = _repostory.Select(machine);

      float machineDownTime = _repostory2.Select(machine);

      value = machineUptime *machineDownTime ;

      //some other magic here

      return value;
}

Now I can get some other parameters out of the function. The only problem is I don't want to do that in every place I use the function.

Some of the places it's like this

CountAvailability(tempStartDate, tempEndDate, machine , ref machineUptime, ref  machineDownTime )

and in others the function should stay the same.

CountAvailability(tempStartDate, tempEndDate, machine)

but I have to pass an empty declared float in order for this to work. Is there another way to get this to work ? Or maybe there is another cleaner solution for this?

Best regards!


Solution

  • The simplest solution (in terms of changing very little code) would be to have an overload:

    private float CountAvailability(DateTime startDate, DateTime endDate, string machine)
    {
        float ignored1 = 0f, ignored2 = 0f;
        return CountAvailability(startDate, endDate, machine, ref ignored1, ref ignored2);
    }
    

    However, you should make these out parameters instead of ref parameters, as you don't use the existing value.

    You should also consider changing the method to something like:

    private Availability GetAvailability(DateTime startDate, DateTime endDate,
                                         string machine)
    

    where Availability would include everything you're currently indicating with the return value and out parameters. Then you don't need an overload - you just ignore whatever bits of the result you're not interested in. (That means wasted work, of course... if it's expensive to compute one of those pieces of information which isn't used, you may well want to consider alternative strategies.)