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:
memories is never used in any output — it's dead, accumulated state. Each call to PredictNext appends 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), the memories list grows unboundedly, and the echoes list 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).
Bug: Accidental mutable static state in
ChamberOfEchoesFile:
Solutions/CSharp/The-Chamber-of-Echoes.cs, lines 3–4Problem
Both
echoesandmemoriesare declared asstaticclass-level fields: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.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 thememoriesside-effect entirely fromPredictNext: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).