How to clean a method with it?

39 Views Asked by At

I have an iterator:

 public List<int> Retrieveints(DataToEvaluate dataToEvaluate)
 { 
     IEnumerable<int> Retrieveints()
     {
 
         if (!IsValid(dataToEvaluate.complexProperty))
         {
             yield return 0;
         }
     }
 }
 

Do you think that I should move out the method?

 public List<int> Retrieveints(DataToEvaluate dataToEvaluate)
 { 
    return IsValid(dataToEvaluate.complexProperty, out int)
 }
1

There are 1 best solutions below

0
JonasH On

If you need to move await from the iterator block I would probably just do that completely, and just add the errors to a list instead, i.e.

public ErrorsResult RetrieveErrors(DataToEvaluate dataToEvaluate)
 {
     var errors = new List<string>();
     if (dataToEvaluate.info == null)
     {
         errors.Add("Info doesn't contain information");
     }
     else{
         errors.AddRange(GetErrorsFromOtherInfo(dataToEvaluate.otherErrors));
    }

     if (!IsValid(dataToEvaluate.complexProperty, out var calculatedResult))
     {
         errors.Add("Complex property is not valid");
     }

     return new ErrorsResult(calculatedResult, errors);
 }

That should be a bit simpler to read and understand.