# Review of changes and working with diffs

A very popular tool when working with code is the diff.

A diff is a text file that shows you the changes (or differences) between two files.

For example, in this section we made changes to database.py. One function in particular, create_tables() went from this:

def create_tables():
    with connection:
        connection.execute(CREATE_MOVIES_TABLE)
        connection.execute(CREATE_USERS_TABLE)
        connection.execute(CREATE_WATCHED_TABLE)

To this:



 
 
 
 

def create_tables():
    with connection:
        with connection.cursor() as cursor:
            cursor.execute(CREATE_MOVIES_TABLE)
            cursor.execute(CREATE_USERS_TABLE)
            cursor.execute(CREATE_WATCHED_TABLE)

If we were to generate a diff for this change, it would look like this:

 def create_tables():
     with connection:
-        connection.execute(CREATE_MOVIES_TABLE)
-        connection.execute(CREATE_USERS_TABLE)
-        connection.execute(CREATE_WATCHED_TABLE)
+        with connection.cursor() as cursor:
+            cursor.execute(CREATE_MOVIES_TABLE)
+            cursor.execute(CREATE_USERS_TABLE)
+            cursor.execute(CREATE_WATCHED_TABLE)

As you can see, the diff shows which lines were removed (the old code), and which were added (the new code).

It can be tricky to read diffs, especially the first few times you do it, because even the smalles change from one line to another (e.g. in this case, the indentation changes) results in one line being remove and one line being added.

However diffs are extremely handy and once you're accustomed to them, they very quickly tell you what has changed.

Here's a complete diff of the database.py changes for this section: https://www.diffchecker.com/MNOMWdNn (opens new window).

On that website you can see the diff as "Split" (original text on left, new text on right). Earlier on in this chapter we were showing the diff as "Unified". Unfortunately the site does not support "Unified" in its free plan, but hopefully this is helpful anyway!