Fluency in Chaining Methods – as most refer or understand it as

Do you use fluent interfaces? What? I am fluent in using interfaces … or atleast I think… yeah I am fluent. Now if that was a conversation that made little to no sense, then you are not alone. There are so many words coined for simpler things than expected and that leads to a confusing situation like in this. So first what is a “Fluent Interface?”

which if you were to write in its individual component would look something like

In the first code block we could chain the methods to achieve that in one go.

How to achieve this?

If we look at the pattern, we find that the methods, replacingOccurances , uppercased() and reversed() are all methods/functions available on the String object. and in the case of replacingOccurances and uppercased() the return value is a String and hence we can simply call the methods. The mere fact that it is a primitive type of type String , we kind of miss noticing the obvious pattern there. Now how about we try to apply the same pattern to our custom Object? Like in the previous example, say we are dealing with a Bank Account, and we have the following methods,

Generally when you write functions, the ones like deposit and withdraw return a Void as there is nothing to return really, in some cases you might return a Bool or an Int to indicate the result or failure. In an alternative case you could even return the balance. In a majority of cases these return Void, what we shall do is alter this a little and have them return the object itself in the form of self.

and in the function the last line or the part where we return we return the object as return self . What this allows us to do is chain commands together to write code like

NOTE: There is also a great risk of errors that you might not notice at first glance, if you did kudos to you, if you didn't, then the question for you is what is the variable type for account? if you answer Account because we created an instance of Account, then you are absolutely .... incorrect!
Had the last line not been added, then the variable type of account would have been Account, but with the introduction of the last line, the variable type for account is Bool as the return value of isOverdrawn() is Bool . This is the easiest mistake to make, if you explicitly define the account variable as var account: Account = Account( then the compiler would let you know of the issue as it cannot assign type Bool to type Account.
Or use code like

where you are discarding the value returned from isOverdrawn() and the compiler will keep nagging you about it, if you want to switch it off, then the mere existence of that command in the chain is incorrect and should not feature as pasrt of the chain. It should instead look something like

Use cases in the wild or Apps

Fluent interfaces can be used everywhere where you are not expecting a return value of a particular type or if you are modifying variables inplace. The first example that comes to mind is Animation and Graphics, where you would want to write code like

or with graphics where you want to draw something

That's what Fluent Interfaces are and why people call them as Chaining Methods, while they are easy to use they can also be a bit tricky to debug in case you are unable to spot the obvious.

Views All Time
Views All Time
118
Views Today
Views Today
1
Posted in Article, Basics, Tip, Tutorial.