Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 232
Michal Olechowski Avatar asked Feb 24 '26 05:02

Michal Olechowski


1 Answers

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.

like image 174
AlexD Avatar answered Feb 25 '26 19:02

AlexD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!