1

How do I make this if else more simple.

if (!request) {
  return 'no request';
} else {
  if (loading === '404') {
    return 'rejected';
  }
  if (loading === '200' && !array1.length) {
    return 'fulfilled';
  }
}

if any ternary operator then how can I make it

5
  • 1
    What's wrong with the current code? It's clear as is, you don't need to minify your developement code. Commented Jan 7, 2022 at 16:05
  • I don't think it can be much simpler than this. Each condition is different, and each return statement is different Commented Jan 7, 2022 at 16:05
  • not all paths lead to a returned value so not sure how a ternary would work. Commented Jan 7, 2022 at 16:06
  • Cannot make it simpler, but you miss a return in the else condition. What if all the condition is not fullfilled? I think you should return something if nothing fullfilled. Commented Jan 7, 2022 at 16:06
  • 1
    The code is working I guess, so the better site to ask help would be codereview.stackexchange.com. Other than that if you have return in if you don't need else this convention would flatten your code. Commented Jan 7, 2022 at 16:08

5 Answers 5

3

You could omit else part, because you return if true.

if (!request) return 'no request';
if (loading === '404') return 'rejected';
if (loading === '200' && !array1.length) return 'fulfilled';
Sign up to request clarification or add additional context in comments.

Comments

0

You can also simply do this with the ternary operator. Hope the below code will you.

!request ? 'no request' : loading === '404' ? 'rejected' : 'fullfilled'

2 Comments

Nested ternaries are almost always bad practice though.
@NullDev you should explain why do you believe smth is bad practice.
0

You can use if-elseif-else statement

if(!request) return 'no request'
else if(loading === '404') return 'rejected'
else if(loading === '200' && !array.length) return 'fulfilled'

1 Comment

There is no need to use else if if you have return. If you remove redundant code the answer would be identical to the very first one.
0

one might consider ...

{
  // ...
  return ((!request && 'no request')
    || (loading === '404' && 'rejected')
    || (loading === '200' && !array1.length && 'fulfilled')
    || undefined // or 'failure' // or 'not fulfilled'
  );
}

... or ...

{
  // ...
  let returnValue = (!request && 'no request')
    || (loading === '404' && 'rejected')
    || (loading === '200' && !array1.length && 'fulfilled')
    || undefined; // or 'failure'; // or 'not fulfilled';
  
  // do more stuff ...
  // ...
  // ... maybe even change `returnValue` again.
  
  return returnValue;      
}

2 Comments

I think that this style is very useful in more standard code bases: Like if you have 5+ of loading, and they all return a string, then this code is perfect. If those three if statements are all there is however, then this code is extremely difficult to dig through. Just an opinion.
btw, no need to wrap logical AND expressions with parentheses. the main difference to the question is the place. if statements could be at any place, but the return statement unconditionally ends the function.
-1
if (request) {
  switch (loading) {
    case "404":
      return "rejected";
    case "200":
      if (!array1.length) return "filfilled";
      break;
  }
} else {
  return "no request";
}

I think a switch statement is better here considering that you may get more cases.

1 Comment

Please no, for the sake of cyclomatic complexity :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.