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;
        }
    }
}

About Michael

I am a software developer and part-time professor. I enjoy studying and discussing mathematics, computer science and software development.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply