So you needed to draw a grid … or a ruler … or another x-y-ish vector graphics and you ended up with code snippets like this everywhere:

public void Grid(Graphics g, Size size, int step) {
    for (int i = 0; i < size.Width; i+=step)
    {
        g.DrawLine(Pens.Black,
            new Point(i, 0),
            new Point(i, size.Height)
        );
    }
    for (int i = 0; i < _size.Height; i += step)
    {
        g.DrawLine(Pens.Black,
            new Point(0, i),
            new Point(size.Width, i)
        );
    }
}
You can avoid redundancy in such situations by using the strategy pattern. First create a new interface called IDirection.
public interface IDirection
{
    int GetMoving(Size size);
    int GetStatic(Size size);
    void AddMoving(ref int x, ref int y, int step);
    Point GetPoint(int moving, int static_);
}
Now imagine yourself free- falling into a deep hole. Remember, you are a programmer. So stop screaming and start thinking about the fall. Your moving direction is vertical. And since you are in a vertical free- fall you almost don’t move in x direction. Therefore your static direction is horizontal. Now write this in code.
public class VerticalDirection : IDirection
{
    public int GetMoving(Size size)
    {
        return size.Height;
    }

    public void AddMoving(ref int x, ref int y, int step)
    {
        y += step;
    }

    public Point GetPoint(int moving, int fixed_)
    {
        return new Point(fixed_, moving);
    }

    public int GetStatic(Size size)
    {
        return size.Width;
    }
}
And a HorizontalDirection class.
class HorizontalDirection : IDirection
{
    public int GetMoving(System.Drawing.Size size)
    {
        return size.Width;
    }

    public void AddMoving(ref int x, ref int y, int step)
    {
        x += step;
    }


    public Point GetPoint(int moving, int fixed_)
    {
        return new Point(moving, fixed_);
    }

    public int GetStatic(Size size)
    {
        return size.Height;
    }
}
There we go. Static and moving direction for each concrete direction class are encapsulated and we can now write new grid drawing code.
public void Grid(Graphics g, Size size, int step)
{
    Grid(g, size, step, new VerticalDirection());
    Grid(g, size, step, new HorizontalDirection());
}

public void Grid(Graphics g, Size size, int step, IDirection direction)
{
    for (int i = 0; i < direction.GetMoving(size); i += step)
    {
        g.DrawLine(Pens.Black,
            direction.GetPoint(i, 0),
            direction.GetPoint(i, direction.GetStatic(size))
        );
    }
}

Sometimes you want to know index of the minimal value of an array. I’ve frequently seen programmers using two data structures to accomplish this task: one to hold the index and the other to hold the value (i.e. current minimum) while iterating. Unless you need to optimize your code for speed you really only need an integer for this task.

int find_min(int* vals, int len)
{
    int mindx= 0;
    for (int i = 1; i < len; i++)
        if (vals[i] < vals[mindx]) mindx= i;
    return mindx;
}

Newer Posts Older Posts Home

Blogger Syntax Highliter