0

I have a unusual nested if statement where a third condition is needed to execute some code, but this condition is independent of my else clause. Thus, I've nested this condition inside the outer 2 conditions. The below works as it should, but I'm not positive this is the best way to handle a case like this. Should I use another method or refactor this in a way I'm not thinking of?

if ($('#preferred-contact').prop('selectedIndex') == index && !$(selector).val())
{
    if (!$(label).parent().hasClass('has-error')) {
        $(selector).after("<span class='help-block form-error'>Required Field</span>");
        $(label).parent().addClass('has-error');
    }
}
else {
    $(selector + ' + .help-block').remove();
    $(label).parent().removeClass('has-error');
}
3
  • 2
    Nope, this is basically how you do it. Commented Nov 15, 2017 at 15:43
  • 1
    This is not a question for Stack Overflow. It's best handled at codereview.stackexchange.com Commented Nov 15, 2017 at 15:43
  • gee thanks for all the help @ScottMarcus you're one of the good ones Commented Dec 4, 2017 at 15:56

2 Answers 2

2

Your First if statement has a second if statement but, not other code.

Why just create a single condition for that ?

let myCondition = index === $('#preferred-contact').prop('selectedIndex') && !$(selector).val();

if (myCondition && !$(label).parent().hasClass('has-error'))
{
    $(selector).after("<span class='help-block form-error'>Required Field</span>");
    $(label).parent().addClass('has-error');
}
else if (!myCondition)
{
    $(selector + ' + .help-block').remove();
    $(label).parent().removeClass('has-error');
}

With that you reduce the cyclomatic complexity and improve your code. In every language you should coding with the least level of nesting possible.

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

3 Comments

Actually this does not do the same as the original code. Imagine use case where first condition is fulfilled and inner condition not
Gg ^_^ you're right! My previous code not respected the original post. Thanks you :) I edit my post for respect the sub-condition.
Please don't provide answers to questions that don't belong on Stack Overflow in the first place. It just encourages more of the same, in terms of questions and dilutes the quality of the questions.
-1

The way you are using conditionals if fine enaugh. However if you insist on getting rid of inner condition, you can rewrite it from backwards and use else if:

if ($('#preferred-contact').prop('selectedIndex') != index || $(selector).val())
{
    $(selector + ' + .help-block').remove();
    $(label).parent().removeClass('has-error');
}
else if (!$(label).parent().hasClass('has-error')) {
    $(selector).after("<span class='help-block form-error'>Required Field</span>");
    $(label).parent().addClass('has-error');
}

6 Comments

Why downwote? Am I missing something?
Please don't provide answers to questions that don't belong on Stack Overflow in the first place. It just encourages more of the same, in terms of questions and dilutes the quality of the questions.
@ScottMarcus if somebody asks for help and anybody can answer, then it was the right place to ask. That is what stackoverflow is about.
Well, no, not really. Stack Overflow has guidelines for the types of questions that should be asked and that's why we have the "Close" option available to members with enough rep. This question is "off-topic" and should not be posted to SO.
@ScottMarcus sure there are guidelines. However imho SO is more about helping people than categorizing content.
|

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.