-
Notifications
You must be signed in to change notification settings - Fork 429
Open
Description
Bug: Accidental mutable static state in ChamberOfEchoes
File: Solutions/CSharp/The-Chamber-of-Echoes.cs, lines 3–4
Problem
Both echoes and memories are declared as static class-level fields:
static List<int> echoes = new List<int> { 3, 6, 9, 12 };
static List<int> memories = new List<int>();This causes two issues:
memoriesis never used in any output — it's dead, accumulated state. Each call toPredictNextappends to it permanently, but nothing reads it back.- State persists across calls — in any context where
Run()is called more than once (e.g., tests, REPL), thememorieslist grows unboundedly, and theechoeslist is shared global mutable state.
Fix
Move both lists to be local variables inside Run() and remove the memories side-effect entirely from PredictNext:
public class ChamberOfEchoes
{
static int PredictNext(List<int> echoes)
{
int difference = echoes[1] - echoes[0];
return echoes[echoes.Count - 1] + difference;
}
public static void Run()
{
var echoes = new List<int> { 3, 6, 9, 12 };
Console.WriteLine(PredictNext(echoes));
}
}Impact
Code correctness and educational quality — this is a teaching repo, and the current pattern models a bad practice (unread side effects on static state).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels