CertainPerformance

u/CertainPerformance


r/javascript•

I imagine this is a pretty common situation, but I've gotten very conflicting responses. In a MVC-ish structure, say that the controller is triggered and needs to share the updated data with some other parts of the application somehow. Feel free to assume that the data in question is quite simple, such as { width, height }.

There are a number of ways to do this:

Controller calls a method like setData in the /model/data.js module. Data module exports both setData and getData. When other modules need data, they can call getData.

Con: Data module is exporting more than one thing

Con: This is basically sharing global state, and global state should be avoided. (hopefully there is

45

r/learnjavascript•

I imagine this is a pretty common situation, but I've gotten very conflicting responses. In a MVC-ish structure, say that the controller is triggered and needs to share the updated data with some other parts of the application somehow. Feel free to assume that the data in question is quite simple, such as { width, height }.

There are a number of ways to do this:

Controller calls a method like setData in the /model/data.js module. Data module exports both setData and getData. When other modules need data, they can call getData.

  • Con: Data module is exporting more than one thing

  • Con: This is basically sharing global state, and global state should be avoided. (hopefully there

20

r/learnjavascript•

Elsewhere I posted this:

// Model module:
const data = {
  someProperty: 'value',
  // and others
};
// this module will occasionally assign to data.someProperty
function getSomeProperty() {
  return data.someProperty;
}
export { getSomeProperty, /* others not related to data */ };

I was told that this is horrendous code because it's shared mutable state, and that I should feel bad for writing it. There are a couple things I don't understand:

  • How can this get me into trouble? The data is stored only in one module, and only that module can change/mutate it. Other modules have access to getSomeProperty, which they call on demand when they need to,
00

r/learnprogramming•

Elsewhere I posted this:

// Model module:
const data = {
  someProperty: 'value',
  // and others
};
// this module will occasionally assign to data.someProperty
function getSomeProperty() {
  return data.someProperty;
}
export { getSomeProperty, /* others not related to data */ };

I was told that this is horrendous code because it's shared mutable state, and that I should feel bad for writing it. There are a couple things I don't understand:

  • How can this get me into trouble? The data is stored only in one module, and only that module can change/mutate it. Other modules have access to getSomeProperty, which they call on demand when they need to,
17

r/learnjavascript•

I've been advised that I should write only pure functions and stateless modules, but I don't really know where to start. Would someone be willing to give me an example of how a base script like this could be turned into stateless modules with pure functions?

https://jsfiddle.net/r4gbdqmz/

It's quite tiny, don't worry. I would really appreciate it so I can start figuring out how to refactor the right way, thanks!

05

You've reached the end of this feed.