3 ms·
It would be great if it was just so simple. The advice from that article ends up punting the complexity into hierarchy of objects/types. Which would be fine if
by TeMPOraL 6y ago
It would be great if it was just so simple. The advice from that article ends up punting the complexity into hierarchy of objects/types. Which would be fine if you never needed to be in several places of that hierarchy at once. Except in practice, you almost always do.
I've worked on a large C++ codebase that went with the "lots of small objects, strong typing everywhere" approach. Classes rarely had more than 5 fields or 5 methods. Data was aggressively transformed and narrowed so that, within any given function, you had in your hands a bunch of objects that contained only the things you needed.
The experience was me slowly going insane from the sheer amount of jumping around files and the need to keep in my head 30 different strongly-typed aliases to std::string.
I find myself leaning towards a belief that there is such thing as excessive subdivision, excessive separation. If you treat 7 +/- 2 as the size of your brain's L1 cache, then what you see in front of you is an L2 cache. Constantly swapping things between L1 and L2 is expensive, but nowhere near as expensive as between L1 and RAM (all the other files in your project you aren't looking at this very second).
--
Not to mention, objects don't communicate lifetime hierarchy well. If you split your Foo class into Foo, Bar, Baz and Quux, just to keep the size of the interface below 7, then when I see a Bar and Quux appearing somewhere in the code, it's not obvious to me that these are part of the same thing and always used together. Perhaps in a perfect world I shouldn't care, but most of the time I really do. This is probably a more general problem of OOP - structuring your program into a large graph of small objects, in conditions when you have near-zero visibility into how the entire graph looks, and you're forced to deal with only small pieces of it.
That picture in the article labeled "Complexity grows without bound"? That's exactly how OOP feels to me when you follow the proper OOP practices.
--
Edit: 'TheOtherHobbes expressed it perfectly here: https://news.ycombinator.com/item?id=24167120 https://news.ycombinator.com/item?id=24167120. Attributes are not the problem. But jumping up and down the ladder of abstraction, breaking in and out of encapsulation layers - that's what's mentally taxing.
- esperent 6y ago> If you treat 7 +/- 2 as the size of your brain's L1 cache, then what you see in front of you is an L2 cache. Constantly swapping things between L1 and L2 is expensive, but nowhere near as expensive as between L1 and RAM (all the other files in your project you aren't looking at this very second). As soon as file grows beyond a hundred lines or so, most of it is off-screen so it's not that different to putting it into another file (it's no longer in L2 cache to use your analogy). It's a choice between jumping around in a single big file, or jumping around between several smaller files organized in a folder tree structure. I have the folder tree open to the side of the editor while I work on the current document so that's also in my L2 vision cache. Personally I prefer that to one big file that I have to keep scrolling/jumping around in. Of course, for a large file I can open an outline view instead to help me jump around in the file. But I prefer files that are short enough so I don't need that.
- TeMPOraL 6y agoA file growing too big is indeed a problem, but it's partially mitigated by the fact that things in a single file are all directly related to each other (if they aren't, it's refactoring time). An outline view or even code folding also helps. On top of that, I can scroll within a single file quickly using single keystroke. Still, if you can keep a tree of all your source files entirely visible on screen, then your codebase is rather small. The kind of codebases I'm talking about, the tree doesn't fit. You have to actively search it to find the files you need. And each time someone decides a class isn't "small" anymore, the tree gains 2 or more extra files.
- rl3 6y agoThis is where data-oriented design really shines, without even considering the excellent performance. It affords a purity in design very similar to what's achievable with functional programming. Granted, what you describe very much sounds data-oriented, with the exception of a high degree of coupling, and that would squarely place it in OOP territory. >The experience was me slowly going insane from the sheer amount of jumping around files and the need to keep in my head 30 different strongly-typed aliases to std::string. As for that problem, I've been meaning to try this person's approach since I'm pretty sure they're a genius ever since reading it.[0] Likewise doing a projects in a small number of giant files can be beautiful. Sokol exemplifies this.[1] [0] https://news.ycombinator.com/item?id=23800729 https://news.ycombinator.com/item?id=23800729 [1] https://github.com/floooh/sokol/blob/master/sokol_gfx.h https://github.com/floooh/sokol/blob/master/sokol_gfx.h
- pbw 6y agoI don't advocate punting the complexity into a hierarchy of objects/types. Limiting yourself to seven attributes per object in not a panacea, you can create an utter shite design with small objects. We've all seen that. Let's turn it around. Do you feel that the more attributes in a class the better? So 10 is better than 5? And 20 is better than 10? I suspect not. I suspect you also feel there is a balance. I'm just saying that seven is an interesting number in the balance based on how our brains work. You want to make sure every additional object is pulling its conceptual weight. You want to make sure you minimize the accidental complexity while accommodating the necessary complexity. Balance in all things. Neither OOP or FP are cure-alls where if you just do those things you can turn off your brain and go on auto-pilot. Brain Oriented Programming is the same way, it's a guiding principle, it's something to keep in mind, but it's not so profound that it wipes out the need for good design judgment, engineering skills, or common sense.