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

Rethink utils #2690

Closed
levino opened this issue Apr 16, 2019 · 3 comments
Closed

Rethink utils #2690

levino opened this issue Apr 16, 2019 · 3 comments
Labels
2.x 2.0 related issues Enhancement Includes improvements or optimizations Stale Has not received enough activity

Comments

@levino
Copy link
Contributor

levino commented Apr 16, 2019

Currently the instance of web3 has a member variable utils which provides static util methods. So a lot of people who only need a static utils method like randomHex do this:

import Web3 from 'web3'
const web3 = new Web3()
web3.utils.randomHex('...')

Which is totally wrong an awkward. There is no reason to attach the utils to the instance of web3. Right now one can use the package web3-utils for an easier approach:

import * as web3Utils from 'web3-utils'
web3Utils.randomHex('...')

This is nicer but also a little annoying to do. I recommend to export the Utils as a variable at the global export object. Then one can do this:

import Web3 from 'web3'
Web3.utils.randomHex('...')

To emphasize: Any method or variable that does not need instance state should always be static and never a member variable of a classes instance.

@nivida
Copy link
Contributor

nivida commented Apr 16, 2019

I totally agree with you 👌

@nivida nivida added the Enhancement Includes improvements or optimizations label Apr 16, 2019
@nivida
Copy link
Contributor

nivida commented Apr 16, 2019

@levino Btw.: The long term goal will be to have a core/types module with VO e.g.: Hex.from() A friendly contributor already started with some drafts here #2208

import {Hex} from 'web3';
const hex = Hex.from(100);
hex.toString();

Edit:
Gist draft for types module https://gist.github.com/nivida/d75c26daea14607f0c57b42582b34f78

@nivida nivida added the 2.x 2.0 related issues label Jun 20, 2019
@github-actions
Copy link

github-actions bot commented Jul 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions

@github-actions github-actions bot added the Stale Has not received enough activity label Jul 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x 2.0 related issues Enhancement Includes improvements or optimizations Stale Has not received enough activity
Projects
None yet
Development

No branches or pull requests

2 participants