Refactoring 012 - Convert Your Key/value Into Full Behavioral Objects

https://hackernoon.imgix.net/images/RIiBoPtpMiRsMKX3dnzl5gb1Urj1-mr83aa5.jpeg

Converting your anemic dictionaries is easy

TL;DR: Convert your key/value into full behavioral objects

Problems Addressed πŸ˜”

Context πŸ’¬

You have anemic associative arrays that hold unstructured data.

You want richer objects with stricter controls.

Static typed languages can also add type checking to these objects.

Steps πŸ‘£

  1. Find the references to the object or associative array
  2. Reify it
  3. Replace generic calls with setters and getters for every key.

You can also debug them better this way.

  1. Add parameter and return type hinting to interfaces.

Do this if your language supports it.

  1. Add stronger assertions on the setters between different keys.

(if you are using TCR, you can do baby refactoring steps)

Sample Code πŸ’»

Before 🚨

<?class AuthenticationHelper extends Singleton { private $data = []; function setParameter(string $key, ?$value) { // no type checking...

Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE

Read more