cloud-opensource-java icon indicating copy to clipboard operation
cloud-opensource-java copied to clipboard

New JLBP - Libraries should not expose unnecessary implementations in dependency tree.

Open saturnism opened this issue 6 years ago • 4 comments

Libraries should use API interfaces as much as possible. In terms of logging, depend on Logging API interfaces (e.g., slf4j). However, in order for slf4j to print out the log somewhere, it requires a binding (e.g., slf4j-log4j). The binding (implementation) should not be exposed to library consumer via transitive dependency. Bindings/implementations such as this should be set as optional so that it doesn't expose to the user depending on this library.

saturnism avatar Mar 08 '19 02:03 saturnism

I agree. I think it's covered by or close to "[JLBP-1] Minimize dependencies". Especialy this

Use the smallest scope possible https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/library-best-practices/JLBP-1.md

suztomo avatar Mar 08 '19 02:03 suztomo

Can you elaborate on this point? As yet I don't understand why a binding should not be a transitive dependency.

elharo avatar Mar 08 '19 13:03 elharo

these bindings are actual implementations that users will need to choose when they run it. If user's app uses log4j, they will choose log4j - our libraries should not dictate that. If our library has slf4j-log4j binding, it implies using log4j. When multiple of these bindings are present, then slf4j doesn't know which implementation to use, and fails to start.

saturnism avatar Mar 08 '19 17:03 saturnism

Probably worth another example in JLBP-1

elharo avatar Jul 22 '21 14:07 elharo