dm-script

Why does TagGroupAddLabeledTagGroup return src_tag_group?


I'm trying to understand the design rationale behind the following dm-script function:

TagGroup TagGroupAddLabeledTagGroup(TagGroup tgt_tag_group, String label, TagGroup src_tag_group)

The function's return value is the same src_tag_group that I pass in. If I already have access to src_tag_group for later use, why does the function return it again instead of, for example, having a void return type?

Questions:

  1. What is the purpose of returning src_tag_group in this function design?
  2. Is there an intended use case where this return value simplifies chaining or improves code readability?
  3. Are there other examples in dm-script where a similar design pattern is used, and what benefits do they provide?

Any insights into this design decision or links to further documentation would be greatly appreciated.


Solution

  • I can also only speculate, as a lot of the scripting language syntax and curiosities go back 20years or more. Clean-ups have been rare in this time, mainly because "backward compatibility" of old scripts was deemed a lot more important than having a super clean and logical naming and syntax convention.

    However, while redundant, returning the (just added) source tagGroup allows for some convenient "pipe-lining" as well, which might be part of why it is like that. ( I do that in a lot of my own classes, rather than returning a void. )

    Example #1 - Pipe-line convenience

    TagGroup TGparent = NewTagGroup() // Whatever parent. Might be a "ImageGetOrCreateTagGroup()
    TGparent.TagGroupAddLabeledTagGroup("Group", NewTagGroup()).TagGroupSetTagAsString("Data","mine")
    TGparent.TagGroupOpenBrowserWindow(0);
    

    Example #2 - Adding & Variable assignment in one line

    TagGroup TGparent = NewTagGroup();
    TagGroup TGChild = TGparent.TagGroupAddLabeledTagGroup("Group 1", NewTagGroup())
    TGChild.TagGroupSetTagAsString("Data","mine")
    TGparent.TagGroupOpenBrowserWindow(0);