Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for Rx extension methods?

So if I'm writing an extension method for a type in another library that converts that type into an Rx IObservable<T>, what exactly is the convention? I ask because I thought AsObservable was the way to go, but I've also seen ToObservable. It's unclear to me which is used when or if there's any real convention at all.

Could it be ToObservable is reserved for turning something that is expected to product a single event into an IObservable<T> where as AsObservable is reserved for converting something that is expected to produce a sequence of events into an IObervable<T>?

like image 929
Drew Marsh Avatar asked Aug 16 '13 23:08

Drew Marsh


People also ask

What naming convention should be used for method names?

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters.

What name can you use for an extension method sponsor class?

The class that defines such extension methods is referred to as the "sponsor" class, and it must be declared as static.


1 Answers

Unless you have a very good reason to write your own cross-duality operators, you will not need to write a "To" postfix when dealing with Enumerables and Observables.

Observe the following truths:

  • ToObservable is expected to convert pull-based sequences into push-based sequences.
  • ToEnumerable is expected to convert push-based sequences into pull-based sequences.
  • AsObservable is expected to wrap a push-based type as IObservable< T >.
  • AsEnumerable is expected to wrap a pull-based type as IEnumerable< T >.

Therefore, To should be used when you're writing a method which switches the duality of the source, and As should be used when the resulting duality is the same as the source's.

In most cases, you will be using As for your own methods, because the cross-duality operators of ToObservable and ToEnumerable have already been written for you.

Sources: Personal experience, MSDN documentation (above), Erik Meijer himself.

like image 123
cwharris Avatar answered Sep 24 '22 19:09

cwharris