Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why underscore private _domainEvents in AggregateRoot? #18

Open
thiagodebastos opened this issue Jul 22, 2021 · 3 comments
Open

Why underscore private _domainEvents in AggregateRoot? #18

thiagodebastos opened this issue Jul 22, 2021 · 3 comments

Comments

@thiagodebastos
Copy link

Hi @stemmlerjs ! Thank you for your efforts and your lessons. I've been reading a LOT of your blog and bought your book to learn more about DDD. I'm wondering why you've underscored _domainEvents here? From what I've learned from you, we don't have to do this in typescript since we already have access to the private keyword. Is there something I'm missing?

private _domainEvents: IDomainEvent[] = [];

@thiagodebastos thiagodebastos changed the title Why underscore private _domainEvents here? Why underscore private _domainEvents in AggregateRoot? Jul 22, 2021
@thiagodebastos thiagodebastos changed the title Why underscore private _domainEvents in AggregateRoot? Why underscore private _domainEvents in AggregateRoot? Jul 22, 2021
@adriana-olszak
Copy link

adriana-olszak commented Jul 22, 2021

Hey,

I believe that this is only a stylistic choice. In general, it's a common JS convention and even though it's not needed in TS lot's of developers still use it for readability.

@thiagodebastos
Copy link
Author

thiagodebastos commented Jul 23, 2021

Hey,

I believe that this is only a stylistic choice. In general, it's a common JS convention and even though it's not needed in TS lot's of developers still use it for readability.

Thanks for getting back to me @adriana-olszak :)

I understand what you are saying, I was more curious because in The SOLID book, Khalil specifically says to not do that in TypeScript:

Rule: Don’t use unnecessary member prefixes
Sometimes, developers prepend their private member variables with an underscore to signify that it’s private.
Example: _firstName.
In JavaScript, there’s no concept of private access modifiers, so the convention is to signal that it’s private this way.
In TypeScript, we do have private access modifiers.
Page 167

In typed languages like Java and TypeScript, usage of the underscore convention is discour-
aged because we have access to scopemodifiers like private and protected. We should use
the language constructs, given their availability, and look to conventions when a common
task cannot be done with the language.
Page 252

I was curious if there was a particular reason, or just a mistake here :)

@thiagodebastos
Copy link
Author

I think I understand now. We use _somePropOrMethod in an abstract class so that we can use somePropOrMethod in a concrete class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants