Discussion:
[dart-misc] Recommendations for when to use import prefixes, esp wi/ lower-case constants
'Samuel Rawlins' via Dart Misc
2018-07-12 17:02:35 UTC
Permalink
Hey Dartisans (+Bob for Effective Dart comments),

It is common to import packages with many top-level constants and functions
with a prefix, e.g.

import 'dart:math' as math;
import 'package:path/path.dart' as p;

void main() {
print(math.sqrt(2));
print(p.join('foo', 'bar'));
}

And I didn't use to import dart:convert with a prefix, because I use
classes (like JsonDecoder). However, with all constants now lower-case in
Dart 2, I think I want to start importing libraries such as dart:convert
with a prefix, maybe even with show directives.

Another reason is that these lower-case constants clutter up the scope with
things that look like local variables I never declared.

WDUT?

import 'dart:convert';
void main() {
print(ascii.decode([97, 98, 99])); // Wait, where did I declare an
`ascii` variable?
var manyJsonDocuments = getDocs();
for (json in manyJsonDocuments) { // I accidentally forgot `var`, but
now get a weird error about reassigning a constant?
...
}
}

import 'dart:convert' as convert;
void main() {
print(convert.ascii.decode(...));
}

import 'dart:convert' show ascii;
void main() {
print(ascii.decode(...)); // still not super obvious where ascii came
from but... you can scroll up to imports and its in your face.
}

// or both prefixed and show?
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CACshfx1HUQsW_qhBvVQYYpVsahztZOsSk%3DS3hb5FieWxhHFGzA%40mail.gmail.com.
'Bob Nystrom' via Dart Misc
2018-07-12 19:08:10 UTC
Permalink
This is an interesting one.
Post by 'Samuel Rawlins' via Dart Misc
Hey Dartisans (+Bob for Effective Dart comments),
It is common to import packages with many top-level constants and
functions with a prefix, e.g.
import 'dart:math' as math;
import 'package:path/path.dart' as p;
void main() {
print(math.sqrt(2));
print(p.join('foo', 'bar'));
}
Right. The way I think of it is any given library is usually either
designed to be imported with a prefix or not. If it's top-level names are
fairly unique, it's usually intended to be unprefixed. If they are very
short, the author usually intends you to prefix. The path package is a good
example of the latter.
Post by 'Samuel Rawlins' via Dart Misc
And I didn't use to import dart:convert with a prefix, because I use
classes (like JsonDecoder). However, with all constants now lower-case in
Dart 2, I think I want to start importing libraries such as dart:convert
with a prefix, maybe even with show directives.
Another reason is that these lower-case constants clutter up the scope
with things that look like local variables I never declared.
Yeah, "dart:convert" is in a weird limbo. I think with the initial all caps
constants, it made sense to use it without a prefix since those names are
less likely to collide. But it feels a little weird to import unprefixed
top-level names like "json".

There was some concern about this, which is why there are top-level
function jsonDecode() and jsonEncode().

But, yeah, it's a little weird. Personally, I still import it unprefixed
and it seems to work out OK.

– bob
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAF8T8Lx52hrSozj2m%2Ba0AhMGRHL%3DS6TF5L3w4m%3Dp%3DQm3BdRUwg%40mail.gmail.com.
Loading...