Do partial imports bring advantages to the application?

Asked

Viewed 102 times

7

Some languages have as a resource the import of parts of the library used.

Using Dart as an example we have two types of partial Imports showand hide .

import 'package:google_maps/google_maps.dart' show LatLng;

Only imports the library’s Latlng class.

import 'package:google_maps/google_maps.dart' hide LatLng;

Imports all library classes except Latlng.

In this context, importing only the classes that will be used from a library brings some advantage to the application?

2 answers

4


For the application itself does not bring advantages, especially performance, but for the readability of your code depends, you may consider that it has yes or no advantage.

When it matters only that you will use the namespace It gets cleaner and doesn’t run the risk of something conflicting with something else that would be somewhere else. At the same time an IDE won’t show you some things you could use if it mattered everything and helped you use something you hadn’t thought about.

Obviously hiding just one guy helps less, but it might just be what you really want, you might know that only that would conflict.

And if you have to import many types, it can get even more confusing, then the biggest advantage is when you have to import only one type from that library.

It is possible that in some situations there is some minimal gain in compile time, but nothing that matters is very minimal, just for the record, it is not that large portions of code will no longer be compiled. The compilation time gain I talk about is because it is easier to manage the symbols when there are less of them, so it is minimal.

The Tree-Shaking helps the Dart application have advantages because there will be much less load and at least in some aspect there will be performance gain, but not because of the import. The technique of Tree-Shaking has nothing to do with the import technique spoken of here, including the link initial of the other answer talks about it, just read carefully. The Tree-Shaking will cause something not to be compiled as long as it is not used by the code, but the fact that it does not import something does not mean that there is no use in the application.

3

Looking from the performance point of view, use the show/hidedoes not imply necessarily in an improvement.

That is, although you tell the compiler that your code will only use one specific class, it may be that this imported class makes use internally of other parts of the library. In the end, everything will be compiled.

Consequently, in languages using Tree-Shaking, which is the case of Dart used as an example in your question, this does not entail difference in build time or in the size of the generated application.

One of the aids that such reserved words bring is the reduction of name conflicts: If you only care about X, and not Y and Z, you can name your Y and Z classes without any problem. For the same reason, your IDE will not suggest or attempt to self-complete things that do not concern your interest.

Another example of help for the programmer is to let clear your intentions when making an import. Whoever reads that import will later know that that code uses only such a class, and will already have a better understanding of its use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.