4 ms·
I love that. Chunking by textual grouping is great. Sometimes that is totally enough. One really nice thing about sub-objects though is you can pass them to and
by pbw 6y ago
I love that. Chunking by textual grouping is great. Sometimes that is totally enough. One really nice thing about sub-objects though is you can pass them to and from functions like:
info = get_owner_info(event.owner)
info.record_duration(event.span)
I usually like that better than:
info = get_owner_info(event.process_id, event.thread_id)
info.record_duration(event.start_ns, event.end_ns)
If several attributes are passed around together through several functions, I'm much more likely to consider making a sub-object. It's 100% true that sub-objects and hierarchy have costs of their own. Nothing is free. If the sub-objects are not pulling their conceptual weight they should probably be removed. I used the Bonsai metaphor because those trees require constant trimming and shaping. There's a name for unchecked growth, budding sub-objects willy nilly: cancer.
The bigger thing though is pushing functionality down into the sub-objects. For example if my server has these three attributes:
self.connection_address
self.connection_start_time
self.connection_status
I'd be highly likely to create a Connection sub-object because I'd feel very confident that I'd want it to sprout methods like:
self.connection.is_alive()
self.connection.drop()
self.connection.get_duration()
self.connection.stats.get_total_bytes()
self.connection.stats.get_mbits()
My article only talks about objects and attributes and doesn't say too much about methods. I think having lots of methods on an object is often fine. Numpy's ndarray object has around 15 attributes but over 50 methods. I don't have a problem with that, especially for such key object.
There's a big difference between attributes and methods. Adding an attribute to an object increases the complexity of every other method in the object, and every future method yet to be added. As the article says an attribute is just a global variable to the object, adding even a few globals can tip the scales to where the object is a confusing mess. But adding a method is fairly harmless, it doesn't really make the other methods more complex.
- thelazydogsback 6y ago>Chunking by textual grouping is great. Sometimes that is totally enough Agreed, but much prefer object-first vs. verb first: owner_info_get(...) Having 600 methods that start w/"get" and 450 that start with "set" doesn't help docs or autocomplete. I remember the Win32 API books were organized alphabetically and it would have been comical if it weren't so useless.