Hacker News new | ask | show | jobs
by Someone 480 days ago
FTA: The code can be reduced to simply:

  public void someFunction(SomeType relatedObject,
                           List<SomeOtherType> unrelatedObjects) {
    ...
    treeMap.put(relatedObject.a(), relatedObject.b());
    ...
    // unrelatedObjects is used later on in the function so the
    // parameter cannot be removed
  }
That’s not true. The original code only does the treeMap.put if unrelatedObjects is nonempty. That may or may not be a bug.

You also would have to check that a and b return the same value every time, and that treeMap behaves like a map. If, for example, it logs updates, you’d have to check that changing that to log only once is acceptable.

1 comments

Good point. It should be replaced with an if not empty check.
An empty check wouldn't be thread-safe either.
Hmm I don’t think we know that from the post. The list of unrelated objects could be immutable or mutable and never modified. An isEmpty check is 100% safe to be called concurrently in both those situations