Principles for Successful Package Management
If SOLID can help you to build cohesive, loosely coupled components which do one thing only and do that well, why can't we do the same thing on the package level?
Join the DZone community and get the full member experience.Join For Free
a couple of months ago i shared some tips and tricks to help you prevent ending up in nuget dependency hell. as a big fan of the solid principles, i've always wondered why nobody thought of applying these principles on the package level. if solid can help you to build cohesive, loosely coupled components which do one thing only and do that well, why can't we do the same thing on the package level?
as it happens, my colleague, jonne , enthusiastically referred me to the book principles of package design by matthias noback . it's available from leanpub and does exactly that, offering a couple of well-named guidelines inspired by solid that will help you design better nuget, npm, or whatever your package management solution of choice uses.
the first half of the 268 pages provide an excellent refresh of the solid principles. he even does a decent job of explaining the inversion of control principle (although i would still refer to the original to really grasp that often misunderstood principle). after that, he carefully dives into the subtleties of cohesion as a guiding principle before he commences on the actual package design principles. the examples are all in php (yeah, really), but the author clearly explains how these would apply to other platforms. note that this post is mostly an exercise for me to see if i got the principles right, so i would highly recommend buying the .epub, .mobi, or pdf from leanpub. it's only 25 usd and well worth your money.
let's briefly discuss the actual principles.
the release and reuse equivalency principle
imho, the first principle has a rather peculiar name. considering its purpose, it could have been called the ship a great package principle. the gist of this principle is that you should not ship a package if you don't have the infrastructure in place to properly support that. this means that the package should follow some kind of clear (semantic) versioning strategy, have proper documentation and pro release notes, have a well-defined license, and be covered by unit tests. the book goes to great lengths to help you with techniques and guidance on ensuring backward compatibility. considering the recentness of the book and the fact it mentions semantic versioning, i would have expected some coverage of gitflow and githubflow . nonetheless, most of the stuff mentioned here should be obvious, but you'll be surprised how often i run into a unmaintainable and undocumented package.
the common reuse principle
the purpose of the second principle is much clearer. it states that classes and interfaces that are almost always used together should be packaged together. consequently, classes and interfaces that don't meet that criteria don't have a place in that package. this has a couple of implications. users of your package shouldn't need to take the entire package if they just need a couple of classes. even worse, if they use a subset of the package's contents, there must not be a need to get confronted with additional package dependencies that have nothing to do with the original package. if that specific package has a dependency, then it's an explicit dependency. a nice side effect of this principle is that it makes packages open for extension and closed for modification.
i've seen packages that don't seem to have any dependencies until you use certain classes that employ dynamic loading. nhibernate is a clear violator of this principle in contrast to the well-defined purpose of the owin nuget package. my own open-source library, fluent assertions , also seems to comply. when a contributor proposed to build a json extension to my library, i offered to take in the code and ship the two nuget packages from the same repository. so, if somebody doesn't care about json, it can use the core package only without any unexpected dependencies on newtonsoft.json.
the common closure principle
the third principle is another one that needs examples to really grasp its meaning. even the definition doesn't help that much:
the classes in a package should be closed against the same kinds of changes. a change that affects a package affects all the classes in that package.
according to many examples in the book, the idea is that packages should not require changes (and thus a new release) for unrelated changes. any change should affect the smallest number of packages possible (preferably, only one). alternatively, a change to a particular package is very likely to affect all classes in that package. if it only affects a small portion of the package or it affects more than one package, then chances are, you have your boundaries wrong. applying this principle might help you decide which class belongs in which package. reflecting on fluent assertions again made me realize that even though i managed to follow the common reuse principle, i can't release the core and json packages independently. a fix in the json package means that i also need to release the core package.
the acyclic dependencies principle
for once, the fourth principle discussed in this book is well described by its definition:
the dependency structure between packages must be a directed acyclic graph, that is, there must be no cycles in the dependency structure.
in other words, your package should not depend on a package which dependencies would eventually result in cyclic dependency. at first thought, this looks like an open door. of course you don't want to have a dependency like that! however, that cyclic dependency might not be visible at all. maybe your dependency depends on something else that ultimately depends on a package that is hidden in the obscurity of all the other indirect dependencies. in such case, the only way to detect that, is to carefully analyze each dependency and create a visual dependency graph.
another type of dependencies that the book doesn’t really cover are diamond dependencies (named for the visual dependency graph). within the .net realm, this is a quite a common thing. just consider the enormous amount of nuget packages that depend on newtonsoft's json.net. so, for any non-trivial package, it's quite likely that more than one dependency eventually depends on that infamous json library. now consider what happens if those dependencies depend on different versions.
the book offers a couple of in-depth approaches and solutions to get yourself out of this mess. extracting an adapter or mediator interface to hide an external dependency behind is one. using inversion-of-control so that your packages only depend on abstract constructs is another. since the book is written by a php developer, it's no surprise that it doesn't talk about ilmerge or its open-source alternative ilrepack . both are solutions that will merge an external .net library into the main dll of your own package. this essentially allows you to treat that dependency as internal code without any visible or invisible dll dependencies.
an alternative to merging your .net libraries is to use a source-only nuget package. this increasingly popular technique allows you to take a dependency on a nuget package that only contains, surprise, source code that is compiled into your main package. liblog , tinyioc and even my own caching library fluidcaching uses this approach. it greatly reduces the dependency chain of your package.
the stable dependencies principle
the name of the principle is quite self-explanatory, but the definition is even clearer.
the dependencies between packages in a design should be in the direction of the stability of the packages. a package should only depend upon packages that are more stable than it is.
in other words, you need to make sure you only depend on stable packages. the more stable your dependency, the more stable your package is going to look to your consumers. determining whether a package is stable or not isn't exact science. you need to do a bit of digging for that. for instance, try to figure out how often a dependency introduced a breaking change. if they did, did they use semantic versioning to make that clear? how many other public packages depend on that package? the more dependents, the higher the chance that the package owners will try to honor the existing api contracts. and how many dependencies does that package have? the more dependencies, the higher the chance some of those dependencies introduce breaking changes or instability.
finally, check out its code and judge how well that package follow the principles mentioned in this post? the book doesn't mention this, but my personal rule-of-thumb to decide on whether i will use a package as a dependency is to consider the circumstances when the main author abandons the project. the code should either be good enough for me to maintain it myself or the project should be backed by a large group of people that can ensure continuity.
the stable abstractions principle
now, if you understand (and agree with) the stable dependencies principle, you'll most definitely understand and agree with the stable abstractions principle. after all, what's more stable: an interface, an abstract type, or a concrete implementation? an interface does not have any behavior that can change, so it is the most stable type that you can depend on. that's why a well-designed library often uses interfaces to connect many components together and quite often provides you would with an interface-only package. for the same reason, the inversion of control principle tries to nudge you in the same direction. in fact, in the .net world even interfaces are being frowned on and are being replaced with old-fashioned delegate types. these represent a very tiny and very focused interface, so it doesn't get any more stable than that. because of their compatibility with c#'s lambda statements you don't even need to use a mocking library.
so, what about you?
the names are not always that catchy and easy to remember, mostly because they use the same wording, but the underlying philosophy makes a lot of sense to me. i've already started to re-evaluate the design decisions of my projects. the only thing i was hoping to read more about is the explicit consequence of building a component or package as a library versus building it as a framework. this is something that heavily influences the way i'm building liquidprojections , my next open-source project.
so what do you think? do you see merits in these principles? do they feel as helpful as the original solid principles? i've love to know what you think by commenting below. oh, and follow me at @ddoomen to get regular updates on my everlasting quest for better designs.
Published at DZone with permission of Dennis Doomen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.