BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage Podcasts Do Functional Programming and Agents Play Well Together Towards Improved Code Quality and Testing?

Do Functional Programming and Agents Play Well Together Towards Improved Code Quality and Testing?

In this episode, Uberto Barbini discusses how clean functional programming can help AI Agents to generate better code. He explains how immutable data and pure functions enhance code quality, maintainability, and testability.

He emphasises insights on navigating software development in the age of AI agents, emphasising the importance of strong modularity, multi-layered testing strategies, and effective developer mentorship.

Key Takeaways

  • Functional programming emphasises immutable data and pure functions, making code explicit, easier to compose, and less prone to side effects.
  • Functional programming makes property-based testing natural, enabling automated generation of test inputs to verify invariants.
  • AI agents thrive on modular codebases with well-defined interfaces and minimal contextual overhead, not complex abstraction hierarchies.
  • A "Swiss cheese" safety model: combining linters, property testing, mutation testing, and multi-agent code reviews is essential for quality assurance with AI.
  • Active engagement and critical oversight from developers remain critical; junior developers must be mentored to use AI as a learning tool rather than a shortcut.

Transcript

Olimpiu Pop: Hello, everybody. I'm Olimpiu Pop, an InfoQ editor, and I have in front of me a long-running software developer that moved from multiple programming languages. And lately, he got quite friendly with Functional and he's keeping close to home even in the age of AI agents, and that's functional programming. Uberto Barbini is our guest today. He will speak to us how things changed since he wrote his book From Objects to Functions and how he embraces programming with AI agents. But without further ado, Uberto, please introduce yourself.

Uberto Barbini: Hi, Olimpiu. Thanks for inviting me. I'm Uberto Barbini. I started programming many years ago when there was those things called a home computer. I work for many years with Java and JVM-related languages, but in the last three years we are using mostly Python. I don't think languages count that much. Instead, I think more than the language is the how they use the language that really matters. And talking about your question, more or less 10 years ago, I switched to functional programming after so many years of object-oriented. And I really like what I saw and I started using functional programming more and more in Java and Kotlin, and then was my book.

Why Transition to Functional Programming? [02:05]

Olimpiu Pop: Thank you. We'll not go into the history of functional programming or object programming, but as you mentioned, you made the shift and you never gone back. I mean, yes, we do use objects, but I know that from our conversations, it's not the way how it used to be. What brought you to functional programming? What would be the main benefits that you see into writing functional code?

Uberto Barbini: My main goal in using functional programming or in general in programming is to get the result home. So we wanted to deliver something devalue for the customer in the best way, less time as possible. So the switch to functional programming was always driven by this kind of efficiency. The main idea behind the functional programming is that your data are immutable and your function are pure. So basically they only use the arguments and the only thing that they produce is their result and making everything very explicit. So you cannot have an exception or write in the file system without somehow declaring in the function type. And this is a kind of super explicit way to work, make it very easy to compose function because you can easily understand what the function will be. It's what they need and what they will produce. So it will be very easy to create a functionality composing a small function and be pretty sure of what will impact.

This may be a bit harder than a normal object-oriented way to write code, but I find much faster than to debug a code or to modify the code once it's written.

Olimpiu Pop: As you mentioned, it's important to know what happens and the side effects and everything. And for a long period of time, there were ways of doing that in object-oriented programming languages. They had all kind of things, mock, stubs, spies, you name it, and that were just trying to figure out what happens. In a functional manner, it should be easier than that. You have inputs, you have the functions that are doing the computation, and then you have output. And that should be quite linear. How did the way how you're writing tests, how you're testing your work evolved from that point of view?

Uberto Barbini: Yes, tests are definitely much, much easier using functional programming because as you said, each class is immutable. The function are very clear what they need and what they will produce. And if you just needed to simulate a function that give us some other result, you just needed to create a small function in your test and passing that function as input, for example. This not only makes everything easier to test, but also open a new way of testing. For example, there is this style that is called property testing where you let the test framework itself to create the value. So you just specify the property that basically remains invariant. So you say, for example, you can generate an object at random generated the JSON from that object, and then from that JSON you generated the object again and the new object must be identical to the original object.

This way of doing the test where basically you just define something and the test will run thousand of times generating a lot of possible values and verify that are still all. It's called property testing something that only functional programming basically allow you to use.

Olimpiu Pop: Okay.

Uberto Barbini: When I started functional programming 10 years ago, it was still something quite experimental like Haskell or Lisp, but it was still considered maybe promising but very slow, not really useful for real programming. And Java was instead very popular with this idea of JavaBeans that was just object with everything that was mutable. So as far from functional programming as you can go, so it was a bit of unconventional choice for me to approach that style. But then nowadays with all the main languages, Java start introducing Lambda and the streams and now Valhalla, Kotlin is already much more functional-friendly than Java. Rust is using the Result instead of using exceptions. So the whole programming scenario is moving in that direction.

Olimpiu Pop: Well, obviously as we saw in the last couple of days, it's quite important to have a lot of safety in your language. And you mentioned Rust and you mentioned the other programming languages and also the supply chain is getting quite important here in Europe especially with the Cyber Resilience Act. But around the world, people are changing towards more safety and obviously having the ability of understanding your code is quite important. Or is it? What's your feeling? Now we're moving one abstraction even further away. We spoke about having abstraction at the code level. Now each developer is a shepherd or I don't know how to call it an orchestrator for a fleet of agents. Is code still important these days? What's your feeling?

Uberto Barbini: I really think that even with the AI agent, code quality is still important. And the reason is that there are even study papers and also my experience is that with a good codebase, basically the agent are much more efficient. They produce better result more consistently than agent having to work on spaghetti-code, messed-up codebase. It's also true that not everything that we consider good code is really matters to agents, but having a good modularity and kind of a clear defined function, it really helps the agents. And the reason should be quite easy because I mean the agent use this concept of context.

So the less things they needed to keep in mind when they work on the code, the better they can use their strength. Functional programming works very well with this because since every function is pure only work on its argument, it's much easier for the agent to check that they can change or they cannot change and what needed to be changed rather than standard object-oriented kind of framework where everything happen by magic conventions or singular analysis and stuff like that.

Modularization & Interface Design for AI Agents [08:54]

Olimpiu Pop: So let me see if I can unwind what you mentioned. You said that when working with agents, having modular code and clear interfaces, it's easier for the agents to write the code. So would it be a fair statement to say that it's just putting the stick in the ground? You build something good, proper, and then you have, let's say, I don't know, a module or a class for those that are still using object-oriented programming, you get to that point, you wrap it up in the right place and you have a clean interface and then it would be easier to build on top of that. And with each step moving forward, it'll be a lot easier and the code is growing up nicer. Is that a fair statement?

Uberto Barbini: Absolutely. Modularization is really important. You mentioned SOLID principle and that's quite interesting because not all the SOLID principle actually really matters for the agents, but this kind of functional modular decomposition really, really helps. Agents, for example, are not great at the abstractions. So having something like interface of interfaces or complicated inheritance is really not helping much. But for clear, well-defined classes and modules, the agent can be consistent on their quality of the job because with the very complicated code, it's a lot of hit-and-miss. Maybe they do one things right and then the next things they mess up completely the codebase. And that's because of the context that can be problematic, it can be easily sidetracked by accidental complexity and stuff like that.

Olimpiu Pop: So now the challenge is, as I can anticipate, up to now you had a team of people and there are multiple ways of working together. One of them was, okay, we are just dividing and conquering and everybody takes a task and then everybody's building it. Now you have a whole array of agents. How do you bring together the previous experience into this new world?

Uberto Barbini: Basically I don't use ID anymore and I rarely look at the coder and instead I rely much more on agent checking other agents. But there is also always a level of coordination between the team because I think Kent Beck at some point said that with the AI, 90% of my skill are completely useless, but the remaining 10% is 10 times more relevant. So it's quite clear for whoever tried that if you are a good developer, I mean someone with the experience of systems of how to design, I mean you can really do 10 times faster or 10 times more things. But for people that don't have this kind of experience, they often get sidetracked by AI or cannot produce result consistently.

Olimpiu Pop: Well, it was challenging with people. Now it's challenging with agents as well to get testing in place. But it's different type of challenge because initially, for instance, in my experience, I'm asking for code coverage and then I get a maybe, and then a couple of months after that I get excuses why that's not possible. With agents, you don't get excuses, but you get the green tests. If you look into the tests, they're not exactly those that you would write and they seem to be very, very focused on their purpose on their goal to develop something. And if they're not able to get ahead of the pack, they are just cutting the corners. How do you move that kind of level of experience into the harness? Because one of my main issues with the way how we're using LLMs is the fact that you have an expert in front of you.

You have somebody that if it'll be to just compare it with the human, somebody that has at least 100 PhDs, knows everything about everything. And then you just say now you'll be a Python developer that is very good in financial software writing. And it feels that the points, a switch is being flipped and then they're just going left or right from that point of view. They're not remaining on that straight line where you want. Or then you have somebody that hallucinates at points, but you ask the same person to just validate their work. But how can you be sure about it? So these are the challenges and that's my... Because after 20 years of writing code and numerical stuff where one is equal to one, it's hard to bend those rules and understand it and get into a statistical approach of it where I know 60% of the cases initially are correct rather than when it was simple mathematical truths.

It's either zero or one. There was nothing in between. So what did you try until now? Are you having everything put in the skills? You have debates with the agent or you still have some guardrails of the old-fashioned tools that are statically and trying to have the safety net around it?

Uberto Barbini: Yes, it's very interesting question. And I mean, I definitely don't have a final answer. I don't think anybody at this moment.

Olimpiu Pop: None of us will.

Uberto Barbini: But the best analogy that I found is in security, they talk about this Swiss cheese model of security where you have multiple layers and each layer can have some holes, but as long as the holes don't overlap with each other, it will still statistically work. And for this reason, when working with AI, we try to have some checks like linters or some grep-like checkers directly guarding the code. Deterministic test coverage, which using mutation testing will also give you a next level of assurance. But also agents that check that the code is respecting the requirements, and then other agents that check that the code is covered by tests. And then other agents again that summarize the PR, for example, for me to review and help me review better and more quickly. I still look at the code, but only when the agents already prepare the PR and I can concentrate on those kinds of things where agents are still not as good, like those kinds of abstraction tests.

As you said, that they tended to write too many tests to detail instead of concentrating on the important parts. I don't want the 25 test for all possible edge case errors or stuff like that. So having all this kind of multiple layer of control, one on top, it makes much easier to work. But yes, your work is, as you said, is a kind of cowboy with this herd of agents going around.

Testing Strategies: How to Ensure the Quality? [16:08]

Olimpiu Pop: I was exploring mutation testing as a way of getting more reliability from that. I feel that is the right tool for this. Mutation testing usually wasn't the easiest thing to get right. A lot of people were doubting its effect, but now in this particular case where the code has a lot of mutation, mutation testing is the right way of making sure that all the things are done. Well, obviously it's expensive because in terms of time, it takes a bit more to just explore all pathways rather than just having one, but I think it's the right way of doing it. There are multiple ways of looking at testing and we had testing pyramids and we had testing diamonds and then we had reverted pyramids, inverted pyramid and so on and so forth. Is there a shape that you prefer or how do you approach it?

Uberto Barbini: My preferred way is to have a end to end test in place. I like to have the 100% confidence that the functionality still work. Maybe some edge case may be wrong, but nobody's working anything. That's the main point. And having some end-to-end test, only end-to-end test can give you this kind of confidence. And I also like to test the integration with the database and stuff like that on external API system only kind of more granular possible. So of course end-to-end test check the happy case, happy part, and then integration testing checking that each adapter basically work, do what is being told. And then unit test to check that the main logic is still valid. And what is changing AI is that it's all much faster because we were told that the TDD is too slow, for example, or to have to write test, but now AI can write the test in minutes and mutation testing can be quite heavy to configure, to understand.

AI understands it perfectly. It can write a mutation test for you. I'm not saying writing mutation test for everything, but maybe for the important things it's worked. Same things are for end-to-end testing. Takes a lot to set up, not only takes a lot of time, but also a lot of competencies because typically to set up an end-to-end tester, you need to be a bit of DevOps and engineer and stuff like that. AI can really speed up a lot of those things.

Olimpiu Pop: I was listening earlier to a perspective from somebody from Stripe and discussing about the actual cost of having something quickly done. And that's my feeling because most of the backend engineers, me included, always complained about the pixels, how to make it nice and all the details and all the browsers and the cross-browser. And then if you added the mobile into the equation, things got even more complicated. And now it's quite easy. You just say, well, obviously you have the internet full of the same pattern. I saw somewhere in Calabria, somebody put on a screen a system that was looking exactly like most of the things that Claude does. So it was like, okay, we're not buying code anymore. We're just building it with Claude during lunch break.

But as you mentioned, for me, it seems to be a trap. A lot of people will just say, "Okay, we don't need, I don't know, UX designers now. We don't need front end engineers now. Everybody's a full stack." But you're just missing out why we brought platform teams in place, why we had enablement teams, because that meant that you had the ability of building the building blocks nicely and then you just use them and everybody can actually have the right things to be in place. And I think this is where, as you said, the 10% that comes in handy because you know what questions to ask. And if you really want to look into the code as well, it'll just help with that. But I like your approach in terms of testing. And that's, as you said, this is the common sense if you actually worked. And that means have end-to-end tests at the higher level. So the business level, what are the scenarios moving from left to right? And that doesn't have to be granular and stuff like that.

So that'll be the happy paths and the things that, I don't know, 80% of the users will use. Then you go lower and then you have sliding a bit left or right with integration tests that will ensure that the points are covered where, I don't know, databases, APIs, reliance on other parts. And then use unit tests where you just have algorithms in functions that actually have to make sense. And that's something that is one-on-one with what we tried as best practices for so long. You did mention TDD as well, that people found it expensive, but they drove the architecture of a system in a given direction based on the way how things are behaving. Maybe these days a lot of people are confusing test-driven development, TDD with token-driven development, and that's another kind of TDD with lowercase T, but that's a whole different conversation. What do you think it's, I don't know, maybe let's say one, two, maybe three dangers that you see in coding with agents?

Uberto Barbini: I mean, my company, I also work a bit as AI adoption leader, let's say, so helping other people to adopt AI. One comment that I saw very often is that people don't really engage with AI. For example, they ask AI to write a ticket, AI agent, whatever it is. It write a wall of text and they basically don't read whatever, just say, "Okay, go with it." And that's the problem because I mean, if you think that it's too much test, you can ask AI, give me the short version or use a simple language, but you still needed to keep this control on what AI is doing.

Otherwise, you can end up doing basically the wrong ticket to fix the wrong things or to do the feature that is not what was really meant. And this is much more common than people realize. I often ask AI to give me the test when I'm discussing at the beginning before the planning, give me the test that you will write, like a high-level test and just show me that AI understood what they want to do or maybe I understood what AI is trying to do.

So not engaging with AI is definitely, I think, the main problem. Then there is people that don't want to use AI either for pure ideological reason like burning carbon or how they make the political position of this big AI brands, which I can understand in a sense, but I still think that as professional, we should use the best tool available. And at the end, it's just a tool. I mean, political battles are okay, but at the end in your job, you need to use the best tool available. There are several reason why someone loves programming. And my main reason to write programs is to build something. So for me, after all this year, it's still a bit of magic, how you write stuff and you can create an application, a video game or whatever. So for me, the code has never been the really important part.

I mean, I really would like to build something. There are some people that instead they really like to write the code and don't want AI to remove that from them. And I don't have an answer, but I think the future will be definitely AI writing economy. Writing code will be like now writing calligraphy instead of using a word processor. Good as hobby, but not.

Onboarding, Mentorship & Essential Skills for Junior Developers [24:12]

Olimpiu Pop: Fair enough. So to just look over what you mentioned, one of them is overreliability. When people are over relying on AI and they don't do it, well, this is the nicer way of doing it because I heard other stuff like meet proxies where people are just taking the output of the AI and they're just dumping it without any kind of critical thinking. Then it's about the impact on the real world because we, on the software engineering side, we got used to living into virtual space a lot, but now we are discussing about the real impact. And usually the impact comes in the places that are the most exposed to by the infrastructure and all the other stuff. And probably that falls into the green software part as well because it was the same thing. We are used to using things on the mobile, on the phone or on the laptops, but we don't see the huge data centers behind the cable where things are actually happening and what's the impact there.

And last but not least is the self of achievement, the purpose, because there are a lot of people that were real craftsmen that were building stuff by hand and now they might lose that feeling, that connection with what they built, but nevertheless, they would probably have to adapt to a changing landscape. And looking around it, we had quite a few conversations also into what it means in terms of education, raising the next generation. What it seems that it's a bit of discontinuation when AI comes to space is the early stages of developing. And here I see two dangers. One of them is very inexperienced developers or people that are not developers per se that take for granted whatever they get from the AI and then they use it and then that can be a problem. But also for the younger generation that don't have a chance of building step-by-step the next points, what would be the skills that you think we should bear in mind and how are you onboarding new people, younger developers?

Uberto Barbini: Yes, that's a big problem. I definitely will suggest any young developer not to trust. For example, we are doing interview and it's very hard now because really a lot of people came to the interview with some code written, but the code was written by AI and they cannot explain what it's doing and stuff like that. And that is not very wise in my opinion, because if you submit some code that you don't know what is doing, you want to be hired. I mean, it's pretty clear you're wasting your time and the other people's time. But also you are missing an opportunity because you can still have the code generated by AI and still using AI to explain you how the code works and learn. So using AI to learn, not just using AI to pass a test, which is also a problem with our university. I mean how computer science is taught, but that's a bigger discussion.

Olimpiu Pop: Yes. We don't have enough space for that.

Uberto Barbini: Yes. But I think one thing that I really don't know about that is that before AI, basically when a junior came in the team, he was given something simple to do and with some supervision from all the senior. I think we needed to put some supervision back, but supervision as in teaching and mentoring, but it's not easy with AI in the middle.

Olimpiu Pop: Well, whoever thinks about the future has to have this conversation because things are moving fast and it seems that we are just burning stages in a couple of points. Thank you, Uberto, for your time. Definitely a great conversation.

Uberto Barbini: Thank you, Olimpiu; it was really a pleasure.

Mentioned:

About the Author

More about our podcasts

You can keep up-to-date with the podcasts via our RSS Feed, and they are available via SoundCloud, Apple Podcasts, Spotify, Overcast and YouTube. From this page you also have access to our recorded show notes. They all have clickable links that will take you directly to that part of the audio.

Previous podcasts

Rate this Article

Adoption
Style

BT