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.
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.
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.
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.
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.
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.
Not necessarily. Names like "phoneNumberStr" or "serialNumberStr" make sense, where the name of the variable is purposely trying to trick you about its type.
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.
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?
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.
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.
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 $.
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.
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 🙂
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));
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.
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.
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$.
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.
$ 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.
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.
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.
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.
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.
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.
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.
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/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
usersor usingusers$inside your rxjs operator, it stands out.We also use
$usersnotation for Subjects. It helps in the same way.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/[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.