4 ms·
I have seen this in the wild, in the form of code that compared objects by asking them to serialize themselves to JSON and then comparing the JSON. It was a cl
by dkarl 6y ago
I have seen this in the wild, in the form of code that compared objects by asking them to serialize themselves to JSON and then comparing the JSON. It was a classic case of a system where behavior and data had no reason to be mixed, except the programmers had been taught that the only correct way to view data was as the behavior of an object. Luckily, each object in the system needed to be able to reproduce the record it represented in JSON form for the purpose of interacting with other systems. That allowed us to check whether two objects created by different subsystems represented the same record.
I have been out of that mindset for so long it is strange to remember, but I do remember: records coming out of a database or an API request were weird and gross. They weren't encapsulated, and they didn't do anything. You weren't supposed to touch the insides of an object, and records were NOTHING BUT INSIDES. It was unsettling, like seeing a picture of a grisly accident. You're not supposed to see the insides. The first thing we always did was hide those fields safely in an object. Whew. Only touch them with getters and setters. Much nicer; much safer. Thank goodness for OOP.