I use this sentence as a mantra whenever I am engaged in an API design exercise, which basically starts when writing my first tests
Selecting the next test is an act of design
In this post, I will give you food for thoughts on the design of APIs, using NFluent as an example. I will also provide some details on the genesis of the library.
Thomas and myself had the opportunity to assist to a great live coding session demonstrating refactoring practices, based on the GildedRose kata, performed by David Gageot. At the end of the BBL, Thomas was pretty amazed by the power of Fest Assertions library which was heavily used during the demo. A few weeks later, he committed to build a similar library for the .Net world…
A few months afterward, I joined the project. At that stage it was close to a first stable release; Thomas and myself engaged on a weeks long discussion, both online and at the office, about what should be the entry point of the library. This is a dear topic to me and I engaged in it passionately: I had wholeheartedly joined the vision and selecting the good entry point was key for me.
A library with a sub par API has no future. I do not care how help it can provide, if the APIs get in the way of getting results, I might as well write it myself.
What should be the form of the API? The mission statement was clear: build a fluent assertion library, allowing check to be expressed in a form close to natural language. Some fluent assertions library, FluentAssertions and Should, rely on extension methods applied to the system under test. For example
Good isn’t it? From a fluent perspective, yes. But not that good from a TDD perspective. First of all, the SUT must exist and have a type so you can type the assertion code. Supporting and evangelizing TDD is part of the NFluent’s mission statement, so this approach seems counterproductive.
It is also that it is difficult to quickly identify assertions within the test code, but being fluent to write and fluent to read is a key part of NFluent mission statement.
Finally, there is also a more subtle impact: namespace pollution: every type now has a Should() method, which appears in auto-completion suggestion. This may be kind of distracting.
Definitely, Should() extension method does not fit those bills. So, we kept the static method approach.
There was a long debate on what should be the proper name for the API. We ultimately settled on the now famous Check.That(…):
- Assert was discarded due to naming conflicts with existing unit testing framework
- Should was discarded for being weak
- Verify and others were discarded for varying reasons.
Also, Check is test related in essence while still being assertive.
NFluent has a fluent API, and you build your tests like you would write a sentence.
var heroes = "Batman and Robin";
Thanks to ‘fluentness’, you just have stop start writing your next check and auto-completion does the rest.
To achieve this ‘fluentness’, NFluent relies heavily on extension methods, which turned out to be the best approach to manage polymorphism and rescue across types.
And it also turned out to be a significant pain in the ass, forcing us to use several code generation strategies to maintain an acceptable level of work.
What is important, is that very little of this surface in the API. There would be no excuse for us if we would impose any unintuitive effort to the users, let aside anything worse, such as boilerplate code.
I also put extra effort into the error messages and the overall error message infrastructure, which had undergone several refactoring phases. I wanted to reach an API where building usual error messages was as easy as possible and the more complex ones still possible. This is crucial to ensure a good consistency among messages.
I hope you are happy users of NFluent. By the way, happy or not, we would be very glad of any feedback you can provide, as it helps us moving forward.
The key messages for your API are:
- Put yourself in your users’ shoes. TDD cab go a long way here.
- Take the time to identify and design your key entry points (classes, static methods) before V 1.0. This is worth it.
- Your APIs must drive your design, never let the inner workings of your library surface through your API.
Keep those 3 rules in mind, they will serve you a long time.