Coder Social home page Coder Social logo

Comments (4)

osidenate avatar osidenate commented on August 19, 2024

👍
What does this project do? I see it referenced in a lot of high profile projects, but it's description just says "A mirror of Facebook's invariant (e.g. React, flux)."

Since the links to React and Flux's invariant are broken, I can't tell what's going on here without looking at the source. Even just a simple one paragraph description would be nice.

from invariant.

gajus avatar gajus commented on August 19, 2024

From the source code:

/**
 * Use invariant() to assert state which your program assumes to be true.
 *
 * Provide sprintf-style format (only %s is supported) and arguments
 * to provide information about what broke and what you were
 * expecting.
 *
 * The invariant message will be stripped in production, but the invariant
 * will remain to ensure logic does not differ in production.
 */

Basically,

invariant(somethingThatMustBeTrue, 'Something that I thought is true is not true.');

Will throw an Error with a message "Something that I thought is true is not true." when somethingThatMustBeTrue is not truth-y.

from invariant.

gajus avatar gajus commented on August 19, 2024

Knowing that I would still like the explanation for how is this better than a simple condition, e.g.

if (somethingThatMustBeTrue) {
    throw new Error('Something that I thought is true is not true.');
}

from invariant.

zertosh avatar zertosh commented on August 19, 2024

@gajus, you can very well use if -> throw, nothing wrong with that. I think it comes down to preference.

The way you'd write an invariant call is very similar to a test's assertion function:

// in a test:
assert.ok(someCondition, 'something bad happened!');
// in user code:
invariant(someCondition, 'something bad happened!');
// using an if:
if (!someCondition) {
  throw new Error('something bad happened!');
}

Some people like that. It's also worth noting that if and throw are statements, while an invariant call is an expression. So:

// this is invalid syntax
var mustBeTrue = true ? 'true string' : throw new Error('not true!');
// while this is valid syntax
var mustBeTrue = true ? 'true string' : invariant(false, 'not true!');

Some people find it convenient to throw errors from expressions. One last thing, invariant also provides printf-like string replacement:

// using an if:
if (!someCondition) {
  throw new Error(
   '[' + moduleName + '] got "' +
   someCondition + '" but expected something truthy'
  );
}
// using invariant:
invariant(
  someCondition,
  '[%s] got "%s" but expected something truthy',
  moduleName, someCondition);

from invariant.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.