Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!