-1

I'm a developer that has the entire background based on languages with OO/FP approaches and now I'm working with NodeJS.

For me, it's something new and, sometimes, kinda confusing. Right now, I need some help to organize my code instead of keep using IF/ELSE in a big function.

I've wrote an API that is used by many departments of my company. Most of departments has a common behavior on code but two departments must call another APIs to build their objects instead of use a common one shared by others.

If I was writing same code using Java, I could use some polymorphism to satisfy customizations in code but I have no clue on what's the best approach to solve it on Javascript.

Here is what running on production:

  function openTickets (event, config) {
    // more code
    let user 
    try {
      if(config.department === 'HR'){ // TODO move 
        const usersResponse = (await company.getUser(phone, customData.zipCode)).body  // this is a http call using got    
        if(usersResponse.results.length > 0){
          user = usersResponse.results[0]
        }         
      } else {
        user = {
          //....
        }
      }
    } catch (err) {
      console.log('error getting user', err)
    }
    //more code
  }

This is just an example on how I'm solving customizations on code and creating a Frankenstein that will be pretty hard to maintain in a few weeks.

What is the best approach to customize specific behaviours on Javascript instead use IF/ELSE?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.