318

Parent and children communicate via a service example from the official guide on Angular.io makes use of dollar signs in Observable stream names.

Notice missionAnnounced$ and missionConfirmed$ in the following example:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class MissionService {

  // Observable string sources
  private missionAnnouncedSource = new Subject<string>();
  private missionConfirmedSource = new Subject<string>();

  // Observable string streams
  missionAnnounced$ = this.missionAnnouncedSource.asObservable();
  missionConfirmed$ = this.missionConfirmedSource.asObservable();

  // Service message commands
  announceMission(mission: string) {
    this.missionAnnouncedSource.next(mission);
  }

  confirmMission(astronaut: string) {
    this.missionConfirmedSource.next(astronaut);
  }
}

Can anyone explain:

  • why $ is used? What's the reason behind this notation? Do I always need to use this for public properties?
  • public properties are used but not methods (e.g. missionAnnouncements(), missionConfirmations()) - again, is this a convention for Angular2 apps?
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
gerasalus
  • 7,538
  • 6
  • 44
  • 66

4 Answers4

447

$ suffix (popularized by Cycle.js) is used to indicate that the variable is an Observable. It could make it to the official style guide too but it's not there yet

Read more here : What does the suffixed dollar sign $ mean?

Update: Read more about the trailing “$” sign on Angular website here: https://angular.io/guide/rx-library#naming-conventions-for-observables

Monfa.red
  • 4,532
  • 1
  • 12
  • 14
  • 28
    Reference in the angular docs: https://angular.io/guide/rx-library#naming-conventions-for-observables – michelepatrassi May 18 '18 at 11:40
  • 2
    what about promises? – galki Dec 21 '18 at 10:40
  • 1
    Is it only the observable or also subjects and other types of streams? – Sarah Apr 16 '21 at 10:33
  • 1
    @Sarah: It was mentioned by @Rachel Diaz that Andre Staltz started this style. I found on his [github](https://github.com/staltz/mvi-example/blob/e96e2fb67732f6b48816dbe450adf21d493c1a83/src/views/items.js#L11) that be uses this convention for behavior subjects as well: `var modelItems$ = new Rx.BehaviorSubject(null);` – Otacilio Oliveira Jun 10 '22 at 16:13
22

The $ naming paradigm originated with Andre Saltz and suggests pluralizing all variable names that contain observables or streams.

getAll(): Observable<Zone[]>{
    let zone$ = this.http
      .get(`${this.baseUrl}/zones`, {headers: this.getHeaders()})
      .map(mapZone);
      return zone$;
  }

Another approach is to pluralize variable names that contain observables or streams with a unicode character that matches the last letter of the word. This addresses the issue with words that aren't pluralized with an "s".

mouse$ vs mic€

Neither of these naming conventions are in the official Angular style guide. Usage of one or the other (or none) is entirely dependent on personal preference.

Raquel Diaz
  • 221
  • 2
  • 4
  • 21
    cactu$ vs cactï – BYTE RIDER Sep 13 '17 at 13:05
  • 1
    Nice reference! Also check out this article. What annoys me is finding an attempt to do this in my codebase (other co-workers) and getting it wrong, putting the suffix on the wrong variable or even worse starting the variable with it. I have seen people use this without consistency also, in that case it makes completely no sense. https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b – Eric Bishard Mar 24 '18 at 23:11
  • If you are to use it, I would reccomend following naming conventions like in this repo: https://github.com/bodiddlie/rxheroes/blob/master/app/effects/hero.ts And also doing it always or never. Be consistent for the observable god's sake. – Eric Bishard Mar 24 '18 at 23:14
  • 5
    `fish$` vs `fish€$` – Martin Schneider Apr 01 '18 at 20:45
  • 3
    The naming convention for observables can now be found in the official documentation: https://angular.io/guide/rx-library#naming-conventions-for-observables – Consta Gorgan Apr 03 '21 at 15:40
  • Whoa! I never knew that the `$` in observables was even related to the fact that it looks like an s. – Ruan Mendes Feb 04 '22 at 16:58
16

Update: https://angular.io/guide/rx-library#naming-conventions-for-observables

Because Angular applications are mostly written in TypeScript, you will typically know when a variable is an observable. Although the Angular framework does not enforce a naming convention for observables, you will often see observables named with a trailing “$” sign.

This can be useful when scanning through code and looking for observable values. Also, if you want a property to store the most recent value from an observable, it can be convenient to simply use the same name with or without the “$”.


Original:

I saw variables end with $ when reading the official hero tutorial:

<div id="search-component">
  <h4>Hero Search</h4>

  <input #searchBox id="search-box" (keyup)="search(searchBox.value)" />

  <ul class="search-result">
    <li *ngFor="let hero of heroes$ | async" >
      <a routerLink="/detail/{{hero.id}}">
        {{hero.name}}
      </a>
    </li>
  </ul>
</div>

Look closely and you'll see that the *ngFor iterates over a list called heroes$, not heroes.

<li *ngFor="let hero of heroes$ | async" >

The $ is a convention that indicates heroes$ is an Observable, not an array.

Most cases are that we do not subscribe to those Observable variables in component. We usually use AsyncPipe to subscribe to the Observable variables automatically

I haven't found it in Style Guide since Angular5.1 has released yesterday(December 6th, 2017).

Community
  • 1
  • 1
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
10

I haven't seen this $ in the style guide but I saw it being used frequently for public properties that refer to observables that can be subscribed to.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567