C#C
C#13mo ago
Torn

How would you abstract these two functions together most cleanly?

readonly LinkedList<Game> games = new();
readonly HashSet<int> ids = [];
int start = 0;
public int jsonExceptions;

public LinkedList<Game> Fetch(Func<LinkedList<Game>, bool> ContinueCondition) {
    int retryCount = 0;
    while (retryCount < 3) {
        try {
            while (AppendGames() && ContinueCondition(games)) {
                retryCount = 0;
            }
            break;
        } catch (Exception ex) {
            if (ex is JsonException) {
                jsonExceptions++;
            } else {
                Log.Write($"Error while fetching games on entry {start}, retry {retryCount}:\n{ex}");
            }
            retryCount++;
            System.Threading.Thread.Sleep(3000);
        }
    }

    FixDates();

    return games;
}

public HashSet<int> FetchIDs() {
    int retryCount = 0;
    while (retryCount < 3) {
        try {
            while (AppendIDs()) {
                retryCount = 0;
            }
            break; 
        } catch (Exception ex) {
            if (ex is JsonException) {
                jsonExceptions++;
            } else {
                Log.Write($"Error while fetching games on entry {start}, retry {retryCount}:\n{ex}");
            }
            retryCount++;
            System.Threading.Thread.Sleep(3000);
        }
    }

    return ids;
}

I feel bad about duplicating the code, but with different inputs and outputs, I'm not quite sure if there's a clean way to unduplicate this code.
Was this page helpful?