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

Instance or static methods for non-operator calculations? #37

Open
littledan opened this issue Jan 8, 2020 · 3 comments
Open

Instance or static methods for non-operator calculations? #37

littledan opened this issue Jan 8, 2020 · 3 comments

Comments

@littledan
Copy link
Member

littledan commented Jan 8, 2020

Fabrice Bellard's BigDecimal implementation in QuickJS (2020-01-05 release) uses static methods like BigDecimal.div rather than instance methods like BigDecimal.prototype.div, as the current README suggests. In personal communication, Fabrice said,

Regarding BigDecimal.prototype.div / BigDecimal.div, I tend to prefer
BigDecimal.div because both operands play the same role. It is a matter
of personal taste anyway.

BigDecimal.round is also a static method in QuickJS, and an instance method in the current README.

I could go either way on this ergonomics question. I thought instance methods would be easier to type, but I don't have strong feelings.

@ljharb
Copy link
Member

ljharb commented Jan 8, 2020

When the order of the operands doesn’t matter then static makes more sense to me; when they do, I’d personally lean towards instance methods.

@littledan
Copy link
Member Author

If we add methods for arithmetic operations to give rounding parameters, I think it'd be confusing to make add a static method and sub an instance method. I'd prefer to make a general decision for all arithmetic things, even if some end up being commutative and others not.

littledan pushed a commit that referenced this issue Jan 15, 2020
littledan pushed a commit that referenced this issue Jan 15, 2020
@Rudxain
Copy link
Contributor

Rudxain commented Apr 25, 2022

I think we should stick with the "principle of least surprise" and follow the current ES pattern. This means not all methods would be static or instance. But we should only choose this option if there's no consensus. If we can settle on a consensus that does things in a different way, we should try it, we shouldn't stick to the past just because "it has always been this way".

My personal opinion is that frequently used methods should be on the prototype, while "more special" methods are static, this reduces the need for minification in some cases, and saves time for the devs and users

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