I'm playing around with a simple port of the Protovis API to Python.
Consider the simple bar chart example, in Javascript:
var vis = new pv.Panel() .width(150) .height(150); vis.add(pv.Bar) .data([1, 1.2, 1.7, 1.5, .7, .3]) .width(20) .height(function(d) d * 80) .bottom(0) .left(function() this.index * 25); vis.render(); I'm debating whether to continue to use this fluent interface style API or use named parameters instead. With named parameters we could write:
vis = pv.Panel(width=150, height=150) vis = vis + pv.Bar(data=[1, 1.2], width=20, height=lambda d: d * 80, bottom=0, left=lambda: self.index * 25) vis.render() Is there a preferred Python style?
Entity Framework Fluent API is used to configure domain classes to override conventions. EF Fluent API is based on a Fluent API design pattern (a.k.a Fluent Interface) where the result is formulated by method chaining. In Entity Framework Core, the ModelBuilder class acts as a Fluent API.
Fluent interfaces help the user of your API to work with an object and it's methods in a more concise manner and can therefore make your API simpler and more desirable to use. Assume a basic Task object, which might look something like this: class Task(object): def name(self, name): self.
Fluent interface, first coined as a term by Martin Fowler, is a very convenient way of communicating with objects in OOP. It makes their facades easier to use and understand. However, it ruins their internal design, making them more difficult to maintain.
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.
My vote is anti-chaining, pro-named-params.
dot-chaining makes for poor code-intellisense since the empirical prototype is just an empty Panel() or Bar(), you can of course pydoc on it, but in this day and age intellisense is available in most IDEs and a great productivity booster.
Chaining makes programatically calling the class much more difficult. It's very nice to be able to pass in a list or dict as *args, **kwargs -- while possible with chaining you'd basically have to support both methods or a bunch of backflips to meta-create the class.
Chaining makes code more difficult to read because inevitably someone will do it all on one line, and wonder why things are all goofed up when they've passed in the same param twice -- you can prevent that but with a named param constructor dup filtering is basically built in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With