1

My first thought was that I'm "serializing" the complex object, but from what I understand that means I'm reducing it down to a string or binary format which could be passed over a network. While what I am actually doing is converting a complex object to more primitive types like list, dict, str, int, float, etc. The context here is python, and I'm trying to rename my function to_dict(...) to something which uses a more idiomatic/industry accepted term.

Currently I'm doing this:

class SomeComplexClass: ... my_complex_obj = SomeComplexClass(...) simple_obj = to_dict(my_complex_obj) simple_obj['foo'] = 'bar' return to_json(simple_obj) 

As you see, I'll typically do some final adjustments to the object, then pass this to a library which knows how to convert this into a JSON string, which is passed over the network as HTTP response. This function is used 608 times in our code base.

Here to_dict(...) is doing some heavy lifting, by recursively walking through the complex object and handling corner cases etc., it even supports multiple types of complex objects, even lists of objects.

It is this last part which makes me question the name to_dict(...), because I at some time extended it to support lists as the top level object type. It feels a little silly to do list_of_simple_objects = to_dict(my_list_of_complex_objects), because it then returns a list of dicts.

5
  • 1
    Maybe something like to_primitives, or transform_to_primitives?CommentedDec 31, 2021 at 14:17
  • I think it makes a huge difference if the function in stake just operates on a specific type SomeComplexClass, or several types, which is not clear to me from this question. If it can convert almost any kind of object, a generic name like to_dict is fine. Otherwise, I would recommend to specify the source type (or category of types) in the name.
    – Doc Brown
    CommentedDec 31, 2021 at 19:17
  • Hey @DocBrown it does indeed work on multiple types. They do have the same base type, but they vary widely.CommentedJan 1, 2022 at 17:40
  • Well, then why not name it nameOfBaseType_to_dict? That should make it suffiently clear.
    – Doc Brown
    CommentedJan 2, 2022 at 11:31
  • @DocBrown That could work, maybe entity_to_dict(), or entity_to_primitives(). Thanks.CommentedJan 2, 2022 at 20:36

2 Answers 2

1

As blunova writes, "descriptive" is good, but depending on what perspective you have on your objects, the description might turn out very different!

For instance: if you perform this action only as a prerequisite to serializing them, 'preSerialize()' might be a good description. To emphasize that you're really doing the opposite of what a constructor does, 'deconstruct()' would be descriptive. If you're pragmatic about object-orientation and only define your own classes to get better type-checking and code completion, then 'untype()' expresses that you're turning a custom-tailored object with typed fields back into a generic glob of lists/maps/scalars. (Personally, I would probably go with Filip's comment of "to_primitives()".)

    1

    I always try to be as descriptive as possible when naming things so other can quickly understand what my code is doing, so I would not call the function just to_dict() because it seems to me not descriptive enough.

    When naming function I stick to the convention of having a verb in the name. So I would call your function something like convert_complex_to_simple_type().

    If I have to work/maintain your code, especially if I am not as experienced as you are, and see the to_dict() function I immediately understand that something is going to be converted to a dictionary but maybe I would appreciate to understand more about the nature of what is going to be converted just by looking at the name of the function.

    Within reasonable limits, I prefer a name a little bit longer but very descriptive, than one that is shorter but less clear.

    3
    • 1
      In general I agree, it's just it becomes a little tedious for often used functions. This function is used 608 times in our code base. All of our endpoints ultimately end in to_dict() in some way or other. But I kind of get where you're going at anyway. Maybe something like to_simple_types(), or to_primitive_types().CommentedDec 31, 2021 at 14:15
    • 1
      Maybe I would edit the question and add the fact the is used so many times, it might be a helpful information.
      – blunova
      CommentedDec 31, 2021 at 14:18
    • 1
      @AndréChristofferAndersen I am sure you already know that, but maybe it could be useful to the community or to those that just get started in software development field. I always think about the book Clean Code: A Handbook of Agile Software Craftsmanship where the author, in Chapter 2, says Choosing names that reveal intent can make it much easier to understand and change code.
      – blunova
      CommentedDec 31, 2021 at 15:03

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.