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

convention around public / private members #703

Closed
Foxandxss opened this issue Mar 28, 2016 · 13 comments
Closed

convention around public / private members #703

Foxandxss opened this issue Mar 28, 2016 · 13 comments

Comments

@Foxandxss
Copy link

So we should create a convention in when to use public and private and even consider when to use an underscore to prefix a method / attribute.

class Foo {
  public foo: string;
  private bar: number;
  private _fn() { ... }

Personally I vote for using an underscore on private method / attribute as well as the private keyword and leaving public without use.

@Foxandxss
Copy link
Author

I totally agree with that. I am sorry, missed that part.

@johnpapa
Copy link
Owner

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.

@johnpapa johnpapa reopened this Mar 28, 2016
@bampakoa
Copy link

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?

@Foxandxss
Copy link
Author

Now that you mention it, yeah, I prefer underscore for attributes as well. You can have something like:

private _foo: string;

@Input()
set foo(value) { this._foo = value; }

get foo() { return this._foo; }

With more complex setter and getters of course.

@johnpapa
Copy link
Owner

yes, typo

@johnpapa
Copy link
Owner

johnpapa commented Mar 28, 2016

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

@bampakoa
Copy link

Do you think that there will be confusion if underscore is omitted?

@wardbell
Copy link
Contributor

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 something.foo in the debugger, they can't know for certain if that foo is (a) public and undocumented or (b) private. Without adequate notice, they feel free to reference it in their own code. In that moment, the developer incurs a support obligation.

When readers see _foo, they should know it is off limits. If they reference _foo they do so at their own risk. There must be no tears when the developer removes it or changes its behavior.

In our docs and samples private members should begin with ''. Where we have neglected to prefix a private with '', we correct that quickly.

@johnpapa
Copy link
Owner

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.

@Foxandxss
Copy link
Author

All what Ward said.

Also with ng2 we can have a reference like:

<my-dir #myDir>

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"

@davidrensh
Copy link

Agree, no better way to distinguish them. It's an old school for developers. It's not hurt anything except convenience.

@johnpapa
Copy link
Owner

johnpapa commented Sep 5, 2016

Angular 2 issues will now go to the A2 style guide in the angular.io repo

@johnpapa johnpapa closed this as completed Sep 5, 2016
@bartvanderwal
Copy link

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:

Avoid prefixing private properties and methods with an underscore.
- Why? Follows conventional thinking for properties and methods.
- Why? JavaScript lacks a true private property or method.
- Why? TypeScript tooling makes it easy to identify private vs. public properties and methods.

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 TypeScript tooling argument might have swayed John. Seeing as he is so fluent in Gulp, he might have decided that not having working .map files for debugging is just an unworkable situation, that should be remedied directly, instead of fixing it in your code by littering it with underscores :P.

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

No branches or pull requests

6 participants