DEV Community

Discussion on: Refactoring — oops, I’ve been doing it wrong.

Collapse
 
curtisfenner profile image
Curtis Fenner • Edited

I would not be very happy coming along such a class User; I'd much prefer the first, plain data object (assuming I have trustworthy type-hints, à la TypeScript). The plain data object has a clear interface, and is completely free of any irrelevant implementation details; the class provides the potential for hidden dependencies, and doesn't expose any obvious API, plus exposes a mutation API.

A generic string key → string value get is the worst possible API to use a simple object. The set is also dubious; you should have a very good reason before you add mutators to objects. Most code is easiest to follow when as much as possible remains immutable.

There's nothing wrong with plain data objects. They're quite often the best way to write your code; assuming anything other than fully-fledged classes are a 'quick and dirty first pass' is the reason why so many people write so negatively about OOP.