this post was submitted on 13 Apr 2025
577 points (98.0% liked)
Programmer Humor
22440 readers
1942 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
JavaScript was a mistake, but this is one of the few things they did correctly. Implicitly importing everything from a package into the current scope makes it difficult to follow where variables or functions come from, and it's prone to cause problems when two packages export the same identifier.
If you're an absolute masochist, there's always a workaround. Against all best practices, you can use the deprecated with statement. Or, you can
Object.assign()
the packages into the global object like a monster. Or if you're using node, you can use thenode:vm
module to create a new V8 context with its own global object of your choosing.Oh yeah, I was merely complaining about the syntax. Coming from other languages, I interpreted that import statement to mean essentially this:
...and as such, it took me a few seconds to understand what's being aliased by
as operations
.As for importing all symbols of a module, I do think it's more harm than good in non-compiled languages. But when it comes to compiled languages, I'd say it depends on the language.
In Rust, for example, I can easily throw down an inline module with one-way isolation, so that it can transparently access everything in its parent module via
use super::*;
, while the parent module can't access what's in the module (unless it's been markedpub
). That can reduce mental complexity of the code (and is actually used a lot, because unit tests are typically put into such an inline module).It's also useful in Rust, because you can re-export symbols in different modules, so you can break up a file without breaking the imports by throwing a
pub use my_sub_module::*;
into the original module.But yeah, on the flipside, I really wouldn't miss it, if it didn't exist in Java. It was rather even annoying, because the popular IDEs have a rule to replace explicit imports with an asterisk as soon as it reached 5 symbols imported from the same module.
It's not as bad as one might think, because you can't declare top-level functions or variables in Java (everything has to be in a class), but it still sometimes led to those asterisk imports bringing in the wrong class names, so I'd have to manually add the import I wanted underneath them...