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

Public Private and the Underscore - A big issue #2461

Closed
DartBot opened this issue Apr 5, 2012 · 8 comments
Closed

Public Private and the Underscore - A big issue #2461

DartBot opened this issue Apr 5, 2012 · 8 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue

Comments

@DartBot
Copy link

DartBot commented Apr 5, 2012

This issue was originally filed by rfab...@planalytics.com


The "_" issue again.

This may be the number one issue slowing down Dart popularity.
After 20 years coding in C,C++,Java,Javascript,Perl, I am
surprised this convention won the battle for public/private syntax!

Please consider a 10 day vote to see what the "common people" want.
Make some examples to demonstrate a fair comparison.

Suggestions:
   Everything private by default with class based privacy.
   Use @­ for anything public (properties,methods,class)
   Use @­ in a suffix style like unix symlinks (name@) (unix ls)
   A "struct" would be classname@ so all fields are public.
   (Raw values are less important for the important character "@")

   PS: Use more lowercase. "foreach" instead of "forEach".
   PS2: Favor composition more than subclassing (GoF/Artima).
      ("Allan Holub", Fragile Base class, Google noop lang)
      (build libraries with composition to teach how to do it)
   PS3: Use more terseness like C/Perl/Python/Ruby "fopen", etc
   PS4: Get good terse Perl like regex working better.
       (Help Java coders replace scripts first, Java later)
   PS5: Use a Ruby like constructor syntax, "initialize".

I know there are reasons for "", making things explicit, performance,
but first impressions to Java Developers is really important also.
Java Developers can handle something different and creative,
but not "
" for private.

jwait.com

@DartBot
Copy link
Author

DartBot commented Apr 10, 2012

This comment was originally written by rfab...@planalytics.com


This issue is a new perspective on a previous issue that was closed (issue #520).

http://code.google.com/p/dart/issues/detail?id=520

That's why I propose to add a type modifier like 'private' or 'hide'.
I think both practices can be incorporated in the language and
everybody would be happy.
One developer could write:
private int myVar = 10;
myVar = myVar + 1;
while another developer prefers:
int _myVar = 10;
_myVar = _myVar + 1;

Personally I'm in favour for the modifier approach.
And I think underscores are a bad idea.
It is clear a lot of people like the underscore approach
(including the Dart devs) and I respect that.
But I think there's a place for both styles in the language.


The new perspective is this:
Do a "pepsi -vs- coke" taste test and see what people like:

There would be three choices:

  1. The current Dart way with "_" for private.
  2. The Java way with a type modifier, "private" (assuming default public)
        or "public" (assuming default private).
        This would make Dart instantly more popular with the Java community.
  3. The unix-symlink-like way proposed above (using '@' for public).
        This is the most radical, but possibly the most interesting!
        (This will surely ruffle some feathers, but could create some excitement also)
        Everything would be private within the class by default.
        Then one could add a "@" postfix to fields, methods or the class to make it
        public accessible (e.g. foo@). One could easily make the whole class
        public by default with just one character added to the class (classname@).

Take the "pepsi, coke, and alternative brand" to the public and do a "taste test". (Include a fair set of examples for each brand).

NOTE: When you building a larger code base you will notice almost everything is
prefixed with an underscore. (e.g. dart/corelib/src/implementation/hash_map_set.dart)
This is very difficult for the standard Java developer to accept.
When building larger code bases one will probably spend 80% of the time writing private implementation code, and the underscore will be annoying to most people.
To do a fair test, this kind of development must be well represented.

Let the people decide.

@kasperl
Copy link

kasperl commented Apr 11, 2012

Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Apr 12, 2012

This is indeed a big issue. Privacy is pretty fundamental to the language and we are committed to the core elements of the Dart design. This is not something you can mix and match: that is, supporting multiple approaches carries considerable risk of a train wreck. Different approaches require different runtime support and introduce different styles and idioms.

We work hard to accommodate mainstream programmer preferences, but in many cases, the mainstream forks. This is one of those cases, and one makes a choice.

The issue has come up before, and it is too fundamental to change.


Set owner to @gbracha.
Added WontFix label.

@DartBot DartBot added Type-Defect area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Apr 12, 2012
@krupalshah
Copy link

I can't understand why this issue has been closed. Indeed, we need a new visibility model at least better than writing underscores. As said

This may be the number one issue slowing down Dart popularity.

Yes, Indeed. I would rather like to learn swift. At least they did right choice with public, private and internal. From the specification :

This has the advantage that both humans and machines can
recognize access to private declarations at the point of use without knowledge of
the context from which the declaration is derived.

Maybe it is good for machines, but for humans; writing underscore every time is tedious and far from 'readable'. All we need a new visibility model in 2.0 .

@zoechi
Copy link
Contributor

zoechi commented Nov 11, 2015

It's closed because there is nothing to do.
x and _x are different things. How would you differentiate between access to a public or private field/function?

int _someVar;
int get someVar() => _someVar;
int set someVar(int value) {
  if(value < 0) throw 'only positive values are allowed';
  _someVar = value;
}

@kevmoo kevmoo added closed-not-planned Closed as we don't intend to take action on the reported issue and removed resolution-wont_fix labels Mar 1, 2016
@abreu-dev
Copy link

abreu-dev commented Mar 9, 2017

Coming from Java, C#, Swift, its very strange to see underscores everywhere to define the whole privacy model of a language. It is not natural for humans reading words prefixed with underscores. It makes the code difficult to read.

Swift has a great way to do it: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html

@zoechi Why the setter have a return type if it is basically a void function?

int set someVar(int value) {
  if(value < 0) throw 'only positive values are allowed';
  _someVar = value;
}

@zoechi Why the need to differentiate someVar from _someVar? Why not have an implicit/internal field with the same of the getter/setter?

get int someVar => someVar + 10;
set someVar(int value) {
  if(value < 0) throw 'only positive values are allowed';
  someVar = value; // this is a special case where the real setter is not called
}

@zoechi
Copy link
Contributor

zoechi commented Mar 9, 2017

@andreabreu-me You don't need a return type for setters, actually you shouldn't add one

I think the whole underline for private discussion is about 3+ years too late.
It doesn't bother me and it's pointless because it won't change.
@lrhn posted a great explanation about why there are good reasons to do this in Dart (don't remember where exactly though).

@milne-dev
Copy link

Underscores are ugly, please change.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue
Projects
None yet
Development

No branches or pull requests

8 participants