What is Fluent Interface?

Asked

Viewed 1,697 times

19

I quickly read some articles about Fluent Interface but I could not clearly understand this pattern.

Definition Wikipedia:

It is an implementation of an API oriented to object that aims to provide the most readable code.

What is Fluent Interface and in what context its use is recommended?

  • The following is an article that will explain this perfectly: http://gc.blog.br/2008/03/um-exemplo-pratico-de-fluent-interface/

  • Either I’m going crazy or I don’t know. I’ve seen this question a thousand times here in the O.R..

  • Only if it’s SO-en, do a test and search here for: Fluent Interface

1 answer

23


It is a way of naming methods to be used in a construction that gives the impression of being writing a flowing, fluid text. He gives up a common, standardized nomenclature to adopt something that makes sense when the method is used trying to simulate a text that is fluent in the human language. It’s a way to make the code more declarative.

To achieve this these methods always return the object it is manipulating, so the next method can be used in sequence. This is called method chaining.

Example:

MailMessage.builder()
    .from("[email protected]")
    .to("[email protected]")
    .subject("oi")
    .message("Oi pra vc")
    .send();

It can eliminate certain repetition of code. But repetition cannot be confused with violation of DRY. The code gets bigger, but it does not hinder canonicality, which is the most important in DRY.

Where it is used

  • The . NET LINQ is a typical case of using fluent interface. The language even allows to use a more fluent way being able to avoid the use of punctuation, parentheses, etc. Although people do not usually use so fluently. In general it would be interesting to create abstractions to use with LINQ, but people make use of more concrete access to collections making it a little less fluent.
  • Is widely used in DDD since it preaches that the code should reflect as closely as possible to the given domain. It practically encourages the use of a DSL. to express what you want and the fluent interface approaches common code than a DSL does, without having to produce specific DSL, with the forgiveness of redundancy.
  • BDD is another use. Especially when using methodologies such as Rspec.
  • In test production is also common. Especially TDD. Tests should not be complex code and they need to be very clear what they are doing. Fluency of text can help.
  • It seems to be very useful when using the Builder pattern and other similar and derived patterns (some people confuse this pattern with the fluid interface.

Critique

It is questionable if it gives legibility to the code. It gives fluency, this is indisputable. Legibility is something else.

Personally, I’ve never seen the win that proponents say. The little I tried to use didn’t seem to be a tool that brings real benefits, at least in the cases I tried. I think to some extent it’s interesting to use, but trying to make real essays is exaggerated. Of course, it’s just an opinion, but just as I can’t substantiate this, I’ve never seen an argument that there’s an advantage. I understand the taste for style and I see what the advantage is, I just don’t see enough value in it.

I have seen people do atrocities with classes and methods just to meet this "requirement". As any tool if misused is better not to use. It is not always easy to implement it.

The programmer begins to have to think a lot about the mechanism and not about the business rule, which is the opposite of what the technique preaches. I believe that the technique is used to facilitate the consumer and not the producer and that there are cases where there is no overhead great in coding. But the problem is never in the basic use, and this is the problem of the adoption of the tools, the person adopts looking at the basics, when he starts to come across more complex problems he suffers more. And as he clings to the tool he doesn’t want to admit that it wasn’t so good.

I’m not saying it’s useless and it’s a totally meaningless fad. But that many people wear just for fashion, this is clear to me. Like everything, you have to know when it’s really advantageous.

An example of what I’m saying is how LINQ is used. I think abstract the "filters" that will be used in them, even to make them more DRY is much more important than making the query fluent. I see more concern with fluency than abstraction, this is generalized.

Questions like that make me wonder how everything is named ? DD seems to bring advantages, of course, but also how one tries to fix what was not broken. It seems to me increasingly clear that everything that is adopted in niches, this occurs for some reason. The problem is when the programmer starts to think that he is in that niche, when he is not.

Alternatives to implementation

Some languages have other ways of doing this with optional and named parameters or preprocessing or object initializers, even estates would help. It may be more interesting than the method chaining technique. At least they avoid atrocities. If it had a form of with well done would avoid many such cases.

Useful examples can be found in How and when to build a valid state object?.

Reference

One of the best references is Page of Martin Fowler who was one of the creators of the term.

  • 4

    Honest answer but that will arouse the anger of the fanboys :)

  • 1

    Coincidentally I read this today, it’s not directly related, but tangential what I’m talking about using what doesn’t add value at all, which creates a burden. https://medium.com/@puppybits/stop-Unit-testing-Everything-e1afb20a5ab3#. 10cx81lqw And of course some people will think I’m saying you don’t need to test anything. Because whoever decides to use a tool without a criterion cannot see the middle ground, the pondering, or you are comrade or enemy of that.

  • 1

    <It is a problem of cultural generation. If you do not follow the fashion blindly, it is considered dinosaur. Only few people realize that all this was built and is only maintained by the few dinosaurs that were not extinct, otherwise it had already been down the drain a long time ago. </Rant>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.