Refactoring Replace Array with Object

This refactoring technique is a special case of Replace Data Value with Object.


You have an array that contains various types of data.


Replace the array with an object that will have separate fields for each element.
String[] row = new String[2];
row[0] = "Liverpool";
row[1] = "15";
Performance row = new Performance();
string[] row = new string[2];
row[0] = "Liverpool";
row[1] = "15";
Performance row = new Performance();
$row = array();
$row[0] = "Liverpool";
$row[1] = 15;
$row = new Performance();
row = [None * 2]
row[0] = "Liverpool"
row[1] = "15"
row = Performance();

Why Refactor

Arrays are an excellent tool for storing data and collections of a single type. But if you use an array like post office boxes, storing the username in box 1 and the user's address in box 14, you will someday be very unhappy that you did. This approach leads to catastrophic failures when somebody puts something in the wrong "box" and also requires your time for figuring out which data is stored where.


  • In the resulting class, you can place all associated behaviors that had been previously stored in the main class or elsewhere.

  • The fields of a class are much easier to document than the elements of an array.

How to Refactor

  1. Create the new class that will contain the data from the array. Place the array itself in the class as a public field.

  2. Create a field for storing the object of this class in the original class. Do not forget to also create the object itself in the place where you initiated the data array.

  3. In the new class, create access methods one by one for each of the array elements. Give them self-explanatory names that indicate what they do. At the same time, replace each use of an array element in the main code with the corresponding access method.

  4. When access methods have been created for all elements, make the array private.

  5. For each element of the array, create a private field in the class and then change the access methods so that they use this field instead of the array.

  6. When all data has been moved, delete the array.

Tired of reading?

No wonder, there are 7 hours worth of the text on this website.

Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.

Learn more...

Live Example

First time here? No worries!

Everything is quite simple. This example is like video (but much cooler):

  1. After pressing the Play button, you will see floating messages, which will guide you through the example. You can proceed by clicking on them.
  2. You can fast-forward or return to previous steps by pressing arrow buttons on the left.
  3. Also, you can take a look at the code diff window, which will show what has changed in code during the example ()
  4. To check the code for errors, you can press "Compile and test" button ()