...

/

Improving the Phone Book Application II

Improving the Phone Book Application II

Let’s learn about the enhancements to improve the functionality and usability of the phone book application.

Adding an index

Let’s first discuss how the database index is implemented with the help of a map. Indexing in databases is based on one or more keys that are unique. In practice, we index something that is unique and that we want to access quickly. In the database case, primary keys are unique by default and can’t be present in more than one record. In our case, phone numbers are used as primary keys, which means that the index is built based on the phone number field of the structure.

Note: As a general rule, we index a field that is going to be used for searching. There is no point in creating an index that is not going to be used for querying.

Working on an index

Let’s now see what this means in practice. Imagine that we have a slice named S with the following kind of data:

Press + to interact
S[0]={0800123123, ...}
S[1]={0800123000, ...}
S[2]={2109416471, ...}
.
.
.

So, each slice element is a structure that can contain much more data apart from the telephone number. How can we create an index for it? The index, which is named Index, is going to have the following data and format:

Press + to interact
Index["0800123123"] = 0
Index["0800123000"] = 1
Index["2109416471"] = 2
.
.
.

This means that if we want to look for the telephone number 0800123000, we should see whether 0800123000 exists as a key in Index. If it is there, then we know that the value of 0800123000, which is Index["0800123000"], is the index of the slice element that contains the desired record. So, because we know which slice element to access, we do not have to search the entire slice. With that in mind, let’s update the application.

The improved version of the phone book application

It would be a shame to create a phone book application that has its entries hardcoded in the code. This time, the entries of the address book are read from an external file that contains data in CSV format. Similarly, the new version saves its data into the same CSV file, which we can read afterward.

Phone book entry format

Each entry of the phone book application ...