Code Smell – Why Are Data Classes Considered a Code Smell?

code smelldata structures

This article claims that a data class is a "code smell". The reason:

It's a normal thing when a newly created class contains only a few
public fields (and maybe even a handful of getters/setters). But the
true power of objects is that they can contain behavior types or
operations on their data.

Why is it wrong for an object to contain only data? If the core responsibility of the class is to represent data, wouldn't add methods that operate on the data break the Single Responsibility Principle?

Best Answer

There is absolutely nothing wrong with having pure data objects. The author of the piece quite frankly doesn't know what he's talking about.

Such thinking stems from an old, failed, idea that "true OO" is the best way to program and that "true OO" is all about "rich data models" where one mixes data and functionality.

Reality has shown us that actually the opposite is true, especially in this world of multi-threaded solutions. Pure functions, combined with immutable data-objects, is a demonstrably better way to code.

Related Topic