Appendix C: Usage guidelines
This is a list of quick guidelines intended to help you when writing Rx queries.
- Members that return a sequence should never return null. This applies to
IEnumerable<T>andIObservable<T>sequences. Return an empty sequence instead. - Dispose of subscriptions only if you need to unsubscribe from them early.
- Always provide an
OnErrorhandler. - Avoid blocking operators such as
First,FirstOrDefault,Last,LastOrDefault,Single,SingleOrDefaultandForEach.; use the non-blocking alternative such asFirstAsync. - Avoid switching back and forth between
IObservable<T>andIEnumerable<T> - Favour lazy evaluation over eager evaluation.
- Break large queries up into parts. Key indicators of a large query:
- nesting
- over 10 lines of query expression syntax
- using the
intokeyword
- Name your observables well, i.e. avoid using variable names like
query,q,xs,ys,subjectetc. - Avoid creating side effects. If you really can't avoid it, don't bury the side effects in callbacks for operators designed to be use functionally such as
SelectorWhere. Be explicit by using theDooperator. - Where possible, prefer
Observable.Createto subjects as a means of defining new Rx sources. - Avoid creating your own implementations of the
IObservable<T>interface. UseObservable.Create(or subjects if you really need to). - Avoid creating your own implementations of the
IObserver<T>interface. Favour using theSubscribeextension method overloads instead. - The application should define the concurrency model.
- If you need to schedule deferred work, use schedulers
- The
SubscribeOnandObserveOnoperators should always be right before aSubscribemethod. (So don't sandwich it, e.g.source.SubscribeOn(s).Where(x => x.Foo).)