Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unless vs if in Javascript with use of exclamation point?

The code below attempts to print out "is even" for numbers divisible by 2.

Should it not be if (test) then() rather than: if (!test) then(), when condition tested is "n % 2". The code below seems to read "IF numbers are NOT divisible by 2, print out 'number is even' ", which does not seem logical.

More generally speaking, what are the advantages of writing an Unless function over using an If statement in specifying condition, when what we could do is simply write if (!condition)?

Any help is much appreciated.

function unless(test, then) {
  if (!test) then();
}
function repeat(times, body) {
  for (var i = 0; i < times; i++) body(i);
}

repeat(5, function(n) {
  unless(n % 2, function() {
    console.log(n, "is even");
});
// → 0 is even
// → 2 is even
// → 4 is even
like image 760
Henry Avatar asked Jan 23 '15 23:01

Henry


People also ask

What is exclamation mark used for in JavaScript?

In Javascript, the exclamation mark (“!”) symbol, called a “bang,” is the logical “not” operator. Placed in front of a boolean value it will reverse the value, returning the opposite.

What does an exclamation point in an if statement mean?

By putting a single exclamation mark before a statement, you reverse the boolean. For example, ! true would equal false and ! false will equal true .

Does JavaScript have Unless?

JS still does not and likely never will have unless . "One thing you can do to make code more readable is wrap the negation in a function." - that's ...

How do you avoid an if statement in JavaScript?

Simple example code of if-else options. By putting these ideas into a conditional block we can remove the if-else statements. The logical AND ( && ) operator (logical conjunction) for a set of boolean operands will be true if and only if all the operands are true .


2 Answers

The arguable advantage of this is that the code reads slightly more like English: "Unless n modulo 2 is nonzero, log to the console that n is even."

In my experience, the practical consequence of this is that most programmers will have to go double-check what unless() actually does before they feel comfortable with it. Because it's not a standard piece of Javascript, they have no way of knowing whether it uses ! or ==true or ===0 or some other ever-so-slightly different test, unless they go look at the implementation.

My favorite example of this principle is COBOL. That language tried very hard to resemble English, so that even non-programmers could use it...but in reality both programmers and non-programmers seem to hate working with it.

like image 148
Ixrec Avatar answered Sep 20 '22 20:09

Ixrec


There are no advantages if the condition is provided inline as in this scenario. You can easily apply a transformation to negate it as appropriate in all cases (e.g. adding a ! in front), and taking unless out of the picture means there's one less thing the reader has to know about.

There could be an advantage if the condition was provided in the form of a callback, e.g.:

function test() { return true; }
function unless(test, then) { if (!test()) then(); }

unless(test, function() { console.log("test failed"); });

In this situation you could not directly pass the negation of test to a hypothetical function onlyIf that is complementary to unless, so having both onlyIf and unless can make the code more readable because it allows you to do this:

onlyIf(test, function() { console.log("test passed"); });

instead of this:

onlyIf(function() { return !test(); }, function() { console.log("test passed"); });

The above situation could be even worse if the callback is given arguments that need to be propagated into test.

like image 42
Jon Avatar answered Sep 18 '22 20:09

Jon