3 ms·
(start_ns, end_ns) -> Span (process_id, thread_id) -> Origin Nope x10. Before that transformation I got what the structure does instantly, but after that I h
by wruza 6y ago
(start_ns, end_ns) -> Span
(process_id, thread_id) -> Origin
Nope x10. Before that transformation I got what the structure does instantly, but after that I have no idea what span and origin exactly are. You cannot make things simpler by adding nomenclature. Remember that "show me your code" vs "show me your data" thing? This hides your data, and now readers have to go to definition and remember +2 references to get the whole picture. It may look nice on a type diagram, where that's obvious, but not in code.
But that shouldn't defeat the article's meaning. I think that the same rule can be applied to groups of related attributes instead. When you have 5-7 groups per structure, it is okay, but when these groups exceed that limit, that's a signal for rethinking your god object.
Group things:
self.name = name
self.category = category
self.args = kwargs
self.phase = phase
self.start_ns = start_ns
self.end_ns = end_ns
self.process_id = process_id
self.thread_id = thread_id
And they'll be much easier to grasp. Your brain will do that span/origin grouping in background, in terms that it definitely knows, but for which it may not even have a vocal association. It may not be able to remember 10 different things, but it will remember that thread_id is coupled with a process_id, because you already have that knowledge in a long-term memory.
- pbw 6y agoI 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.