Mastodon

Dollar Signs in Variable Names for Observables in Typescript

Recently, my team and I had a discussion about naming conventions in TypeScript. The question was if to name observables with or without a dollar sign like this:

missionAnnounced$ = this.missionAnnouncedSource.asObservable();

I would rather write this variable without the dollar sign.

Reason 1: The type of a variable can be determined by using functionality of the IDE. IntelliJ IDEA for example shows information about variables, methods and classes by pressing Ctrl + Q.

Reason 2: Using variable names to reflect technical information that have nothing to do with the business logic is an antipattern in Java. Some time ago, interfaces have been prefixed with an „I“ - see this article:

„If your interface is a Truck, call it Truck. Not ITruck because it isn’t an ITruck it is a Truck. An Interface in Java is a Type. Then you have implementations of DumpTruck, TransferTruck, WreckerTruck, CementTruck, etc.”

Reason 3: Researching the topic, I wanted to find proof that other developers agree with me and that using the dollar sign as a hint for an observable is an antipattern in TypeScript, too. However, I found this which links to the official Angular page, that states:

“… you will often see observables named with a trailing “$” sign. This can be useful when scanning through code and looking for observable values …”

That is not exactly a recommendation to use the dollar sign. But it is at least OK to do it.

My opinion may be logically sound, but it contradicts the current Angular best-practices. Therefore, I withdrew the recommendation for my team to get rid of the dollar signs.

EDIT: Sven Laschinski mentioned that the HTML templates could be easier to read if observables are recongnizable as such. I agree. Well, this matter seems to not have a simple solution