1

I wrote this simple function once, to display notifications on hash change :

function watchHash() {
    if(location.hash == '#thanks') {
        displayNotification('Thanks for your feedback, I\'ll try to get back to you as soon as possible.'); // Notify on form submit success
    }

    if(location.hash == '#error') {
        displayNotification('Oops, something went wrong ! Please try again.'); // Notify on form submit error
    }
}
window.onhashchange = watchHash;

I came back to it today and I thought, is it correct if I write it this way instead?

function watchHash() {
    if(location.hash == '#thanks') {
        displayNotification('Thanks for your feedback, I\'ll try to get back to you as soon as possible.'); // Notify on form submit success
    }
    else if(location.hash == '#error') {
        displayNotification('Oops, something went wrong ! Please try again.'); // Notify on form submit error
    }
    else {
        return;
    }
}
window.onhashchange = watchHash;

If so, is it relevant?

I'm a bit confused here, I'd like to stick to best practices.

Thanks for your help.

1
  • On a side note the hashchange event isn't supported (properly) in all browsers, so you should shim that (if you haven't already) Commented Sep 27, 2012 at 18:23

5 Answers 5

4

The second scenario is much better. Why?

Because in the first scenario if first condition is met or not met - it doesn't matter, the second case is checked too, the third, the fourth and so on.

In second case If the first case fails, then the second one is tested, if it fails, the third is tested, so your software does not spend useless time checking scenario which will not happen.

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

Comments

2

In your case, either method works just as well. However, there are some times when using the else clause is truly the best way to handle over-lapping logic.

Untested p-code, just for an example

if (isRaining) && (iHaveUmbrella) {
  iGetWet = false;
} else if (isRaining) {
  iGetWet = true;
} else {
  iGetWet = false;
}

In this case, the else if means that if the first condition is true, the second condition is never checked.

Comments

1

it might not be relevant for this usecase, but the 1st way is slower because the interpreter has to check conditions more often.. consider a game loop which is called 60 times per second and you don't structure your if else blocks you will get a huge performance hit.

Comments

1

Either way works properly. Differences between two are readability and pattern of coding.

Regarding readability, I personally prefer the first way to work on a single input. Second one works in better performance, but you might lost in multiple boundaries if you're not proficient developer.

Regarding pattern of coding, we can see that the first one won't return anything. Second one will return null ONLY IF hash tag is neither your selections. You can't use this function to check the condition because it won't return when hash tag is either '#thanks' or '#error'.

Comments

0

Your second version is "correct" insomuch as its functionality is identical to the first version, but the added code is entirely redundant.

1 Comment

The functionality is the same, but the second version seems a bit more performant. I'm not necessarily looking for optimal performance, so would you say that the first version is more relevant in this case?

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.