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

147
Visualizações
Minimize use of if...else constructs to get the right value for an object property with some design pattern if possible

I want to assign a value to property when creating an object based on lot of conditions. Currently I'm using a separate function to get the value of the property, something like this:

function getLocationId(currency, storeCode, isBundle) {
  if (currency === 'MYR') {
    if (storeCode === 'store-1') {
      return 1;
    } else if (storeCode === 'store-2') {
      return 2;
    }
  } else if (currency === 'SGD') {
    if (storeCode === 'store-1') {
      return 3;
    } else if (storeCode === 'store-2') {
      // This function can return the same value for a different condition
      return 2;
    } else if (!storeCode && !isBundle) {
      return 8;
    }
  }
  .
  .
  .
  // More conditions, with some involving `isBundle`
}

function getAccountId(currency, storeCode, paymentMethod) {
  // Function definition similar to getLocationId
  // with checks for currency, storeCode, paymentMethod
}

function getRequestObject(event) {
  return {
    .
    .
    .
    location: getLocationId(event.currency, event.storeCode, event.item.isBundle),
    account: getAccountId(event.currency, event.storeCode, event.paymentMethod),
    .
    .
    .
  };
}

I feel like there are a lot of if...else constructs used with nested checks again. Is doing that even a good idea? Is there any design pattern I can use to build a request object with the right location ID and account ID based on the parameter event in getRequestObject?

PS: I'm not returning the integer value in the actual codebase, instead using this library called node-config and storing these IDs in a JSON file which I can later retrieve with config.get('propertyName').

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

0

A slightly cleaner approach without else statements after return statements.

function getLocationId(currency, storeCode, isBundle) {
    if (currency === 'MYR') {
        if (storeCode === 'store-1') return 1;
        if (storeCode === 'store-2') return 2;
        // other if or return a default value for this currency
    }
    if (currency === 'SGD') {
        if (storeCode === 'store-1') return 3;
        if (storeCode === 'store-2') return 2;
        if (!storeCode && !isBundle) return 8;
    }
}
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