Skip to content

Removal branch uses a linear Remove where RemoveAt(0) is direct #10

Description

@dmccoystephenson

Problem

In src/CubeFactory.cs, the removal branch looks the element up by index and then removes it by value:

// remove oldest cube
GameObject oldestCube = cubes[0];
cubes.Remove(oldestCube);
Destroy(oldestCube);

List<T>.Remove performs a linear scan for the first reference equal to oldestCube, even though the index being removed is already known to be 0. cubes.RemoveAt(0) expresses the same operation directly.

Equivalence note

The two forms are equivalent only if the list can never hold the same reference twice. Every entry is added from a distinct Instantiate(cubePrefab) result, and nothing else writes to cubes at runtime, so duplicate references cannot arise through the component's own code path. cubes is a public serialized field, however, so a consuming project could in principle populate it by hand in the Inspector — that possibility is worth the owner's consideration before the change is made.

Suggested resolution

cubes.Remove(oldestCube) should be replaced with cubes.RemoveAt(0), and the local oldestCube should be retained so the reference is still available for the following Destroy call.

Verification note

This is a change to executable code in src/CubeFactory.cs. Although it is intended to be behaviour-preserving, confirmation requires a play-mode run in the Unity Editor; no such run backs this report, which was derived from reading the source.

This issue was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions