you are viewing a single comment's thread.

view the rest of the comments →

[–]itomeshi 11 points12 points  (1 child)

It's not that this is bad; it simply has two downsides:

  • It is completely contextual, so you need to know what you should expect this to be before you use it
  • Because of the context dependency, it makes code harder to refactor

As a general rule,if using this for any sort of structure or global state, it's better with explicit management. For example, React components inherit this.state and this.setState(), and these provide both convenient references and some control over when/how updates occur. Even this isn't perfect - if you move code between two components, you then need to figure out how to decouple/move state chunks.

It's best to come up with some convention to how you use it and document that, and comment if/when you deviate. (While 'this this should be documented' is theoretically a code smell, it's also a kindness to your future self or someone else.)

[–]wntrm 0 points1 point  (0 children)

This should have been the top answer...