-
Notifications
You must be signed in to change notification settings - Fork 884
discussion(style-guide) Drop _-prefix for private fields in TypeScript #1108
Comments
Or maybe we should change Microsoft's guidelines :) I like the underscore for this cases. |
@wardbell states it very nice in this issue. Underscore is a good indicator during debugging and as @Foxandxss states also, it can be really helpful for developers that design directives for public consumption. |
Problem with Microsoft guidelines is that they are good for their usage but could be not the case of Angular 2. For example, you can do like:
In the first case, you are using Without the underscore, we have no way on telling "hey, you shouldn't use this, don't come later telling that I didn't warn you". We can't prevent (at least that I know) people from accessing everything inside the component, even private stuff, so at least the underscore gives them a big hint. |
I guess this is also for Dart "compatibility" where the |
That too. |
I think it is also helpful for people using plain old JavaScript, where access modifiers aren't available. |
Hi guys, ok - I kind of understand some of your arguments, but to me it still seems, that most of them are only valid when thinking in "TypeScript is JavaScript" . I'm a little "scared" that by promoting underscore-prefix we would motivate people to just use their well-known patterns from JS-world just because they are used to it. So let me try to make my points:
I really don't think that people writing TypeScript-applications are going to debug their applications in compiled JS. This is a total PITA and it's super easy to configure Source-Maps to debug in TypeScript. {{dir.foo}} {{dir._bar}} IMO this argument only persist as long as you don't use a good IDE. E.g. Webstorm already only shows the public fields of an object for auto-completion and I'm totally sure that tooling-support for ng2 and TypeScript will become even better in the future: class CalendarEntry {
private formatter: any;
constructor(public startTime: string, public endTime?: string) {
}
}
@Component({
selector: 'calendar',
...
})
export class CalendarComponent {
calendarEntry: CalendarEntry;
} Also you don't ask people to prefix their interfaces with "I" like
In my opinion you should use Dart-conventions when writing your apps in Dart and you should use TypeScript-conventions when writing apps in TypeScript (So when writing apps in Plain old JS you should also use the underscore-prefix). You would never ask a Dart-Developer to name their files with dashes (e.g. customer-management.service.dart) as Dart uses underscores for filenames. I think that the "mental model" should be, that it's a coincidence that TypeScript will become JS to be used in the browser, so IMO it's quite of bad to transfer old "JS-Hacks" in the shiny new TS-world ;) Still I'm not 100% sure about it, but I think Angular should be really careful what to promote here, especially when violating the underlying languages rules. |
This would make ts2dart more complicated because names would need to be changed. In your application, if you don't intend to make your packages available to Dart or JS, then you are free to not use |
@zoechi I see that this is true for the development of angular itself (the framework) but I just don't see the use-case to compile my own app to Dart, or am I missing something here?
I think you really hit the point here: contributing to angular has kind of special requirements because of the ts2dart thing etc. but for at least 99% of the people out there this is irrelevant. IMO the style-guide should be meant for the developers out there getting up and running with the platform - for Contributors there already is a detailed documentation on what to pay attention (https://github.com/angular/angular/blob/master/CONTRIBUTING.md#rules) |
While it's not important for apps, when developing re-usable "3rd-party" modules/widgets/libraries that are meant to be consumed by people which might not use TypeScript (i.e. consume the compiled code), it is much better to preserve some semantics that can't be otherwise communicated in "plain" JS. Like I said, for an app (that is not meant to be consumed as a dependency by another product), it doesn't make a difference. But it would be very confusing to have a guideline that says: "Use this naming convention when developing a 3rd-party module, but use another naming convention when developing your app". Not to mention that not all people are using IDEs 😛 Anyway, it's a less than ideal situation because of the fragmentation (different languages, differents IDEs/editors, different tools), but (for the time being) the m2c |
I think this is the main point where our opinions differ. When creating a public API you always need to think about the target-system. E.g. when creating a JSON/REST-Interface in Ruby you would not define the fields with snake_case, but you would also not write all your ruby code in camelCase just because of an API you need to provide. Looking at all major blogs and resources writing about Angular 2 you can see that people are already embracing the new language features. E.g. @PascalPrecht's toughtram (http://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html) @vsavkin's blog (http://victorsavkin.com/post/137821436516/managing-state-in-angular-2-applications) @gsans's medium articles (https://medium.com/google-developer-experts/angular-2-introduction-to-redux-1cf18af27e6e#.94p98vo6x), the ionic-blog (http://blog.ionic.io/ionic-2-and-auth0/). @jashmenn's ng-book (https://www.ng-book.com/2/), all of the currently available popular starter kits, the egghead and udemy video courses. etc. etc. I really don't think that all of them did that accidentally, so this shows, that people "like" the new TypeScript features and use them. I would really love to hear the opinions of more people then the ones already involved in the decision here johnpapa/angular-styleguide#703. Maybe I overrate the namings, but now asking developers to stick with their old habits (and violate microsofts guidelines for TS) just because of some JS-Backwards-compability things, to me seems like a big step backwards... |
Languages that don't have public / private accessors need a convention such as _ to "suggest" what is and isn't safe to use. When accessors are available the convention is no longer required and can actually become unhelpful because it can now be wrong and misleading. Suppose you have There should never be that many variables in a class or such a big block of code that being able to see what is an isn't public is a major undertaking IMO. |
I have been using ts for a while (not with angular) and a week ago we started using ng2 in our new projects, and one of my colleague used '_variable' names for private variables and he told us that it's in the recommendation. I just didn't want to believe him so that is how I ended up here. I find it very weird and unnecessary to prefix private variables with an underscore when we code in typescript.
I think this is the point, the style guide itself is written in typescript, so it should embrace good practices in typescript not best practices in plain js or dart. |
My $0.02. I'm starting to regret my support for The best argument I've seen here is cross-language support ... both in code and the style guide. @choelle is convincing w/r/t the direction of the TypeScript ecosystem and the dubious need to cater to people debugging TS in JS (people like me ;-)). IMO, it is not possible to write a cross-language StyleGuide. While many of the recommendations makes sense everywhere ("Avoid data access code in components; delegate to a data access service"), many are language specific. One simply does not write components in JS the way one would in TS. Our first Style Guide is a TS Style Guide. We have the ability to reuse, share, and swap guidelines for Style Guides versioned in other language tracks. Therefore, I vote for dropping the Let's see what @johnpapa thinks. p.s.: I think Microsoft guidelines are an important source. They are based on deep experience of talented developers. We should be reluctant to depart from them and not do so casually. But, IMO, they are not determining factors either. |
I 100% agree with no underscores. It's already in my revised draft. You will see it in the next week or so. This is a typescript style guide. So we will use what makes the most sense for Ts |
BTW - keep in mind that this guide is very very very very early and not publicly linked to in the menu. This is on purpose. Once we feel there is gold here, we will add it to the menu. But right now, it's placeholders and thus not ready for public consumption. That all said, I'll repeat I 100% agree in removing the Thanks for the conversation. |
It's not just the style guide. The tutorial on angular.io says:
|
That is not a problem. Once the styleguide is in and we have our conventions stablished, we will update all the doc. |
ONE EXCEPTION. I still like
|
Oh, forgot about that one too. Here we have no choice I think, the modifiers won't work in this case so underscore is needed. |
@Foxandxss that's fine. PRs work :) |
Cool, glad to hear this! Thanks a lot for the feedback! I guess I should close this one now... |
Re-opening so that we remember to sweep the docs and samples! Jesus - if you are up to it and want to start the process, please do! |
I like underscore prefix, because it's easy to know it's private when I just read the code. Not everybody has or likes WebStorm. Readable code should not depends on some special IDE. I don't think all typescript guidelines from MS is good, such as
So I like @Foxandxss words:
|
We opted to drop the |
If I have a property like If I drop the underscore? How do I name the private property different than the get operator? |
Thanks. How to tell Resharper this :) |
I have never used ReSharper but in Webstorm you can create snippets with predefined jump locations in the snippet. That could be a solution for you. :) |
The referenced coding guidelines are used for the TS code base and they are not a recommendation for other projects, see microsoft/TypeScript#11227 |
@johnpapa I'm curious if there is a specific design reason why _ prefixing is not desired for private members? Honestly not disputing the decision, just want to understand if there are specific design reasons vs preference. |
because underscore was used in the past with javascript because there was no private, but there is no need nowadays. |
@Foxandxss Thanks. Definitely makes sense that it isn't necessary. I just want to understand if there is anything inherently bad in using _ or any gotchas I'm not thinking of if it is a convention I decide to use. |
There is no problem at all if you do that. The most important thing is being consistent. Either you do it or you don't. But not in certain files only and not in others. |
NB I still find it handy for backing fields as in
|
I see it problematic not to use underscores in this case:
this will cause a duplicate entry error. having an underscore as a prefix would have solved this issue 😒 |
@denu5 As the above comment says, its desired to use the underscore when a getter/setter is defined. |
Am I wrong and the founding fathers already ad this disscussion long time ago and that's (LONG TIME) maybe why the question keeps coming up? To me it seems that this is so unimportant that once you read the book (from the founding fathers) you just follow up and forget about it... https://en.wikipedia.org/wiki/Hungarian_notation |
@josersleal ok, so you never use the new cool es6 getters/setter. what will you do in this situation?
Ps. which book? i'm interested to improve oop skills 😄 |
Just thinking ..is there any way to add the underscore automatically for private members during compilation? ..a compiler option could even allow to decide if it is used. Is that impossible? |
For getter/setter, it would be nice the compiler to understand this, like in Java:
|
Another of my head...
EDIT: nevermind this, only useful in constructor... |
Plain JS already have getter/setter methods: class MyClass {
let num = 5
get specialNumber() {
return this.num + 2;
}
set specialNumber(value) {
this.num = value;
}
} |
private myVar;
|
What I mean is that if the TS compiler can understand the prefix "getVarname" and "setVarname", we would not be tempted to use a private with an underscore, but instead just "varname". The compiler could automatically translate this (in TypeScript):
to this (in JavaScript):
Maybe it's not possible, but that way seems like it would the best of both worlds. |
@ShinDarth what exactly are the inconsistencies? |
See #3137 |
Neither link explains or hints that the _privateProperty with underscore behind a Getter is an execption to the convention not to use underscore property names. |
The first draft of the style-guide uses the
_
-prefix for private fields:Microsoft clearly states that this is not recommended in their coding guidelines:
I personally think that a lot of people will start using TypeScript because of ng2 and it would be really bad to have conflicting guidelines for coding on both sides. I would assume that the Angular 2 style-guide mostly follows TypeScript recommendations.
The text was updated successfully, but these errors were encountered: