75

Here is the code I'm reviewing...

import { Observable } from 'rxjs/Rx';
// reducer
import { playerRegister, PlayerState } from './player';
export function getPlayer$ (state$: Observable<MyAppState>): Observable<PlayerState> {
  return state$.select(state => state.player);
};
Thom
  • 14,013
  • 25
  • 105
  • 185
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • 10
    Some people use it to indicate that the function is asynchronous. – Robby Cornelissen Mar 29 '17 at 03:07
  • 16
    `$` is just a character with absolutely no significance – Jaromanda X Mar 29 '17 at 03:07
  • @robby-cornelissen thanks, you can move it to answer. – ishandutta2007 Mar 29 '17 at 03:10
  • Also have a look at [Why would a JavaScript variable start with a dollar sign?](http://stackoverflow.com/q/205853/1048572) – Bergi Mar 29 '17 at 03:18
  • Observables and Finnish Notation: https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b – cartant Mar 29 '17 at 07:49
  • Some developers use $ convention for indicating dom element. So in your question it might be used to indicate the function which will modify the dom elements. – Tushar Kotlapure Mar 29 '17 at 18:27
  • 39
    @JaromandaX I think to say "`$` is just a character with no significance" isn't particularly helpful as the OP has obviously seen, or suspects that, there is some convention in place whereby the `$` is used to _indicate_ some quality of the variable. Hence the question "What [does it] indicate?". A similar question could be "What does the `Factory` suffix indicate on a class name?" and an equally unhelpful response would be "The characters `F`, `a`, `c`, `t`, `o`, `r` and `y` have no special significance". – El Ronnoco Dec 21 '18 at 14:10
  • @ElRonnoco - that's your opinion, and no, the word "Factory" conveys meaning, because it's a word with meaning. The symbol "$" in a variable name or object property does not have any significance in the code posted or anywhere else for that matter – Jaromanda X Dec 21 '18 at 21:51
  • I have started liking this convention. Mainly because the way you treat a regular array vs observable is so much different. Making it super easy while coding as well as when reviewing a code. – Sarang Jun 04 '20 at 05:47

4 Answers4

166

Syntactically, the dollar ($) character has no special meaning in JavaScript identifiers.

It is, however, sometimes used by convention to indicate that a variable holds an Observable or that a function will return an Observable.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • 1
    Never seen that. Is this convention codified anywhere, does a popular library use it? – Bergi Mar 29 '17 at 03:17
  • @Bergi I've seen it around a couple of times. As far as I know, not limited to a specific library or framework, but looking into it further. – Robby Cornelissen Mar 29 '17 at 03:18
  • 2
    I don't want to split hairs, but Observables are not functions, and the logic within them or attached to them is not necessarily executed asynchronously. – Matt Burnell Mar 29 '17 at 03:20
  • 1
    @Bergi A reference to Cycle.js mentioned [here](http://stackoverflow.com/questions/37671700/angular2-style-guide-property-with-dollar-sign) – Robby Cornelissen Mar 29 '17 at 03:20
  • @MattBurnell You're right. I have however seen it being used for observables as well as for promises and other types of async functions. Will update my answer. – Robby Cornelissen Mar 29 '17 at 03:23
  • $ suffix can be used for anything as a personal choice, but there's no convention to use it with promises or other async stuff. It's for observables only. – Estus Flask Dec 12 '17 at 08:14
  • @estus Can't find any public reference anymore, so limited my answer to Observables. – Robby Cornelissen Dec 12 '17 at 09:34
  • @ Matt Burnell: I think he refers to a function that returns an observable. At least that is when I use it. I define a getter that returns an observable and, to indicate that, I add a $ to its name. – yogibimbi Feb 12 '21 at 13:08
48

This is a code convention named Finnish Notation, apparently due to the origin of the developer that is attributed for first using it. It's used to indicate the Observable type of a variable or function.

The idea is that an Observable usually represents a stream of multiple Values and a pluralized variable / function name would indicate this. To not be confused with array variables (which are usually also pluralized), the $ character is used instead of the s. When reading the variable, you'd read the $ as an s.

Example

When naming an array, you'll most likely use the proper plural form of a single element's name, as in:

const pets = ['cat', 'dog', 'turtle']

While, if you had an observable that emitted those three values, you'd use:

const pet$ = from(['cat', 'dog', 'turtle']) // read: pets

It's up to you and your team whether you want to use it. I guess there is no explicit consensus as of now, so you can have a long and meaningful argument about it ;-). There are already tslint rules available that allow you to enforce your decision.

ggradnig
  • 13,119
  • 2
  • 37
  • 61
  • 4
    Thank you for not only explaining _how_ it is used, but also _why_, and _what_ it is called. Your thoroughness makes this a much more useful answer, and makes the concept easier to remember! – maurice Jul 21 '21 at 16:49
20

I'm not sure if it's used more widely than within the RxJS community, but within this community it's commonly used to indicate that a variable is a stream (i.e. an Observable) or that a function returns such a stream.

Matt Burnell
  • 2,646
  • 1
  • 22
  • 23
6

For a function it means it returns an observable.

For a variable it means it is an observable.

This notation is widely used in Angular projects and I find it very useful to quickly see that it is an observable and not the actual value.