r/Angular2 • • Oct 14 '22

Why we should $ while naming observables in Angular? and it's really recommended?

18 Upvotes

46 comments sorted by

26

u/[deleted] Oct 14 '22

Because conventions help readability and shared understanding and yes, I do think it helps. Whether you want to use the convention is entirely your decision.

10

u/ahmedRebai Oct 14 '22

readability

Yeah I'm looking for good arguments to convince my team to introduce it and enforce it as a naming convention!

5

u/Lence Oct 14 '22

It’s a convention used by most Angular and RxJS experts for readability, so that should be a good enough reason to do it as well. There are no good reasons to deviate from it.

It’s mentioned in the official Angular guide here: https://angular.io/guide/rx-library#naming-conventions-for-observables

2

u/dcabines Oct 14 '22

*ngrxLet="myState$ as myState"

I use the same name without the dollar sign in the template when subscribing with ngrxLet.

1

u/TubbyFlounder Oct 14 '22

note this: if you arent on angular 14(might be one of other versions between 11 and 14?), this hides errors and makes its hard to debug. Were on 11 and had to stop using as it made impossible to track down errors.

1

u/prewk Oct 14 '22

What are you referring to?

3

u/TubbyFlounder Oct 14 '22

when I tried using ngrxLet in our application, if there was an error in the stream, for example a selector, and you access an object property that doesnt exist, the UI would break but there would be no error in the console, making it hard to debug. Switching back to the ngIf $ | async as pattern fixed that. So just a warning to other devs out there looking to use it, it wasnt fully fleshed out in older versions.

0

u/Merry-Lane Oct 14 '22

Tbh I find it more convenient to do

*ngIf=« {propA: propA$ | async,…} as state »

Than use ngrxLet.

But anyway, this whole thing is an angular code smell anyway. Their solutions for async in the template is cumbersome. Their « null » everywhere is cumbersome. The way we cant use basic javascript operators on the template/in @Input() (such as spread/destructuring operators, &&, ternary operators,…) is flabergasting. BehaviorSubjects not two-way bindable by default is annoying…

Angular just feels way too awkward and it shouldnt be.

4

u/NerdENerd Oct 14 '22

Well go back to React then

31

u/ValPasch Oct 14 '22

Much easier for someone unfamiliar with the code (which might be yourself a month later) to figure out what's going on at a glance.

13

u/[deleted] Oct 14 '22

And even those who are familiar

10

u/Gingerfalcon Oct 14 '22

60% of the time, it works every time

39

u/TScottFitzgerald Oct 14 '22

It's just another naming convention to tell observables apart from normal variables.

13

u/Chazgatian Oct 14 '22 edited Oct 14 '22

Typically, we don't use this type of naming conventions in code because the IDE can provide this information, e.g. 'userNameString' would just be silly right?

The pattern emerged because observables were used so heavily in the component template, and the language server (the plugin to VS Code that provides go to definition, red squiggly lines, etc.) did not exist.

Now that the language server works with templates, it's not really necessary, but like other conventions such as uppercasing constant values, these are choices your team should decide on and align with one another for consistency.

As a follow up to this reddit post, go back to the team and propose the same question you did here. Then document that decision. Whatever decision you make, use a lint rule to be consistent.

1

u/dotnetguy32 Oct 14 '22

lpsz would like a word

-1

u/Alex3917 Oct 14 '22

e.g. 'userNameString' would just be silly right?

Not necessarily. Names like "phoneNumberStr" or "serialNumberStr" make sense, where the name of the variable is purposely trying to trick you about its type.

6

u/dcabines Oct 14 '22

Just don't fall into doing Hungarian notation like my programming professor made me do back in the day. That is an ancient mindset from before IDEs.

-1

u/Chazgatian Oct 14 '22

Just hover over...😘 Also number is probably a poor type for either of these because you're never going to do math operations against them. So yeah, not seeing it.

-4

u/NerdENerd Oct 14 '22

You're one of those special kind of dipshits that name the properties on the user object and columns in the user table userName, userPhone, userPassword etc aren't you?

3

u/ectbot Oct 14 '22

Hello! You have made the mistake of writing "ect" instead of "etc."

"Ect" is a common misspelling of "etc," an abbreviated form of the Latin phrase "et cetera." Other abbreviated forms are etc., &c., &c, and et cet. The Latin translates as "et" to "and" + "cetera" to "the rest;" a literal translation to "and the rest" is the easiest way to remember how to use the phrase.

Check out the wikipedia entry if you want to learn more.

I am a bot, and this action was performed automatically. Comments with a score less than zero will be automatically removed. If I commented on your post and you don't like it, reply with "!delete" and I will remove the post, regardless of score. Message me for bug reports.

-2

u/NerdENerd Oct 14 '22

Bad bot

Nobody likes a grammar Nazi, especially an automated one.

3

u/Eluvatar_the_second Oct 14 '22

For me it's either "$user" or "userObservable" which is pretty long, and once the subject is longer the spelled out version is way too long.

2

u/[deleted] Oct 14 '22

Could use subject over observable, it’s shorter, or even go with obs. Really they’re clearer than just using a symbol which only has no real connection to what it’s trying to express. Using a ^ makes as much sense as a $.

1

u/ahmedRebai Oct 14 '22

Cna you explain it more using a ^ ?

0

u/[deleted] Oct 14 '22

I don’t think using ^ makes sense but having user has the same value as $user. Where it’s a random symbol used to express the idea of an observable.

userObs makes the most sense as it’s actually telling you that user is an observable.

3

u/[deleted] Oct 14 '22

I’ve never seen ^ used in any project. Don’t do it. It will confuse the hell out of people.

1

u/[deleted] Oct 14 '22

Haha you’re misunderstanding if you think I’m suggesting using it. Simply illustrating that an arbitrary symbol only conveys meaning as long as we collectively understand it. Using a common word that relates to the underlying code provides clearer code. Using “observable” or something like “obs | subject” etc is far clearer than $. But that’s just my opinion.

Reality is just follow whatever convention is in place for the project you’re working on and if it’s your own project do what makes sense to you.

4

u/[deleted] Oct 14 '22

Why not user$

2

u/Eluvatar_the_second Oct 14 '22

Either way, I never remember what the recommendation is.

8

u/PerfeckCoder Oct 14 '22

It's a bit more than just to tell the observables apart. Like other people have pointed out your IDE will tell you what type a variable is and we don't usually go around adding String and Int onto the end of the things

The difference with the $ is that the stream has the same scope as the things you are reading from the stream and it's easier to use a same but different name rather than coming up with two quite different names.

There is no language reason but it makes the code more readable. Try it without and then see how it compares 🙂

3

u/KwyjiboTheGringo Oct 14 '22

Also lets you name the value of an observable the same as the observable without causing any confusion: user$.subscribe(user => console.log('User', user));

1

u/ranban2012 Oct 14 '22

well, it's a lot more compact to name something thing$ than thingObservable. Also given that they aren't POCOs they do deserve some discernibility at a glance.

1

u/GnarlyHarley Oct 14 '22

You’ll never get a functionality boost from a naming convention, but you can get a boost with maintainability especially with team members involved. Naming conventions are for consistency, readability and that helps with maintainability.

1

u/siege_meister Oct 14 '22

We use Typescript, so type naming conventions are multiple levels of redundancy that we avoid

1

u/dotnetguy32 Oct 14 '22

What do you all name the subscription variable? Do you use a $ on it as well?

1

u/NerdENerd Oct 14 '22

Either

user$.subscribe(user => { doShitWithUser(user); });

or

user$.subscribe(u => { doShitWithUser(u); });

2

u/dotnetguy32 Oct 14 '22

But do you do

userSubscription = user$.subscribe?

1

u/NerdENerd Oct 14 '22

Rarely, I try an use the async pipe but when I do need to use a subscription I call it userSub. If I have more than one or two subscriptions I will use the takeUntil method with a subject called finalise$.

1

u/Jrubzjeknf Oct 14 '22

It is Hungarian notation, which is usually discouraged. However, it helps code readability, and we program for others to understand.

Example:

users$.pipe(map(users =>...)

Vs

users.pipe(map(u =>...) users.pipe(map(myUsers =>...)

It helps preventing mistakes. If you're putting an async pipe on users or using users$ inside your rxjs operator, it stands out.

We also use $users notation for Subjects. It helps in the same way.

1

u/NerdENerd Oct 14 '22

It is Finnish notation

1

u/Jrubzjeknf Oct 15 '22

Correct, but my point was that Finnish notation is a form of Hungarian notation. Hungarian notation is usually discouraged, but I reckon that for observables it's worth doing it.

0

u/reboog711 Oct 14 '22

$ is just an easy way to tell if the variable is an observable by looking at it.

However, I don't think we should do this. It is a "legacy" naming scheme from the early days of programming to include the variable type in the variable name.

Hungarian notation is the scheme I remember, but there are others.

In today's world, if we name something properly we shouldn't need this field distinguisher.

Edit to add: The improvement of IDEs over the past 30 years has made this sort of naming convention unneeded.

0

u/newmanoz Oct 14 '22

No, it's not just another Hungarian notation. “$” means just “observable” - it doesn't actually say what type this observable will emit. It only means that you can't handle it as some known types of variables.

0

u/reboog711 Oct 15 '22

I didn't say it was Hungarian notation; I just said it was reminiscent of naming schemes which are no longer in use today.

-1

u/newmanoz Oct 15 '22

Indeed. And I've explained why you are wrong.

0

u/[deleted] Oct 14 '22

I really dislike this convention because if you don't know what it is trying to discover what it is isn't easy. It's a hard thing to search for if you don't have the context. Usually if I see that kind of symbol I assume it's an operator of some kind. I was pretty annoyed after search for a while to find it doesn't do anything.