Functional programming principles help AI generate better code

Uberto Barbini appeared on an InfoQ podcast to discuss how functional programming can improve code quality when working with AI agents. Barbini, who wrote the book "From Objects to Functions," explained that the core idea behind functional programming is immutable data and pure functions. Pure functions only use their arguments and produce only their result, making everything explicit. This approach makes code easier to compose and less prone to side effects.

Immutable data and pure functions improve testability

Barbini noted that tests are much easier to write with functional programming because each function is clear about what it needs and what it produces. If a function needs to simulate a different result, a small test function can be created and passed as input. This clarity also opened the door to property-based testing, where the test framework generates many possible values automatically. With property testing, a developer specifies an invariant property—for example, generating JSON from an object and then generating the object again from that JSON, which must be identical to the original. Only functional programming naturally enables this style of testing.

AI agents prefer modular code with well-defined interfaces

AI agents thrive on modular codebases with minimal contextual overhead, not complex abstraction hierarchies. Barbini explained that agents are not great at abstractions like "interfaces of interfaces" or complicated inheritance. However, clear well-defined classes and modules help agents maintain consistent quality. Without good modularization, agents may get sidetracked by accidental complexity, producing correct code in one area and completely messing up the codebase in the next. Functional modular decomposition helps because each function works only on its arguments, making it easier for an agent to determine what it can and cannot change.

Swiss cheese safety model for quality assurance

Barbini described a "Swiss cheese" model for quality assurance with AI. Just as security uses multiple layers where holes must not overlap, software quality requires multiple overlapping checks. These include linters, property testing, mutation testing, and multi-agent code reviews. Mutation testing introduces small changes to code to verify that tests catch them. Deterministic test coverage combined with mutation testing provides another level of assurance. Additional agents can check that code respects requirements, verify test coverage, and summarize pull requests for human review.

Developer mentorship remains critical

Active engagement and critical oversight from developers are essential. Junior developers must be mentored to use AI as a learning tool rather than a shortcut. Barbini noted that good developers with systems design experience can be 10 times faster or produce 10 times more things with AI, while those without this experience often get sidetracked or cannot produce results consistently.

Testing strategies in the age of AI agents

Barbini discussed challenges with testing when AI agents are involved. With people, asking for code coverage often results in excuses months later. With agents, developers get green tests, but examining the tests reveals they may not match what a human would write. They are often very focused on their specific purpose rather than covering important edge cases. Barbini advised against writing 25 tests for every possible error—instead, multiple layers of control make it easier to work effectively. He compared the developer's role to a cowboy herding agents, concentrating on abstraction tests where agents are still weaker.

Code quality matters even with AI assistance

Barbini emphasized that study papers and experience show AI agents are much more efficient and produce better results more consistently when working on a good codebase rather than spaghetti code. The reason is that agents use a concept of context—the less they need to keep in mind, the better they can use their strength. Functional programming works well with this because pure functions only work on their arguments, making it easier for agents to reason about changes.