2

So I am learning reactive programming, and I am building an app for fun that listens on various stock information tickers, and calculates specific things to show in charts etc.

So right now I have information coming from different sources, each of one is one RxJS observable (think stream), so my "ticker$" observable is a combineLatest of all the different ones.

Then I calculate different things based off the arrays that the ticker$ observable emits, and ultimately I do a mergeAll() on the calculations result observable, so from that point on I have a flattened observable with useful information over time.

The my question is about maintaining state, performing side effects, things like that. If I want to continue chaining observables this way, I end up adding to the inputs of the observable things that I calculate and thus produce a new output containing new and old information, sort of Railway Oriented Programming, Since if I just map the values and produce new changed output I end up loosing information and later operators for instance can't log out previous data.

There is also the problem of maintaining state, let's say I use scan to maintain some state but then that state it's needed in another part of the app, what would be a good way of handling this?.

1

Ideally, your pipelines should be kept side-effect free. I know this is not always feasible, in which case I have a few recommendations (which I'm sure could be extended):

  • Avoid side effects, if at all possible

    I know, I already said this. But it's worth repeating.

  • Make side-effects explicit

    Rx has the do function for this. (See e.g. here). By following this standard practice, at least it's easy to see, by looking at the pipeline, that there is a side-effect that needs to be taken into consideration.

  • Keep your side effects focused

    Don't do multiple unrelated side-effects at once. This is not specific to reactive programming, of course, but it is especially important here. The main concern here is to make the side effect very easy to understand. But additionally, you might be able to re-use the same function in multiple places, which means I only have to understand what's happening once.

  • Keep side effects local

    If a function in your pipeline changes state, it should preferably own that state and be the only function that changes it. You mentioned state that is needed in other parts of the app. You should try to see if that's really required. It will make it significantly harder to track what's going on.

    If you really can't do without it, try to find a way to mark the shared state as such. As clearly as possible! By whatever means necessary!


You mentioned that some of your transformations take your input and append information to it. That might be just the way to go in many scenarios - and it is not a side effect.

For example, if your inputs are share price and earnings per share, you might want to calculate the price/earnings-ratio and return all three values for later calculations.

Just make sure you keep your data immutable, i.e. don't append anything to your input data, but make an augmented copy and return that as your output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.