Refactoring #5: From arrays to Data Transfer Objects

From arrays to Data Transfer Objects

Using an object instead of an array

This problem is perfect for refactoring to an object. In fact, these objects are called Data Transfer Objects (DTO), as they’re meant to transfer data of some kind, not necessarily with logic in it. In our case, it’s a ‘tags difference’ object, but we’ll try to find a proper name for it. There are some steps to perform this refactoring safely, but a simple, dummy implementation looks like this.

What’s inside the Data Transfer Object?

Generally, the key-value pairs we had in the previous array now become first-class citizens as private object properties. You’re going to have to write some getters for them.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Geni Jaho

Geni Jaho

Full-stack web developer with a passion for software architecture and programming best practices.