white-label icon indicating copy to clipboard operation
white-label copied to clipboard

Why underscore private `_domainEvents` in AggregateRoot?

Open thiagodebastos opened this issue 4 years ago • 3 comments

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?

https://github.com/stemmlerjs/white-label/blob/503ee491192a3eff026704f4de9a7477daebb630/src/core/domain/AggregateRoot.ts#L8

thiagodebastos avatar Jul 22 '21 10:07 thiagodebastos

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.

adriana-olszak avatar Jul 22 '21 13:07 adriana-olszak

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 avatar Jul 23 '21 02:07 thiagodebastos

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

thiagodebastos avatar Jul 30 '21 03:07 thiagodebastos