Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-88709

Get rid of "if obj.hasElement(field) then obj[field]" antipattern

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Storage Execution
    • ALL
    • Execution Team 2024-05-27

      This is an antipattern because it makes us go through the BSON object twice:

      if (obj.hasElement(elemName)) {
         elem = obj["elemName"]
         doStuff(elem);
      }
      

      Instead it should be:

      elem = obj["elemName"]
      if (!elem.eoo()) {
         doStuff(elem);
      }
      

      We do this in several places in our codebase, such as here, here, here, etc.

      While some of the examples I listed are not on the hot path, it would help if we got rid of this idiom altogether so that it stops spreading.

      See comments for more info.

            Assignee:
            damian.wasilewicz@mongodb.com Damian Wasilewicz
            Reporter:
            vishnu.kaushik@mongodb.com Vishnu Kaushik
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: