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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

The meaning of side effects 馃 #2

Open
skyrpex opened this issue Jul 4, 2023 · 2 comments
Open

The meaning of side effects 馃 #2

skyrpex opened this issue Jul 4, 2023 · 2 comments

Comments

@skyrpex
Copy link

skyrpex commented Jul 4, 2023

I think the current definition of side effects is incorrect:

A function is considered to have side effects if it performs any action other than returning a value. Side effects include, but are not limited to, modifying variables or objects, reading or writing to files, making network requests, and interacting with the DOM.

I believe the above description suits pure more than side effects. A function can have no side effects even if it reads external data such as files or variables from outer scopes. On the other hand, it would cause side effects if the function wrote to a file or modified variable from outer scopes (note that modifying variables created in its own scope still doesn't count as having side effects).

Although, I understand that even reading a JS variable may cause a side effect in certain situations (for example, when using a proxy). Network requests, even GET requests, may cause side effects too (eg, a page view counter that increases for each visit). Finally, reading files may cause the OS to update some file descriptors or whatever... But still, is it safe to assume that most of the time, those operations shouldn't count as side effects?

@antfu
Copy link
Member

antfu commented Jul 5, 2023

I guess maybe we could remove that section, as explaining side-effect is probably not in the scope if the spec? Otherwise, feel free to send PRs to rewrite them into what make send to you

@fabiosantoscode
Copy link

AFAIK, to Terser and uglifyjs, the pure annotation also asserts that the function will not be throwing an error. When minifying, you shouldn't remove code that throws.

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

3 participants