Skip to main content
added 2 characters in body
Source Link
Doc Brown
  • 220.6k
  • 35
  • 410
  • 625

YouThe example is artificial enough thatto make the answer is clearly "it depends""it depends".

Ask yourself: will code like

 this.foo2 = new Foo(); this.callback(foo2, this.n); 

result in something meaningfulcorrectly working code? Or will it become a bug when the callback is called with a different Foo instance than the one where it belongs to?

If it results in a bug, you can make the code less errorprone by not passing thisremoving the redundancy and ommitting the superfluous Foo parameter. If it makes sense, then go ahead, use variant #1 with the additional Foo parameter.

You example is artificial enough that the answer is clearly "it depends".

Ask yourself: will code like

 this.foo2 = new Foo(); this.callback(foo2, this.n); 

result in something meaningful? Or will it become a bug when the callback is called with a different Foo instance than the one where it belongs to?

If it results in a bug, you can make the code less errorprone by not passing this. If it makes sense, then go ahead, use variant #1 with the additional Foo parameter.

The example is artificial enough to make the answer clearly "it depends".

Ask yourself: will code like

 this.foo2 = new Foo(); this.callback(foo2, this.n); 

result in correctly working code? Or will it become a bug when the callback is called with a different Foo instance than the one where it belongs to?

If it results in a bug, you can make the code less errorprone by removing the redundancy and ommitting the superfluous Foo parameter. If it makes sense, then go ahead, use variant #1 with the additional Foo parameter.

Source Link
Doc Brown
  • 220.6k
  • 35
  • 410
  • 625

You example is artificial enough that the answer is clearly "it depends".

Ask yourself: will code like

 this.foo2 = new Foo(); this.callback(foo2, this.n); 

result in something meaningful? Or will it become a bug when the callback is called with a different Foo instance than the one where it belongs to?

If it results in a bug, you can make the code less errorprone by not passing this. If it makes sense, then go ahead, use variant #1 with the additional Foo parameter.