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
convention around public / private members #703
Comments
I totally agree with that. I am sorry, missed that part. |
Cool. And I think your issue has some perspective that is important to add to the guide, so i will reopen it. :) I'd like to add the part about naming of these to the guide. If you asked, then so will someone else. |
What's the point of using underscore in private function names and not members as well? Isn't it also private? Or, does underscore give a different meaning to the function itself? |
Now that you mention it, yeah, I prefer underscore for attributes as well. You can have something like:
With more complex setter and getters of course. |
yes, typo |
I don't like underscores personally ... but since there is no real private nor public in javascript, it is very helpful to have a way to differentiate them. So I use them UPDATE: i was referring to ES5 ... with Typescript I don;t use underscore |
Do you think that there will be confusion if underscore is omitted? |
TypeScript/JavaScript doesn't have true private variables and the use of classes means that we have even less of an ability to hide variables than in pre-class JavaScript. We have only the '_' convention to save us. The primary value of the '_' convention is that it shouts "Do not touch!". When debugging the JavaScript it's all we have to go on. When people see When readers see In our docs and samples private members should begin with ''. Where we have neglected to prefix a private with '', we correct that quickly. |
Ward's assumption is that we would be debugging the javascript .... to some that may seem odd, but I see it as a great backup plan if sourcemaps do not work so hot or if we just want a clearer understanding of what is happening. It is also a great visual indicator of how to use it. |
All what Ward said. Also with ng2 we can have a reference like:
and access the attributes inside. If one of them has an underscore, it is signal of "this could be broken tomorrow, don't do it" |
Agree, no better way to distinguish them. It's an old school for developers. It's not hurt anything except convenience. |
Angular 2 issues will now go to the A2 style guide in the angular.io repo |
Please note the current Angular codeStyle (rule 03-04) has been changed compared to @johnpapa's suggestion here and now advises NOT to using an underscore for private variables:
So this is the reverse of what is suggested in this issue. When I first read this issue this I thought I had found a difference, but the updated/flipped rule IS in line with the typescript coding guidelines (rule nr 6 for names). I think the |
So we should create a convention in when to use
public
andprivate
and even consider when to use an underscore to prefix a method / attribute.Personally I vote for using an underscore on private method / attribute as well as the
private
keyword and leavingpublic
without use.The text was updated successfully, but these errors were encountered: