nus-cs2103-AY2021S2 / forum

10 stars 0 forks source link

Use of wildcard import #301

Open linhns opened 3 years ago

linhns commented 3 years ago

Although the module coding standard states that use of wildcard is not allowed, should this be the case in workplaces as IDEs often automatically convert to wildcard import when there are more than 3 imports from the same library?

tjtanjin commented 3 years ago

Hello! In case this comes in useful, this post here contains instructions on how to force IntelliJ to include each and every import individually :D

ong6 commented 3 years ago

Feels like this is an IntelliJ specific problem, VScode does not do this. + wildcard imports are bad for many other reasons as explained here

JulietTeoh commented 3 years ago

Wildcard imports do not really affect the speed of a program, but in the workplace, not using wildcards can help in writing clean code. This is because if there are 2 classes of the same name, one written by you and one from the wildcard, the compiler will not complied your code and you may become confused as to why there is an error, because you did not know the names of the classes from the wildcard package. Thus, I think it is more advisable to not use wildcard,s even though they are really convenient.

benedictkhoomw commented 3 years ago

Semantically, I think of imports as declarations of the code's dependencies. That is, someone reading the code can glance at it and see what specifically it depends on.

If you use a wildcard, it is either like saying "I depend on everything in this namespace" or "I don't know what I am depending on", The former is bad unless you really are depending on everything in a particular namespace (which may be an indicator of a different problem), and the latter may lead to problems further down the line. If you are not clear on your dependencies, it becomes easier to depend on things you should not be depending on (i.e. scenarios that might violate design principles).