Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upConflict between `no-new` and `no-unused-vars` rules #958
Comments
This comment has been minimized.
This comment has been minimized.
|
Change your API so new isn't used for side effects, or disable the rule for
that specific line. Warnings exist to tell you something is a bad idea.
Disabling rules is possible so you can disagree.
…On Tue, Jul 25, 2017, 18:54 Joshua Barnett ***@***.***> wrote:
If I want to instantiate a class as part of an initialization process but
I do nothing with the instance after the fact I run into the two dilemmas.
// no-unused-varsconst gameEngineInstance = new GameEngine()
If I remove the unused variable I get the following
// no-newnew GameEngine()
If I do the following it won't instantiate the class.
GameEngine()
So what should I do in this example to comply with standard?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#958>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACWlep8YoV476xyZQZcNzFCTp23jPl_Fks5sRh3hgaJpZM4Oi1Ha>
.
|
This comment has been minimized.
This comment has been minimized.
|
I agree with what @yoshuawuyts said, it's very bad practice to have side effects happen in a constructor. I would recommend refactoring so that it isn't necessary. If you do want to continue doing so, you can disable linting for that line, like so: // eslint-disable-next-line
new GameEngine() |
LinusU
closed this
Jul 31, 2017
This comment has been minimized.
This comment has been minimized.
|
@LinusU can you link me to some information or an example of how I might write such an API? |
This comment has been minimized.
This comment has been minimized.
|
Move the side-effects into a function or method call of an instance. E.G. collect options on instantiation, then once instantiated, run a .mount() or .start() method that does whatever side-effects you were doing in the instantiation. |
This comment has been minimized.
This comment has been minimized.
|
In this specific case, I can imagine that the GameEngine sets up some kind of loop that runs every frame or similar. Instead of doing that in the constructor move that to a e.g. old: class GameEngine {
constructor () {
// ...
setInterval(() => this.renderFrame(), 1000 / 30)
}
// ...
}
// ...
new GameEngine()new: class GameEngine {
constructor () {
// ...
}
start () {
setInterval(() => this.renderFrame(), 1000 / 30)
}
// ...
}
// ...
const engine = new GameEngine()
engine.start() |
This comment has been minimized.
This comment has been minimized.
|
I'm The constructor it initialises variables with sensible defaults does
that count as a side effect?
…On Mon, 31 Jul 2017, 21:04 Linus Unnebäck, ***@***.***> wrote:
In this specific case, I can imagine that the GameEngine sets up some kind
of loop that runs every frame or similar. Instead of doing that in the
constructor move that to a .start() function.
e.g.
old:
class GameEngine {
constructor () {
// ...
setInterval(() => this.renderFrame(), 1000 / 30)
}
// ...
}
// ...
new GameEngine()
new:
class GameEngine {
constructor () {
// ...
}
start () {
setInterval(() => this.renderFrame(), 1000 / 30)
}
// ...
}
// ...
const engine = new GameEngine()
engine.start()
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#958 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABv6Rh6akfbblvU_645_Q9KtPhT805wkks5sTjNdgaJpZM4Oi1Ha>
.
|
This comment has been minimized.
This comment has been minimized.
No, that is how it should be Side effects are something that affects anything outside of the class itself. So it's perfectly fine to modify anything that beings with
|
synthecypher commentedJul 25, 2017
•
edited
If I want to instantiate a class as part of an initialization process but I do nothing with the instance after the fact I run into the two dilemmas.
If I remove the unused variable I get the following
If I do the following it won't instantiate the class.
GameEngine()So what should I do in this example to comply with standard?