Home Brainmess: Extract Tape Class (Cleanup)
Post
Cancel

Brainmess: Extract Tape Class (Cleanup)

This is just a quick update. My last refactoring on Brainmess was to extract out the tape class. While reviewing that code today, I noticed some artifacts left over from the refactoring process. These were cleaned up in commit 836a96

Four of the methods: MoveForward, MoveBackward, Increment, Decrement still had return values that were not needed. For example before we extracted out the tape class, this is what the MoveForward method looked like:

int MoveForward()
{
    return tc++;
}

and this is how it was called from the Run method:

switch(instruction)
// ... snip
{
case '>':
    MoveForward();
    break;

// snip...

The MoveForward method and the tc variable were then moved into the Tape class. You can see however that there is no need for the MoveForward method to return anything. It can be simplified to the simple expression tc++.

Similar changes can be made for the other three methods so that we get this for the Tape class.

public class Tape
{
    private readonly int[] tape = new int[5000];
    private int tc = 2500;

    public void MoveForward()
    {
        tc++;
    }

    public void MoveBackward()
    {
        tc--;
    }

    public void Increment()
    {
        tape[tc]++;
    }

    public void Decrement()
    {
        tape[tc]--;
    }

    public int Current
    {
        get
        {
            return tape[tc];
        }
        set
        {
            tape[tc] = value;
        }
    }
}
This post is licensed under CC BY 4.0 by the author.

Brainmess Description

Loop Invariant Proofs