0

I need to remove the extra if and else loop as per the req and need to make it short for reading of the code should be easy. can anyone suggest some better for below:

if (data[0].FA_N__c) {
            if (test === false) {
                this.cl = acL;
            }
            else if (test === true) {
                this.cl = acWo;
            }
        }
        else {
            if (test === false) {
                this.cl = nAc;
            }
            else if (test === true) {
                this.cl = nAcWo;
            }
        }
    }
1
  • If we're talking about readability, I don't find those variable names very readable. Commented Jul 8, 2020 at 14:55

2 Answers 2

1

make use of Conditional (ternary) operator in JavaScript to make your code short:-

        if (data[0].FA_N__c) {
            this.cl = test ? acWo : acL;
        }else {
            this.cl = test ? nAcWo : nAc;
        }
0

A little bit more context of where the code is structured in will be fine :)

When you have the possibility to make a return, you can do something like this:

if (data[0].FA_N__c) {
  if (test === false) {
    this.cl = acL;
    return;
  }

  this.cl = acWo;
  return;
}

if (test === false) {
  this.cl = nAc;
  return;
}

this.cl = nAcWo;

When your are inside a loop, you can replace the return with a continue.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.