Thursday 20 April 2023

Functional Programming - Why Bother?

So I have been discussing functional programming with a colleague at work, specifically concerning streams and collections, and he mentions that most of the times he finds a for-loop easier to read than the whole stream().filter().etc.

Of course, this opinion is very much a subjective point of view and depends on your experience with software design, I think.

So I started thinking where the functional programming style with collections and streams offer advantages.

Inversion of Control

Let us say you need to do something with (possibly) all the items in a list.

The iterative style would be to tell the list to give us (one by one) all its items (in a forloop), that we may do something with them.

The declarative style is to tell the list to do something with its items and give us the result.

This is the inversion of control, the execution of the task switches to another party (the list).

If you think about it, it's quite silly to ask a list to provide us with all its items. The list contains all the items already, it is better if we let the list do the task, to keep execution at the place where it makes the most sense, where the items actually are.

After all, the list is the one who probably knows the most efficient way to wrangle with its contents.

Example

The following example shows both a foreach-loop and a stream implementation.

First a little setup code:

The foreach loop would look as follows:

The stream version would look as follows:

Example

An interesting example can be seen in reference [1], where a stream() actually talks to a database. This way the List or Stream is responsible for proper talking to the database, and we do not get bothered with SQL statements.

If you think about it, then a stream().filter().filter().filter().sort(x).findFirst() can actually be mapped to an SQL statement (select * from x where filter1 and filter2 and filter3 order by x limit 1)

Conclusions

So one of the interesting conclusions we can draw is that instead of imperative programming (telling the computer how to do it), we can switch to declarative programming (telling the computer what to do, and letting the computer figure it out).

And it kind of struck a spark with me.

Things work similarly in real life. You can ask your husband to do the grocery shopping, and you provide him a shoppinglist.

But most of the time, we also add expected "behaviour" (functions). Like, buy the eggs at the greengrocer and not in the supermarket, also get some gas, and go to the superstore first, as it closes the soonest. That sort of thing.

And I see this happening more and more.

If I were to draw this to its inevitable conclusion, I guess we'll end up via Machine Learning all the way to advanced AI, where we tell computers what to do, instead of having to specify HOW to do it.

Naturally, this is going to blow up in our face a couple of times. For example, when the AI decides to do something in an illogical way, but that just means there's an unfortunate gap in the training model.

And it makes me wonder, that, in the future my programming skills will be used, no longer to tell the computer how to do things, no longer to tell the computer what to do, but telling the computer when it does something, what I expect the outcome to be (i.e. writing/changing training models.)

Which is not a problem for me. The way I see it, my profession will not disappear, it will simply change like it has in the past and will in the future.

It's up to me to change with it.

References

[1] Express JPA Queries as Java streams
https://piotrminkowski.com/2021/07/13/express-jpa-queries-as-java-streams/

No comments:

Post a Comment