When in doubt, make a reference diagram.
Even if you only might be in doubt,
make a reference diagram.
It's going to seem slow,
painful, even to watch me do it.
When you're doing it for the first few times it's going to
be even slower and more painful,
but it's worth it because it gives you a way of understanding
things that can be pretty confusing otherwise.
So, here's a little code example.
Four lines and then we're printing out what's the result.
But it's not so easy to figure out exactly what's going to print out.
So, let's make a diagram to follow it.
We're going to basically simulate like what CodeLens would do.
We're going to do it ourselves.
So, x on line one gets bound to a list of three items: one, two, three.
On line two, y becomes an alias for x.
On line three we have this x plus equals.
Now, I gave you the suggestion that when you're dealing with lists,
just don't use plus equals.
But sometimes you have to read
other people's code and they don't follow that good advice.
So, suppose you had this x plus equals,
that's the version where we're going to take the new list: four and five,
and append them on to the old list,
so that we now have a list of five items,
and line three changes the object that x is pointing to.
Since y is pointing to that same object,
y now has five items as well.
Next, on line four,
we're going to use the better version that is a little easier to understand
where we do the normal thing of
figuring out the right hand side which takes the list: one,
two, three, four, five,
and appends another list to it,
making a new list.
So, then we have one, two,
three, four, five, that's a list.
We concatenate on this other list which contains six and then we reassign it.
That's what this equal is doing so that y no longer points to what it used to point to,
it now points to this new value.
X, however, still points to the old value.
So, when we print out x on line five,
we're going to get five items,
and we print out y on line six,
we're going to get all six items.
Let's check and see if that analysis is correct.
Sure enough, line five prints out five items,
line six prints out all six.
Trying to do this in your head,
especially if you ever end up with
more than four lines of code that are doing things like this,
very difficult to do.
So remember, if there's anything even remotely confusing,
make a reference diagram.
Practice now even when it might not be so confusing.
You'll thank me later. On to the next.