Unless we change the compiler, Java misses the import X as Y
syntax, which would be useful in cases like mine: In this very moment I'm working on a project having multiple classes with the same name, but belonging to different packages.
I would like to have something like
import com.very.long.prefix.bar.Foo as BarFoo
import org.other.very.long.prefix.baz.Foo as BazFoo
class X {
BarFoo a;
BazFoo b;
...
}
Instead I finish in having something like
class X {
com.very.long.prefix.bar.Foo a;
org.other.very.long.prefix.baz.Foo b;
...
}
Here it seems pretty harmful, but on my specific case I need to use horizontal scrolling in order to browse my source code, and that concours in making worse a program which is already a mess.
In your experience, what is the best practices in this case?
I feel your pain, whichever solution you use, the very fact that there are two classes with the same name is confusing enough.
There are several solutions workarounds:
BarFoo
and BazFoo
which do nothing other than extend their respective Foo
classes thus providing them with their own names. You can even define these as inner classes. Example:private BarFoo extends com.very.long.prefix.bar.Foo{
//nothing, except possibly constructor wrappers
}
private BazFoo extends com.very.long.prefix.bar.Foo{
//nothing, except possibly constructor wrappers
}
class X {
BarFoo a;
BazFoo b;
//...
}
There are some drawbacks, though:
getClass
.You could solve these drawbacks by wrapping the Foo classes rather than extending them, e.g.:
private BarFoo {
public com.very.long.prefix.bar.Foo realFoo;
}
private BazFoo extends com.very.long.prefix.bar.Foo{
public com.very.long.prefix.baz.Foo realFoo;
}
class X {
BarFoo a;
BazFoo b;
//now if you need to pass them
someMethodThatTakesBazFoo(b.realFoo);
}
Choose the simplest solution and good luck with it!
The best practice is to refactor the code.
Either the classes should not have the same name, because it's normal to use them both in the same classes, and thus choosing the same name for both is not a wise choice. So one of them at least should be renamed.
Or it's not normal to use them both in the same classes because they belong to completely different abstraction levels (like database access code and UI code, for example), and code should be refactored to use each class where it must be used and not elsewhere.
What I've typically done in these situations is to import the most commonly used Foo in my class, and then fully-qualify the other one:
import com.very.long.prefix.bar.Foo
class X {
Foo a;
org.other.very.long.prefix.baz.Foo b;
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With