Clean Tuples

Depending on the preferences of a developer you may sometimes find a lot of tuples within the source code. Whether this is good or bad practice is another discussion which is not part of this article. Therefore I don’t want to start a discussion whether you shall use classes, structs or tuples. I want to have a look at tuples form the “clean code” point of view.

In my opinion, tuples have one major disadvantage: The code to access tuples is not easy to read. If you use tuples and you write your code they are very easy to use. But in case you have to read, understand and maintain your code after a while or source code which was written by other developers, tuples can become a mess. The following source code shows an easy example. We define a tuple to store information about a person, we create a person tuple in some function and we access the elements of the tuple.

typedef std::tuple<std::string, std::string, int> Person;

Person CreatePerson()
{
  return std::make_tuple("John", "Doe", 35);
}

int _tmain(int argc, _TCHAR* argv[])
{
  Person person = CreatePerson();

  int age = std::get<2>(person);

  std::cout << age << std::endl;

  return 0;
}

This simple example is easy to read. You will understand the source code immediately and you are able to maintain the code. But I think that’s only possible because the whole source code will fit in one screen. What will happen if you use such tuples in a bigger context? The type definition will be placed in one file, the function to create the tuple in another file and the function which access the tuple elements is in a third file. In such a case you will lose the big picture and you need additional time to collect all the information. You cannot longer just read the code, you have to spend time and explore the code to find all needed information.

Have a look at the function to create the tuple. Within this function you have to know that the first parameter is the first name and the second parameter is the last name of the person. Therefore such source code has a high possibility for errors. And on the other hand if you want to use the tuple, you access the elements by an index. In case of the age parameter there is no issue as the used type is unique within the tuple. But if you want to access the first or last name of the person, you will find the same issue like before. Accessing by an index may lead to errors.

As a result of these issues we will think about a possible solution. How can we create clean source code? We found two issues: Creating the tuple and accessing the elements in a clear way. I think a very easy possibility is to exchange the not speaking index number with a speaking enumerator. By using an index enumerator you will be able to set and get the elements of a tuple by using their name. The following source code shows the adapted example.

typedef std::tuple<std::string, std::string, int> Person;

enum PersonIndex
{
  FirstName,
  LastName,
  Age
};

Person CreatePerson()
{
  Person person = std::make_tuple("", "", 0);

  std::get<FirstName>(person) = "John";
  std::get<LastName>(person) = "Doe";
  std::get<Age>(person) = 35;

  return person;
}

int _tmain(int argc, _TCHAR* argv[])
{  
  Person person = CreatePerson();

  int age = std::get<Age>(person);

  std::cout << age << std::endl;

  return 0;
}

This little modification will result in a lot of advantages. You type definition is followed by the according index enumerator which explains the tuple. So you don’t have to explain the tuple with a comment. The creation of the tuple can now be done with an explicit assignment of the parameter name and its value. And the access of the elements will also be done by using the parameter name. The resulting source code is now very easy to understand. You can read the code and will understand it immediately without the need of looking for some more information in other files. For example, it is no longer necessary to look for the type definition and an explanation of the elements. By using the index enumerator you can create “clean” tuples.

Werbung
Dieser Beitrag wurde unter .NET, C++, Clean Code veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s