2 ms·
It depends on how you organize your utility classes. For example, if your project has a lot of serializers, then you might have a package that contains them. Or
by javajosh 14y ago
It depends on how you organize your utility classes. For example, if your project has a lot of serializers, then you might have a package that contains them. Or you might create a bunch of static methods on a single "SerializerUtils" class.
The other thing is that I prefer symmetry. I would probably write the function to take and return the same thing.
Finally, I might strongly consider writing a generic method that, using reflection, traverses arbitrary data structures and constructs an XML DOM. The caller can do what it needs, e.g. serialize into a string.
If, for some reason, your system requires a specific transformation from a custom data structure into a string, then it belongs in the custom data structure class - possibly a toString method, or something more specific.
RouterMessageHeader.toXMLString();
or
XMLSerializer.stringify(routerMessageHeader);
- jacquesm 14y agoSo, we are in agreement I think. The example I picked wasn't from java but C (but that's almost irrelevant), so your: RouterMessageHeader.toXMLString(); Ended up as router_message_header_to_xml_string(); So 35 characters in all. Not quite terrible but also not very elegant. At least it says what it does :) I still can't help the feeling that the data structure leaked into the naming of the function and I see no way to solve that, I can't even imagine how one would go about that and still have a function with global scope (in itself something that you'd try to avoid wherever you could, but in this case we can't for various good reasons). I've looked at making this a generic system but that increases the overall complexity of the bookkeeping (creation, destruction, modification) around the objects to such an extent that it probably isn't worth it (though I can see some other advantages of this approach, such as easier debugging).
- javajosh 14y agoYou probably won't see RouterMessageHeader.toXMLString() in the calling code though. You'd see: RouterMessageHeader foo = new RouterMessageHeader(); foo.toXMLString(); There are other ways for foo to be derived, of course, and in many of them you never need to reference the ReferenceMessageHeader class name.
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- jerf 14y agoIt would probably be instructive to consider what it would mean if you extend this out further. If we have a function that also ships it out on a socket, are we going to get send_router_message_header_as_xml_string(...)? If it also does error handling of some sort, are we going to embed that in the name? My point is still ultimately the same as others here, but I'm trying to demonstrate it from the other direction. It isn't that the long function name is OK, it is that it is poorly decomposed into tasks. But digging another "why" back, the root cause for that is what you say later, that it "increases the overall complexity of the bookkeeping" etc., and the reason for that is that C isn't really that good a language for abstraction. After 40 years of experience with it we've managed to create some best practices that carry it far further than anyone could have imagined at the beginning, but when it gets down to it, when you have a language that doesn't abstract very well, you do eventually end up paying the price in functions like that. It probably is the right answer in C, no sarcasm, but that's not the same thing as saying it's the right answer in a more general sense. (And to be more concrete, C has poor facilities for composing abstractions together without complication and loss. On a feature-by-feature basis you can make it do pretty much anything you want, Turing Complete etc etc, which makes it hard to see in blog-post-sized examples, but they don't wire together very well.)
- jacquesm 14y agoWhat surprises me more is that given that it is 40 years old how well it holds up. Sure there are other languages that deal with namespaces better. But none of them is without warts and the trade-offs involved make C in spite of its age a pretty good contender for systems level stuff.
- jerf 14y agoTo be clear, I agree entirely. It is a credit that it has lasted this long. But I do think that we are well into a time where C should no longer be your default answer, but rather something you get backed into because there is no other valid choice. (Which still certainly happens.)