0

i have this kind of method:

function() {
  if (condition) {
    return true;
  }
  return false;
}

I thought of returning the condition itself , to make it more clean, and maybe more readable. like this:

function() {
  return (condition);
}

is this considered a good practice?

1
  • 2
    Yes (a former teacher of mine would yell at us when we used your first example). However, it won't always return a boolean (ie: 3 && 4 will return 4). If you absolutely need a boolean, use double negation: return !!(condition); Commented Aug 19, 2015 at 13:25

1 Answer 1

1

If there is no other code than just returning true/false then yes, return just the condition.

function() {
    return (condition);
}

Just as you said, it is more readable, less code. I wouldn't even call it good practice, it's more like the first code is a bad practice.

Sign up to request clarification or add additional context in comments.

Comments

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.