Put the Logic with the Data

Jul 18, 2017
-
2
minute read

The understandability of code is directly related to the distance between the data and the code that makes decisions based on that data. The fewer places you have to look to see the actual behavior of the system, the easier it is to comprehend.

Recently, I was working with a lower-level system that doesn’t have UI controls, and the project needed the ability to let objects respond to various Mouse interactions. I built the MouseStateActions component to hold the data that describes what is to be done as its relationship to the mouse changes:

Whenever you see code in one class manipulating more than a single Field, Variable or Property in another class, this is a sure sign that a refactor is needed. I simplified the system by moving the logic that acts on the data into the class that holds the data.

This offers two benefits. First, this allows the MouseStateActions to hide its CurrentState and ClickedAt properties. This ensures that only the declarative properties can be set. Secondly, the MouseStateProcessing system is now much, much clearer. The code clearly indicates that he is an intermediary between the Mouse and the object with MouseStateActions.

Furthermore, this reduces the coupling between the two since now there is no data passing between the System and the Component. This reduces the chance of bugs, and makes it easier to modify the behavior of MouseStateActions if further logic or state-tracking is needed in the future.

For data structures that you own, whenever and wherever possible, keep the logic and utility functions alongside the data.