• (nodebb)

    But what you can see here is some vestige of "who owns the memory". getUniqueArrays could easily create its own Set, return it, and be done. But no, it needs to accept a Set as its input, to manipulate it.

    It might also be a vestige of dependency injection management. If the function makes the container object itself, it is then dependent on how they are created, while if it takes one as input, it doesn't need to know that stuff, just how to fill the container. (And no, passing it the container factory isn't exactly the right way to work around that.)

    Perhaps it doesn't matter, but that's an alternative explanation.

  • Sole Purpose Of Visit (unregistered)

    In what way is this wrong?

    Apart from anything else, it follows the FP paradigm of mapping an input collection to an output collection, which I tend to do just as a reminder to myself.

  • 516052 (unregistered)

    It depends on the context. This sort of implementation decouples the method from being dependent on how the collection is created and passes that responsibility onto the caller. And that can be a good or a bad thing depending on the circumstances.

    In the context of a simple array it does not matter over much either way. But say this was a more complex structure or one that required initialization of some other sort. You could make a good argument that you shouldn't split a constructor into N different method calls that all have to happen in order every time an object is created.

    And even with arrays, this sort of function opens you up to a lot of potential pitfalls. Say for example you pass a non empty array into it by accident.

    Overall the more I think about it the less I like it.

  • (nodebb)

    I think it's generally an anti pattern to pass in an empty collection for a method to fill it up. It breaks the client- server paradigm in that instead of saying "give me X", the caller says "here's a box, put X into it". And yes it's entirely possible they follow that pattern everywhere - we have to take care their word for it - and it is a WTF.

    Having said that, I'm struggling to understand how that is a good memory management practice in C. If caller A requests B to create a new array, or created the array itself and requests B to fill it up, and said array ends up with C later which neglects to free it, you still have a memory leak. It sounds to me like they came up with a methodology of leak prevention which is kind of leaky.

  • LCrawford (unregistered)

    | Say for example you pass a non empty array into it by accident.

    The function might as well be called appendUniqueArrays()

  • (nodebb) in reply to Sole Purpose Of Visit

    On the other hand, it breaks the FP paradigm of functions not mutating their inputs, so...

  • R Samuel Klatchko (unregistered)

    An actual C programmer would say, "um, that's not C".

    But seriously, what language is that? It looks closer to C++ but range based for loops don't use "in". It's possibly C# but their collection based loop should use "foreach" instead of "for".

  • my name is missing (unregistered)

    The post states it's Java, which it is.

  • richarson (unregistered) in reply to R Samuel Klatchko

    "Unfortunately, many years ago, a lot of their software started being developed in Java."

  • 516052 (unregistered) in reply to R Samuel Klatchko

    C# uses IN but you have to give a type for the iteration variable even if only var. This almost looks like C#/LINQ code.

  • TheJayMann (github) in reply to R Samuel Klatchko

    The article mentions that the software was being developed in Java. Thus, that would rule out languages such as C++ or C#, and would suggest that it is Java. However, Java's collection iterator construct does not use the in keyword; rather, it uses the : symbol. However, there are other JVM languages which do use this construct, such as Kotlin and Groovy, and, based on the surrounding code, Groovy appears to be the best match.

  • Naomi (unregistered)

    I think it's Java with some syntax errors introduced in obfuscation (either by Remy or the submitter). The article mentions Java, and LinkedHashSet is the Java class for a Set that stores elements in insertion order.

  • NotAThingThatHappens (unregistered)
    Comment held for moderation.
  • Harris (unregistered)

    I can see this being useful if you want different collection implementations depending for performance optimization or for the different behaviours.

    E.g. HashSet tends to be much faster than TreeSet, but doesn't have any Ordering guarantees.

  • PrimeNine (unregistered) in reply to Harris

    in C++ it's a relatively common convention we use for performance - templating methods on the input container means the caller can make the choice of (for example) using stack rather than heap memory if the use case is appropriate. Controlling how memory is allocated/used is frequently the single most important factor (and area we can get the biggest wins by improving) in the performance of software in my field (Games Dev)

  • Loren Pechtel (unregistered) in reply to PrimeNine
    Comment held for moderation.
  • Ollie Jones (unregistered)

    The C problem is that every malloc() must be matched by exactly one, not zero, not two, free() calls. Otherwise, "Bus error, core dumped" or whatever results from heap corruption, or heap exhaustion. If function a returns a data structure (a pointer to a data structure) to function b, how does that data structure get its free() called?

    Seriously.

    To this day.

    In software that's a happy hunting ground for code-execution exploits, that we read about in Krebs and CVE reports.

  • Chris (unregistered)

    The thing that bothers me is returning the set, which was passed to it as a parameter. The set hasn't been altered (i.e. it is not now referencing a new set), so you're just giving the caller that which they already have. The only time I can think of where that may be useful is when you may want to chain calls on one line. E.g. factory methods, StringBuilder (which I guess is a factory class for strings).

  • Bekas (unregistered)
    Comment held for moderation.
  • FTB (unregistered)
    Comment held for moderation.
  • Chatogaster (unregistered)
    Comment held for moderation.
  • (nodebb) in reply to Ollie Jones
    Comment held for moderation.
  • Charles (unregistered)

    The C problem is that the programmer needs to be diligent and competent. When calling a function you need to know what it does and what requirements that imposes on the caller. And you then need to ensure those requirements are met. Whether other languages allow you to be careless and incompetent can be discovered elsewhere on this site.

    And another reason to pass in a container is so that the same container can be re-used rather than creating and destroying a new one for every function invocation. Sometimes this makes a very big difference.

  • Asinine (unregistered)
    Comment held for moderation.

Leave a comment on “A Pointer to your References”

Log In or post as a guest

Replying to comment #:

« Return to Article