{<3>}

How you save data will determine how good the component you are working on will be. Let us work through a demonstration of what that means.

Lets say what you want to do is represent a line.

int x1,x2,y1,y2;

You can represent your line like this, with x1 and y1 representing one of the coordinates and x2 and y2 representing the other point.

Now this might seem to be a reasonable enough way of representing the line. However, lets try to use it in different scenarios.

Lets say we are trying to define a method that accepts input as a line.

void RandomMethod(int x1, int y1, int x2,int y2)
{
	//Do something with the line
}

Now we know this works but the problem is that it is too verbose. As a programmer you spend a huge part of your day writing line after line of code and so you want things to be as concise as possible. Also, it is inflexable. For example, what if we start representing 3d lines. If this RandomMethod doesn't actually care about the contents of the line but it instead passes it to other function, shouldn't you be able to change the format of the line without having to change your RandomMethod? Also, are ints the best way to represent line coordinates?

So, so far we have identified two problems with this structure. We need to be able to change the internal structure of the line without having to change everything that deals with the line. Also, int sucks.

Here is what our next version looks like,

double line[4];

So now we have something a little more interesting. All the different points that define the line are in one neat variable. Also, doubles rock.

So now we change out RandomMethod to,

void RandomMethod(double line[])
{
	//Do something with the line
}

Now even if we have a hundred points, our method doesn't have to care if it is just passing them on.

Seems like we are doing good. Now lets think about it. What are some of the things we do with a line?

Lets see what the code for checking the length of the line looks like.

double GetLengthOfLine(double line[])
{
  double xlength = line[2] - line[0];
  double ylength = line[3] - line[1];
  return sqrt(xlength * xlength + ylength * ylength);
}

For someone looking at your code for the first time, it might not be easy for them to figure out exactly what you are doing. It might seem easy for this case but in general its not. Now, lets see if we can make this something thats much easier to read.

struct Line
{
  double x1;
  double y1;
  double x2;
  double y2;
};

Now this is a lot more interesting. Now our GetLengthOfLine method looks like this,

double GetLengthOfLine(Line line)
{
  double xlength = line.x2 - line.x1;
  double ylength = line.y2 - line.y1;
  return sqrt(xlength * xlength + ylength * ylength);
}

Now for anyone reading out code, it is much more easier to figure out what contains what. And it also saves us from having to create a convention of what index is what coordinate (eg. is index 1 x2 or y1?).

Okay. What else do we do with lines? We add lines as vectors right?

{<2>}

How would we do this with our current code? First we would need a method that takes two lines. After that, we would need to add the two together and return a new line that results from them.

Line AddLines(Line line1, Line line2)
{
  return Line = {
    line1.x1,
    line1.y1,
    line2.x2,
    line2.y2
  };
}

So now that we have this function, given two lines, line1 and line2, how would we add them?

Line lineSum = AddLines(line1, line2);

Thats nice. But imagine if we are doing adding four lines together, line1, line2, line3, and line4

Line SumOfFour = AddLines(AddLines(AddLines(line1, line2), line3), line4);

Now that looks ugly. And only for something that should be super easy to add. Well, lets see if modifying how we store the lines and adding some handling can help.

class Line
{
  double x1;
  double y1;
  double x2;
  double y2;
  
  Line operator+(const Line line1, const Line line2)
  {
    double xlength = line.x2 - line.x1;
    double ylength = line.y2 - line.y1;
    return sqrt(xlength * xlength + ylength * ylength);
  }
};

So now, to add four lines together we have,

Line SumOfFour = line1 + line2 + line3 + line4;

Imaging we were still using arrays and you would quickly realize the kind of headache you would have caused yourself trying to do the line above.

I hope this shows you how important it is to pick the right datastructures. I picked this example because it was the easiest ;). Also, the code is mostly C++ but I haven't even run it and if I have syntax errors (or even wrong things in there), the idea was never to write code but rather to talk about how to design parts of code.