What are the conventions in Dart about class members' encapsulation and type annotations? -


i new dart language. know more conventions programmers follow while developing in language.

  1. should encapsulate class members do, example in java? whenever create property of class, should make private , provide getters/setters? or there situations when should leave them public? if so, examples of these situations?

  2. in opinion, type annotations such string, int, etc. increase readability of code. serve documentation other developers reading/using code. programmer should not think value of type storing in variable right now. again, situations, require using var keyword when declaring variable?

dmitry.

thank you.

thanks checking out dart!

no need encapsulate class fields. dart creates implicit getters , setters you. if need compute field, can implement getter or setter manually. bonus: doesn't break consumers of api.

example:

class person {   int age; } 

later, want calculate age:

class person {   datetime birthdate;    int age => new datetime.now().difference(birthdate).indays ~/ 365; } 

in both cases, can this:

print(person.age); 

pretty cool! no change in api, , no defensive getters , setters (just add them when need them).

you should use type annotations "surface area" of code. example, use type annotations method , function signatures. cases variable's type obvious, should consider using var, because it's more terse , readable.

for example:

string docoolstuff(int bar) {   var clearlyabool = true;   return 'hello world'; } 

note return type , bar parameter type annotated, clearlyabool uses var because initialize bool.

feel free use type annotations everywhere, it's programmer choice. anecdote: dart2js source code uses type annotations pretty everywhere.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -