Imports: fully qualified or relative paths?
I'm curious what everyone thinks about this. Programming languages generally have a way to import code from another file/module. Some languages have some kind of abstract namespacing scheme that maps to a location in the file system (e.g., Java), whereas other languages allow you to provide a literal path to the file/module (e.g., JavaScript), though sometimes the file extension is omitted. Languages in the former category typically require fully qualified namespaces, whereas languages in the latter category typically allow for relative paths. And of course some languages deviate from this and/or implement some kind of hybrid system.
Another consideration is that some languages don't have a notion of "project" or "root" directory, so fully qualified paths don't make sense. Those languages tend to use relative paths.
What are the tradeoffs here? What are you doing for your language project(s)?
Posted Mar 17, 2021, 8:37 PM UTC
With JavaScript, imports look like either
import "../../blah/foo"orimport "mylib/blah/foo"depending on whetherblah/foois in a different package or not.I prefer fully qualified paths so that imports dont have to change when code is moved to or from a different package.
Additionally, when a file is moved with absolute imports, the imports inside it are still valid and only the importers have to change. With relative imports, the imports inside the file also have to change.
I'd use both types: relative if you're writing different modules of a single program, where it's unlikely that files ever get moved around much. More obvious that it's from your program.
Absolute if you're using stuff that might be replaced with external libs.