Hiding Inefficient Implementations

A few weeks ago I was writing some code to analyze matrices of two-dimensional data in C++. Obviously I needed some type of data structure and I just started with a vector of vectors approach.

std::vector<std::vector<T> > m;
for(unsigned int r = 0; r < 5; ++r)
m[3][2] = 3.14;

The code is relatively simple to write and works for a quick implementation. Unexpectedly, the idea I had showed promise so I continued writing more code to test my ideas further. About the tenth time I wrote std::vector<std::vector<double>>, I realized I was going to end up with some very ugly code if I did not do something about it.

There are several aspects of the vector of vectors approach that are not ideal.

  1. There is no restriction that the number of columns is equal for every row.
  2. Checking the number of columns requires accessing an element of the first vector. To be safe, that means also first checking that the size of the main vector is not zero.
  3. Each time a vector is initialized, it requires writing a loop.

What I really needed was a matrix class, so that I could start writing more readable code.

Matrix<double> m;
m(3,2) = 3.14;

The above code is much simpler and much easier to read compared to its first incarnation. The question becomes, how do I want to implement this class? Should I just implement it as a single vector? Should that vector be an std::vector or a C-style array? Should I just pick from one of the many Matrix libraries online?

These are questions that can take some time to answer properly. Chances are I will just need the code for the idea I am testing; spending time thinking about the most efficient or optimal way to implement a matrix will end up being a waste of time. Instead of worrying about the implementation, I simply encapsulated the vector of vectors approach into a Matrix class.

template<class T>
class Matrix
    std::vector<std::vector<double> > matrix_;
    unsigned int rows_;
    unsigned int cols_;

While I have not improved my inefficient implementation, I have hidden it from view. All code that uses a matrix will be much simpler and easier to read. If in the future I need to improve my implementation, I can simply modify the Matrix class without breaking any other code.

Simple encapsulations like this can save you many headaches in the future. When I am writing code, I try to stop and think how I would like the code to look if I was not constrained by the data structures and syntax of the language. Sometimes it is possible to write the code the way I wanted by moving the complexities of the implementation or syntax into another class. This Matrix class is just one example.

The complete Matrix class can be found on GitHub.

This entry was posted in Research and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s