Fluent APIs and types

Fluent APIs and types

I'd like to borrow an example from Mario Fusco to illustrate how to take advantage of types to make programming safer and more fun. Consider this Java class:

IDE screenshot of questionable code
Questionable code

The class uses the return this trick to enable method call chaining. However, there is a temporal dependency between the methods in this class, and if I call them out of order, I will get an exception at runtime. At runtime, possibly throwing an exception in the face of a user!

The nice thing is that we can use types to prevent this error at programming time. That is:

IDE screenshot of improved code
Better code

Now if you start with a HungryCat, the only option you have is to call method catchBird(...) , and the IDE itself will guide you towards invoking the correct method:

Screenshot of the IDE showing how the autocomplete guides you to invoke the only method available
Autocomplete to the rescue

And this is how you do a fluent API: at every step, the IDE guides us to choose among the available methods, and the compiler prevents us to invoke methods out of order.

IDE screenshot showing example usage of the fluent code

The original example from Mario Fusco is just 2 slides out of this slideshare presentation, that contains a lot of other good stuff.

That's all folks!



Uberto Barbini

Passionate Programmer, AI Expert, Hands-on Architect, Public Speaker, Author, Android-Kotlin GDE

1 年

It's easier if you use a sealed interface Cat (or CatState) to be sure to get all possible kind of cats. Also you can map the different state changes in an CatEvent class... and you know where this will end. :)

Mario Pio Gioiosa

Senior Software Engineer

1 年

Thanks Matteo for the article. In the latests months I've been using Rust and this article made me immediately think to the typestate pattern. It's a very convenient way, available in Rust, to do the exact same thing you did. The difference is that the "state" goes in the type parameter. Something like this..

  • 该图片无替代文字
Christian Nastasi

Technical Coach / Divulgatore Informatico / Founder @Mirai Training @DevDojoIT

1 年

Very nice point of view, thanks for sharing

回复
Mirco Poretti

Senior Software Engineer | Tech enthusiast

1 年

I love this example, thanks Matteo ??

Marco Consolaro

Consulting, Coaching & Training for Modern Software Engineering - Innovative Agile Software Technical Training Coach & Trainer| Co-Autor “Agile Technical Practices Distilled” Award Winning Book | Co-founder Alcor Academy

1 年

Very nice! This is an example of Connascence of Execution Order. Another way to solve it, without breaking the Cat in different classes, could be leveraging Interface Segregation. The Cat would implement all the interfaces, but using a Factory Method you can expose it as the interface with the right behaviour in the right time!

要查看或添加评论,请登录

Matteo Vaccari的更多文章

  • Publish metrics from a Go web app

    Publish metrics from a Go web app

    I keep being delighted by how easy and fun it is to write web applications in Go. Today I wanted to learn how to…

  • A convenient alternative to SPA

    A convenient alternative to SPA

    In 2022 the Thoughtworks Radar warned against adopting SPA By Default, meaning that building a JavaScript-based Single…

    7 条评论
  • TDD Coaching and training in 2023

    TDD Coaching and training in 2023

    During the awesome Avanscoperta retreat in Riccione I hosted a session on TDD training and coaching. Here's a recap.

    3 条评论
  • There is no advanced TDD

    There is no advanced TDD

    There's not really such as thing as "advanced TDD", other than practising TDD more diligently, writes Esko Luontola. I…

    6 条评论
  • Just the facts

    Just the facts

    La serie di blog di Sara Pellegrini su Kill Aggregate solleva dei punti importanti sulla difficoltà di usare il…

    7 条评论
  • My first experiment with Copilot

    My first experiment with Copilot

    I'm running an experiment of writing a small hobby project with Github Copilot; it's almost a week now and I wrote, or…

    5 条评论

社区洞察

其他会员也浏览了