Michal Olechowski
Michal Olechowski

Reputation: 646

Method containing foreach statment to have only one return

I have following method prepared for unit tests and i know it will always run for-each loop, is there a way to get rid of second return statement?

public Enums.GYRStatus GetStatusForTransformer(
            string factoryCode, 
            Enums.Technology technology, 
            string transformerType,
            int transformerSize,
            string transformerModel)
{
   fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string,
                                                    Enums.Technology,
                                                    string, int, string>, int>() 
   {
       { Tuple.Create("SELUD", Technology.CVT,"---", 0, ""), 1} };
   }

   foreach (var pair in fakeStandardsAndSizesFictionary)
   {
       if (pair.Key.Item1 == factoryCode &&
          pair.Key.Item2 == technology &&
          pair.Key.Item3 == transformerType &&
          pair.Key.Item4 == transformerSize &&
          pair.Key.Item5 == transformerModel)
           return (Enums.GYRStatus)pair.Value;
   }
   return (Enums.GYRStatus)1; // second return never used
}

Upvotes: 2

Views: 60

Answers (6)

Amit Tiwari
Amit Tiwari

Reputation: 368

public Enums.GYRStatus GetStatusForTransformer(
            string factoryCode, 
            Enums.Technology technology, 
            string transformerType,
            int transformerSize,
            string transformerModel)
{
   Enums.GYRStatus retValue=(Enums.GYRStatus)1;
   fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string,
                                                    Enums.Technology,
                                                    string, int, string>, int>() 
   {
       { Tuple.Create("SELUD", Technology.CVT,"---", 0, ""), 1} };
   }

   foreach (var pair in fakeStandardsAndSizesFictionary)
   {
       if (pair.Key.Item1 == factoryCode &&
          pair.Key.Item2 == technology &&
          pair.Key.Item3 == transformerType &&
          pair.Key.Item4 == transformerSize &&
          pair.Key.Item5 == transformerModel)
           retValue = (Enums.GYRStatus)pair.Value;
   }
   return retValue; 
}

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149558

Assuming (GYRStatus)1 is a valid return value, you could do:

GYRStatus status = GYRStatus.First;
foreach (var pair in fakeStandardsAndSizesFictionary)
{
   if (pair.Key.Item1 == factoryCode &&
   pair.Key.Item2 == technology &&
   pair.Key.Item3 == transformerType &&
   pair.Key.Item4 == transformerSize &&
   pair.Key.Item5 == transformerModel)
   {
      status = (Enums.GYRStatus)pair.Value;
      break;
   }
}
return status;

You could also return a GYRStatus? to indicate no value was selected:

GYRStatus? status = null;
foreach (var pair in fakeStandardsAndSizesFictionary)
{
   if (pair.Key.Item1 == factoryCode &&
   pair.Key.Item2 == technology &&
   pair.Key.Item3 == transformerType &&
   pair.Key.Item4 == transformerSize &&
   pair.Key.Item5 == transformerModel)
   {
      status = (Enums.GYRStatus)pair.Value;
      break;
   }
}
return status;

Or, you could have a GYRStatus.None value in your enum as well. It all depends on the flow of execution. If it is possible that you may end up not finding a value with your predicate, return a default value instead of throwing an exception. If it isn't possible, throw.

Upvotes: 2

AlexD
AlexD

Reputation: 32576

You can replace

return (Enums.GYRStatus)1;

with

throw new InvalidOperationException();

It also looks more correct semantically, assuming that this place should never be reached.

Upvotes: 7

Enigmativity
Enigmativity

Reputation: 117124

You could do this:

public Enums.GYRStatus GetStatusForTransformer(string factoryCode,   Enums.Technology technology, string transformerType, int transformerSize, string transformerModel)
{
    fakeStandardsAndSizesFictionary = new Dictionary<Tuple<string, Enums.Technology, string, int, string>, int>() 
    {
        {Tuple.Create("SELUD",Technology.CVT,"---",0 ,""),1},
    };

    return fakeStandardsAndSizesFictionary
        .Where(pair =>
            pair.Key.Item1 == factoryCode
                && pair.Key.Item2 == technology
                && pair.Key.Item3 == transformerType
                && pair.Key.Item4 == transformerSize
                && pair.Key.Item5 == transformerModel)
        .Select(pair => (Enums.GYRStatus)pair.Value)
        .First();
}

Upvotes: 2

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37050

There is no way for the compiler to know that the loop ALLWAYS returns a value. Therefor you have to provide that switch also.

Upvotes: 0

TomDoesCode
TomDoesCode

Reputation: 3681

There isn't, even if you know that it will always return, the compiler doesn't!

Upvotes: 0

Related Questions