Does the order of the provideA and provideB in the following example matter? Like which method should be written first? If not from functional point of view, does it matter from best practice point of view?
@Singleton
@Provides
@Inject
A provideA(ExampleClass1<B> example1, ExampleClass2 example2) {
return new A(example1, example2);
}
@Provides
@Singleton
@Inject
B provideB(A a) {
return new B(new C(a));
}
What matters is the return-type and the annotations. The rest (the method name, the method location and the method parameters) don't matter.
The return-type is important because it's the injection type and it's what you'll be providing. The annotations are important because they tell you how the provider must be configured.
The order of the method doesn't matter because it's not predictable and you might write methodA
before methodB
, but the compiler might decide that it writes methodB
before methodA
in your .class
file. It's not like in C where you have to (at least) declare the method that you'll use before using them.
In the same vein, the method name and parameters (or even... parameters order) don't matter. Today you may have a provider method called foo(A a, B b)
, tomorrow you may have changed it to bar(X x, Y y)
it'll still be called with the appropriate parameters.
Regarding the best-practice, always keep your code readable. So if you have conventions about putting all the provider methods together and all the helper methods together, so be it. If your team says to minimize the distance between declaration usage, use that. If your code conventions say to order the methods in reversed-alphabetically order, then do so.