Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
How to improve function that toggles element in map?

There is a function:

toggleSelect(key: string, object: RegistryLayerItemGeneric, selected: boolean) {
    if (selected) {
        let objects = this.state.selectedRegistryObjects.get(key);

        if (objects && object.ObjectId in objects) {
            delete objects[object.ObjectId];
        }

        this.state.selectedRegistryObjects.set(key, {
            ...objects,
        });

        return;
    }

    const objects = {
        ...(this.state.selectedRegistryObjects.get(key) || {}),
        ...{ [object.ObjectId]: object },
    };

    this.state.selectedRegistryObjects.set(key, objects);
}

This function deletes element in map and adds if not exist. How can I improve it? I think this functon complecated for understanding.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There are a few things I would do here that should help.

  1. Split the function into multiple functions so that each function only does one task. I have begun doing this below

  2. Rename the parameters to be more descriptive of what they contain, i.e. instead of "selected: bool", say what selected is "mapElementExists: bool".

  3. Rename the function to be more descriptive, so that the reader knows what is toggled here

I have begun doing this below

toggleMapElements(key: string, object: RegistryLayerItemGeneric, mapElementExists: boolean) {
    if (mapElementExists) {
        removeMapElement(key: string, object: RegistryLayerItemGeneric);
    } else {
        addMapElement(key: string, object: RegistryLayerItemGeneric)
    }
}

removeMapElement(key: string, object: RegistryLayerItemGeneric) {
    let objects = this.state.selectedRegistryObjects.get(key);

    if (objects && object.ObjectId in objects) {
        delete objects[object.ObjectId];
    }

    this.state.selectedRegistryObjects.set(key, {
        ...objects,
    });

    return;
}

addMapElement(key: string, object: RegistryLayerItemGeneric) {
    const objects = {
        ...(this.state.selectedRegistryObjects.get(key) || {}),
        ...{ [object.ObjectId]: object },
    };

    this.state.selectedRegistryObjects.set(key, objects);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

That is definitely a complicated function.

Here's a simpler version

toggleSelect(key: string, object: RegistryLayerItemGeneric, selected: boolean) {
  const objects = (this.state.selectedRegistryObjects.get(key) || {});

  if (selected) {
    delete objects[object.ObjectId];  
  } else {
    objects[object.ObjectId] = object;
  }      

  this.state.selectedRegistryObjects.set(key, {...objects});
}

I don't have a typescript environment in front of me, so can't confirm whether it passes all the typescript noise, but this will work from a javascript perspective.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda