Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More meaningful name for invariant function #153

Open
cjoecker opened this issue Sep 30, 2022 · 12 comments
Open

More meaningful name for invariant function #153

cjoecker opened this issue Sep 30, 2022 · 12 comments

Comments

@cjoecker
Copy link

I would like to have a more meaningful name for the function than invariant. Something like throwOnFalsy is for me easier to understand.

While working with developers that don’t know what tiny-invariant is, I realized that is not easy understand at first sight what the invariant function does. To improve this, it would be nice to rename the function with something more descriptive. I would still keep the original function to avoid breaking changes but also allow to import the new function with the descriptive name.

What do you think? Is throwOnFalsy good for you or maybe you have a better name?
If you think it is a good idea, I would submit a PR for that.

@cjoecker cjoecker changed the title More meaningful name More meaningful name for invariant function Sep 30, 2022
@Trystanr
Copy link

I'm not sure the name is totally confusing but perhaps something like isNullish?

@alexreardon
Copy link
Owner

I agree entirely that invariant is not a great name. Personally, I think assert would be a way clearer name than invariant. However, leaning on invariant means that tooling that automatically allows invariant messages to be stripped (eg https://www.npmjs.com/package/babel-plugin-dev-expression) just works™️

Personally, I would like to see us move away from invariant towards assert. I am exploring making a new (ESM only) assert package: https://github.com/alexreardon/tiny-assert

@cjoecker
Copy link
Author

@alexreardon I like the assert!
I’m just wondering if it makes sense to make a new library for that.

tiny-invariant is already known in the community. Maybe it would b le better to leave the name for recognition purposes and just add a new function. Many brands preserve historical names just to allow the people to find them back.

@sandinmyjoints
Copy link

@cjoecker Would it help your issue if you did import assert from 'tiny-invariant';?

@cjoecker
Copy link
Author

cjoecker commented Nov 1, 2022

@sandinmyjoints yeah! That would be more intuitive and the name of the library remains the same 💯

@pkerschbaum
Copy link
Contributor

I guess this package tiny-invariant could just re-export the default function as a named export assert.
So that people could do:

import { assert } from 'tiny-invariant';

Autosuggestion and autoimport of IDEs (e.g. VS Code) would probably benefit from that.

@alexreardon
Copy link
Owner

My only concern is that adding some name exports makes this "tiny" package a bit larger than it needs to be 🤔

@cjoecker
Copy link
Author

cjoecker commented Nov 6, 2022

@alexreardon that’s an interesting point. Two questions comes to my min: how big is the impact of that extra export? How relevant is that increase in size having into account that npm does tree shake and only compiles the code that is really being used?

@pkerschbaum
Copy link
Contributor

pkerschbaum commented Nov 12, 2022

I made the code changes to export invariant as assert too here: master...pkerschbaum:tiny-invariant:feat/export-as-assert.
To determine the increase in size, I created the .tgz files which would get published to the npm registry (by running yarn pack). Both of the current master and of the feature branch.

The size of the master .tgz package is 4668 bytes, that of the feature branch is 4720 bytes. Thus the size of the NPM package would increase only by 52 bytes. I think this represents the "upper bound" of size increase consumers of the package would have - with tree-shaking and stuff like that it would probably be even less.

I would say the increase of the size is no problem :)

@cjoecker
Copy link
Author

I made the code changes to export invariant as assert too here: master...pkerschbaum:tiny-invariant:feat/export-as-assert. To determine the increase in size, I created the .tgz files which would get published to the npm registry (by running yarn pack). Both of the current master and of the feature branch.

The size of the master .tgz package is 4668 bytes, that of the feature branch is 4720 bytes. Thus the size of the NPM package would increase only by 52 bytes. I think this represents the "upper bound" of size increase consumers of the package would have - with tree-shaking and stuff like that it would probably be even less.

I would say the increase of the size is no problem :)

Looks nice! Thanks for the PR!

@TomerAberbach
Copy link

I agree entirely that invariant is not a great name. Personally, I think assert would be a way clearer name than invariant. However, leaning on invariant means that tooling that automatically allows invariant messages to be stripped (eg https://www.npmjs.com/package/babel-plugin-dev-expression) just works™️

Personally, I would like to see us move away from invariant towards assert. I am exploring making a new (ESM only) assert package: https://github.com/alexreardon/tiny-assert

I think if terser/terser#1080 were fixed, then the name could be anything and messages would still be stripped (at least when using Terser).

@FPierre
Copy link

FPierre commented May 9, 2023

Opposite opinion: I love invariant, over any others words. This makes me think of the DDD invariant (strong business rule that must be enforced). It make the intent very expressive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants