函数式编程与智能体能否协同提升代码质量和测试效果?
原题Do Functional Programming and Agents Play Well Together Towards Improved Code Quality and Testing?
推荐理由适合希望提高代码变更可验性的团队,借纯函数与属性测试案例找出即使更换实现也应该成立的业务规则。
读前速览
文章探讨了函数式编程与 AI 智能体之间的协同作用,指出函数式代码显式且无副作用的特性能够提升智能体的表现,并有助于开展基于属性验证等稳健的测试策略。
函数式编程能够提升代码的清晰度和可组合性。
通过采用不可变数据和纯函数,函数式编程使代码行为更加明确、可预测。这让函数更易于组合,也降低了意外副作用发生的可能性,便于人类开发者和 AI 智能体理解系统。
基于属性的测试天然适用于函数式代码。
函数式函数的确定性使其非常适合基于属性的测试。这种方法可以自动生成测试输入,验证系统不变量是否成立,相较于传统单元测试提供了更稳健的测试方式。
AI 智能体在模块清晰、定义明确的代码库中表现更佳。
当代码具有清晰、独立的模块和定义明确的接口时,智能体能更好地完成工作。这可以降低智能体理解系统所需的上下文开销,从而更稳定地生成高质量代码。
质量保障需要采用多层次的方法。
依赖单一测试方法并不足够。文章提倡采用「瑞士奶酪」模型,结合代码检查工具、基于属性的测试、变异测试和多智能体代码审查,在开发流程的不同阶段捕获潜在错误。
完整摘要
Uberto Barbini 讨论了函数式编程原则——不可变数据和纯函数——如何营造出可预测的环境,使 AI 智能体比在复杂的面向对象框架中更容易处理代码。他强调,函数易于组合,也天然适合基于属性的测试;这种测试能够自动验证不变量。文章还指出,模块化和清晰的接口有助于智能体持续生成高质量代码,并倡导采用多层次的「瑞士奶酪」测试方法,确保系统整体可靠。
目录 · 9 节
在本期节目中,Uberto Barbini 将探讨整洁的函数式编程如何帮助 AI 智能体生成更优质的代码。他解释了不可变数据和纯函数如何提升代码质量、可维护性和可测试性。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.
他强调了在 AI 智能体时代开展软件开发的相关见解,并指出强大的模块化设计、多层次测试策略和有效的开发者指导至关重要。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 代理在模块化代码库中表现最佳,这类代码库拥有定义清晰的接口,且上下文开销极低,而非复杂的抽象层级结构。AI agents thrive on modular codebases with well-defined interfaces and minimal contextual overhead, not complex abstraction hierarchies.
- “瑞士奶酪”安全模型:结合代码检查工具、属性测试、变异测试和多智能体代码审查,对于借助 AI 进行质量保证至关重要。A "Swiss cheese" safety model: combining linters, property testing, mutation testing, and multi-agent code reviews is essential for quality assurance with AI.
- 开发人员的积极参与和严格监督仍然至关重要;必须指导初级开发人员将 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.
订阅平台:Subscribe on:
Apple Podcasts YouTube Soundcloud Spotify Overcast Podcast Feed
Do Functional Programming and Agents Play Well Together Towards Improved Code Quality and Testing? Play episode Play
文字记录Transcript
Olimpiu Pop:大家好。我是 InfoQ 编辑 Olimpiu Pop。今天和我对谈的是一位资深软件开发者,他接触过多种编程语言。最近,他对函数式编程颇有好感;即使在 AI 智能体时代,他也始终坚守自己熟悉的领域——函数式编程。今天的嘉宾是 Uberto Barbini。他将和我们聊聊,自从写完《From Objects to Functions》一书之后,情况发生了哪些变化,以及他如何拥抱借助 AI 智能体进行编程。话不多说,Uberto,请先介绍一下自己。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:嗨,Olimpiu。谢谢你邀请我。我是 Uberto Barbini。很多年前,家用电脑这种东西刚出现时,我就开始编程了。我多年都在使用 Java 和 JVM 相关语言工作,不过过去三年里,我们主要使用 Python。我不认为编程语言有那么重要。相比语言本身,我认为真正重要的是人们如何使用语言。说到你的问题,大约十年前,在使用面向对象编程多年之后,我转向了函数式编程。我很喜欢自己看到的东西,于是开始越来越多地在 Java 和 Kotlin 中使用函数式编程,后来就写了那本书。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.
为什么要转向函数式编程?[02:05]Why Transition to Functional Programming? [02:05]
Olimpiu Pop:谢谢。我们就不追溯函数式编程或面向对象编程的历史了。不过正如你提到的,你转向了函数式编程,而且再也没有回头。我是说,我们确实会使用对象,但我知道,从我们之前的交流来看,现在已经不像过去那样了。是什么让你开始采用函数式编程?你认为编写函数式代码的主要好处是什么?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:正如你提到的,了解发生了什么、有哪些副作用以及其他一切都很重要。很长一段时间以来,面向对象的编程语言中都有各种各样的方法来做到这一点:mock、stub、spy 等等,都是为了弄清楚究竟发生了什么。从函数式的角度来看,这应该更容易。你有输入,有负责执行计算的函数,然后得到输出,整个过程应该相当线性。从这个角度来看,你编写测试、测试自己工作的方式是如何演变的?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?
乌贝托·巴尔比尼:是的,使用函数式编程,测试确实要容易得多,因为正如你所说,每个类都是不可变的。函数需要什么、会产生什么结果,都非常明确。如果你只需要模拟一个会给我们返回其他结果的函数,比如说,只要在测试中创建一个小函数,并将它作为输入传进去就行了。这不仅让一切都更容易测试,也开辟了一种新的测试方式。比如,有一种称为属性测试的风格,你可以让测试框架自己创建值。你只需指定基本上保持不变的属性。举个例子,你可以随机生成一个对象,根据这个对象生成 JSON,然后再根据这个 JSON 生成对象,而新对象必须与原始对象完全相同。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:好的。Olimpiu Pop: Okay.
Uberto Barbini:10 年前我开始接触函数式编程时,它还相当实验性,比如 Haskell 或 Lisp。人们或许认为它很有前景,但速度很慢,并不真正适用于实际编程。相比之下,Java 当时非常流行,推崇 JavaBeans 这种理念——对象中的所有东西都是可变的。可以说,这与函数式编程背道而驰,所以对我来说,选择这种编程风格有些不同寻常。不过如今,随着各大主流语言的发展,Java 开始引入 Lambda 和流,现在还有 Valhalla;Kotlin 在支持函数式编程方面已经比 Java 友好得多。Rust 则使用 Result,而不是异常。整个编程领域都在朝这个方向发展。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:嗯,显然,正如我们过去几天看到的那样,让你的编程语言具备很高的安全性非常重要。你提到了 Rust,也提到了其他编程语言;此外,供应链在这里也变得相当重要,尤其是在欧洲,《网络韧性法案》出台之后。不过在世界各地,人们都在转向更安全的方向,而显然,能够理解自己的代码也非常重要。或者说,真的如此吗?你怎么看?现在我们又进一步远离了一层抽象。我们谈过在代码层面进行抽象。如今,每个开发者都像是一个牧羊人,或者我不知道该怎么称呼——一个负责统筹一群智能体的协调者。如今代码还重要吗?你怎么看?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:我确实认为,即使有了 AI 智能体,代码质量依然很重要。原因在于,不仅有相关研究论文,我自己的经验也表明,代码库质量好的话,智能体的效率会高得多。与必须处理意大利面式代码、混乱代码库的智能体相比,它们能够更稳定地生成更好的结果。当然,我们认为好的代码,并非每一点对智能体都真的重要,但良好的模块化以及定义清晰的函数确实能帮到智能体。道理应该很简单,因为智能体会运用“上下文”这个概念。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.
AI智能体的模块化与接口设计 [08:54]Modularization & Interface Design for AI Agents [08:54]
Olimpiu Pop:那我来看看能不能理清你刚才提到的内容。你说,和智能体协作时,如果代码是模块化的、接口也很清晰,智能体就更容易编写代码。那么,公平地说,这是不是相当于先立下一个标杆?你先把东西构建得很好、很规范,然后,比如说,我不知道,可以做成一个模块,或者一个类——如果还有人在用面向对象编程的话——做到这一步后,把它封装在合适的位置,提供一个干净的接口,这样之后就更容易在此基础上继续构建。随着每一步不断推进,事情会容易得多,代码也会变得更整洁。这样说公平吗?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:没错。模块化真的非常重要。你提到了 SOLID 原则,这很有意思,因为 SOLID 原则并非每一条都对智能体真正重要,但这种功能性的模块化分解确实非常有帮助。比如说,智能体不太擅长处理抽象。因此,像接口套接口或复杂的继承结构,并没有多大帮助。但对于清晰、定义明确的类和模块,智能体能够始终保持工作质量的一致性;面对非常复杂的代码时,它们的表现就会很不稳定。也许有一件事做对了,下一件事却会把整个代码库搞得一团糟。这是因为上下文可能会带来问题,智能体很容易被偶然的复杂性之类的东西带偏。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:基本上,我现在已经不再使用 IDE,也很少查看代码,而是更多依靠智能体检查其他智能体。不过,团队之间始终也需要一定程度的协调,因为我记得 Kent Beck 曾说过,有了 AI,我 90% 的技能都完全没用了,但剩下的 10% 变得重要 10 倍。所以很明显,任何尝试过的人都会发现,如果你是一名优秀的开发者——我是说,有系统设计经验的人——你真的能快 10 倍,或者多做 10 倍的事情。但对于没有这类经验的人来说,他们常常会被 AI 带偏,或者无法稳定地产出成果。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:嗯,和人打交道很有挑战性。现在,要让测试到位,和智能体打交道也很有挑战性。不过这是另一种挑战。比如说,根据我最初的经验,我会要求提供代码覆盖率,得到的答复可能是“再看看”,几个月后又会听到各种借口,解释为什么做不到。和智能体打交道时,你不会听到借口,但你会看到测试全绿。仔细看这些测试,你会发现它们并不完全是你会写的那种,而且似乎非常、非常专注于自己的用途和目标——开发出某个东西。如果它们没法抢在别人前面,就会开始偷工减料。你要如何把这种层次的经验融入测试框架?因为我使用大语言模型时遇到的一个主要问题,就是你面对的是一位专家。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.
你有这么一个东西,如果只是拿它和人类比较,它相当于一个至少拥有 100 个博士学位、无所不知的人。然后你只要说,现在你要当一名非常擅长编写金融软件的 Python 开发者。你会觉得某些地方像是开关被拨动了,于是从那一刻起,它就朝着左边或右边走,而不是沿着你希望的直线继续前进。或者,你会遇到一个有时会产生幻觉的东西,但你又让同一个东西去验证自己的工作。可你怎么能确定它做得对呢?所以这些就是挑战,这也是我的……因为写了 20 年代码和数值计算,习惯了 1 就等于 1,要改变这些规则、理解它,并接受一种统计学的方法,确实很难。在这种方法下,我知道一开始有 60% 的情况是正确的,而不是像简单的数学真理那样确定无疑。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:是的,这是个非常有意思的问题。我是说,我肯定没有最终答案。我觉得目前谁都没有。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:但我发现最好的类比来自安全领域。他们会谈到安全的“瑞士奶酪模型”:你有多个层,每一层都可能有一些漏洞,但只要这些漏洞彼此不重叠,整体在统计意义上仍然有效。因此,在使用 AI 时,我们会尝试设置一些检查机制,比如 linter 或类似 grep 的检查器,直接守护代码。确定性测试覆盖率也能提供保障,而变异测试则能进一步提升保障级别。此外,还可以让智能体检查代码是否符合需求,再让其他智能体检查代码是否有测试覆盖;还可以让其他智能体总结 PR,供我审查,帮助我审查得更好、更快。我仍然会查看代码,但只有在智能体已经准备好 PR 之后才会看,这样我就能专注于那些智能体目前还不太擅长的事情,比如这类抽象测试。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.
正如你所说,他们往往会写太多测试,过于关注细节,而不是把精力放在重要部分上。我不想为了所有可能的边界情况错误之类的东西写 25 个测试。所以,有这样多层的控制机制,一层套一层,工作起来就轻松多了。不过,没错,正如你说的,你的工作有点像牛仔,带着一群代理到处跑。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.
测试策略:如何确保质量?[16:08]Testing Strategies: How to Ensure the Quality? [16:08]
Olimpiu Pop:我一直在探索如何通过变异测试来提高可靠性。我觉得这正是适合这项工作的工具。通常,变异测试并不容易做好。很多人都对它的效果心存怀疑,但在这个代码中变异很多的特定情况下,变异测试是确保所有事情都做到位的正确方法。当然,它的成本显然不低,因为与只测试一种情况相比,探索所有路径要多花一些时间,但我认为这是正确的做法。看待测试的方式有很多种,我们有测试金字塔、测试菱形,还有倒置金字塔,等等。你更偏好哪种形状?或者说,你是如何处理测试的?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?
乌贝托·巴尔比尼:我更喜欢先准备好端到端测试。我希望能有百分之百的把握,确认功能仍然正常。也许某些边缘情况会出错,但没人会因此什么都做不了。这才是重点。而且,只有端到端测试才能给你这种把握。我也喜欢针对数据库集成以及外部 API 系统之类的东西进行测试,尽可能细化。所以,端到端测试当然是检查正常流程;集成测试则检查每个适配器是否基本正常工作、是否按要求执行;然后用单元测试检查主要逻辑是否依然有效。AI 带来的变化是,所有这些都快得多了。比如以前有人说 TDD 太慢,或者写测试很费时间,但现在 AI 几分钟就能写好测试,而变异测试的配置和理解起来都可能相当繁琐。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 完全理解这些内容。它可以为你编写变异测试。我不是说所有东西都要写变异测试,但对于重要的部分,这种做法确实有效。端到端测试也是如此。搭建起来需要做很多工作,不仅耗时,还需要具备不少技能,因为通常要搭建端到端测试环境,你得懂一些 DevOps,还得是工程师之类的。AI 确实能大大加快这些工作的进度。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:我之前听到一位来自 Stripe 的人分享了一个观点,讨论的是快速完成某件事的实际成本。这也正是我的感受,因为大多数后端工程师,包括我自己,总是在抱怨像素、怎么把界面做得好看,以及各种细节和浏览器兼容性。后来如果再把移动端也考虑进来,事情就变得更加复杂了。而现在这事相当简单。你只要说,嗯,网上到处都是同样的模式。我在卡拉布里亚某处看到,有人把一个系统放在屏幕上,看起来和 Claude 做出来的大多数东西一模一样。所以当时我就想,好吧,我们现在不买代码了,午休时直接用 Claude 来构建。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.
但正如你提到的,对我来说,这似乎是个陷阱。很多人会直接说:“好吧,我们现在不需要 UX 设计师了。我不知道,我们现在也不需要前端工程师了。人人都是全栈工程师。”但你只是忽略了我们为什么要组建平台团队、为什么要有赋能团队,因为这样你就能把基础模块搭建好,然后直接使用它们,这样每个人都能拥有合适的东西。我认为这就是你说的那 10% 能派上用场的地方,因为你知道该问什么问题。而且,如果你真的想深入了解代码,也会有所帮助。不过我喜欢你在测试方面的做法。正如你所说,这其实是常识——前提是你真正做过相关工作。也就是说,要在更高层级进行端到端测试,也就是业务层面,看看有哪些从头到尾的场景。这些测试不必细化到很琐碎的程度。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.
所以这就是那些顺利路径,以及我不知道,大概 80% 的用户会用到的东西。然后再往下,你可以稍微往左或往右调整,加入集成测试,确保覆盖到数据库、API、对其他部分的依赖等方面。接着用单元测试测试那些算法,也就是函数中确实需要讲得通的部分。这和我们这么长时间以来一直努力推行的最佳实践是一致的。你也提到了 TDD,很多人觉得它成本高,但它们会根据系统的行为方式,推动系统架构朝着特定方向发展。也许如今很多人把测试驱动开发(TDD)和令牌驱动开发(token-driven development)搞混了,后者也是一种 TDD,只不过 T 是小写的;不过那又是完全不同的话题了。你觉得,在用智能体编写代码时,我不知道,也许列一两条,最多三条你认为会遇到的风险?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:我的意思是,我在自己的公司里也多少担任着 AI 推广负责人的角色,可以这么说,帮助其他人采用 AI。我经常看到有人说,他们其实并不会真正和 AI 互动。比如,他们让 AI 写一张工单,或者让 AI 智能体做点什么。AI 写出一大篇文字,他们基本上也不看,直接说:“好,就按这个来。”问题就在这里。我的意思是,如果你觉得内容太多,可以让 AI 给你一个简短版本,或者用简单的语言来写,但你仍然需要掌控 AI 正在做什么。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.
否则,你最后可能会接错任务,修错问题,或者做出并非真正所需的功能。这种情况比人们意识到的要常见得多。我经常会在规划开始前讨论时,让 AI 给我它打算编写的测试,比如一个高层次的测试,让我看看 AI 是否理解了他们想做什么,或者我是否理解了 AI 打算做什么。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.
所以我认为,不去接触 AI 绝对是主要问题。还有些人不愿意使用 AI,纯粹是出于意识形态方面的原因,比如担心碳排放,或者反感这些大型 AI 品牌所持的政治立场。这一点我在某种程度上可以理解,但我仍然认为,作为专业人士,我们应该使用现有的最佳工具。说到底,它只是一个工具。我是说,政治斗争无可厚非,但在工作中,你需要使用现有的最佳工具。人们喜欢编程有各种各样的原因。我编写程序的主要原因,是为了创造一些东西。所以对我来说,过了这么多年,写下一些代码就能创造出一个应用、一款电子游戏或其他东西,这仍然有一点像魔法。因此对我来说,代码本身从来都不是最重要的部分。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.
我是说,我真的很想做点东西。有些人则是真的很喜欢写代码,不希望 AI 把这件事从他们手中夺走。我也没有答案,但我认为未来肯定会是 AI 编写代码的经济时代。写代码会像现在不用文字处理器、转而写书法一样。作为爱好挺好,但不行。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.
初级开发人员的入职培训、导师指导与必备技能 [24:12]Onboarding, Mentorship & Essential Skills for Junior Developers [24:12]
Olimpiu Pop:有道理。简单回顾一下你提到的内容,其中之一是过度依赖。人们过度依赖 AI,却不进行……嗯,这算是比较温和的说法,因为我还听说过“人肉代理”之类的情况:人们只是拿来 AI 的输出,直接一股脑儿地贴上去,完全不加任何批判性思考。接下来是对现实世界的影响。我们软件工程领域的人已经习惯了大量生活在虚拟空间里,但现在我们讨论的是现实影响。而影响通常发生在基础设施及其他相关事物暴露程度最高的地方。这可能也属于绿色软件的范畴,因为情况也是一样的。我们习惯于在手机或笔记本电脑上使用各种东西,却看不到网络电缆背后那些庞大的数据中心——真正的事情都在那里发生,也看不到那里会造成什么影响。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.
最后但同样重要的是成就感和目标感。很多人曾是真正的工匠,亲手打造各种东西;如今,他们可能会失去那种感觉,失去与自己所创造之物之间的联系。尽管如此,他们很可能还是得适应不断变化的环境。放眼来看,我们也聊了不少关于教育以及培养下一代的话题。AI 进入开发领域后,似乎出现了一些脱节,而这正处于发展的早期阶段。在我看来,这里有两种隐患。一种是经验不足的开发者,或者严格来说并非开发者的人,会把 AI 给出的内容视为理所当然,直接拿来使用,这可能会带来问题。另一种则是年轻一代没有机会循序渐进地搭建起后续能力。你认为我们应该牢记哪些技能?你们又是如何让新人、年轻开发者融入团队的?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:是的,这是个大问题。我一定会建议年轻开发者不要轻信。比如说,我们现在在进行面试,这件事变得很难,因为确实有很多人带着一段写好的代码来面试,但代码是 AI 写的,他们却解释不了代码在做什么之类的。在我看来,这么做不太明智,因为如果你提交了一段自己都不知道在做什么的代码,还想因此获得聘用,那显然是在浪费你自己和别人的时间。不过,你也错过了一个机会:你仍然可以让 AI 生成代码,再用 AI 向你解释代码的工作原理,从中学习。所以,应该用 AI 来学习,而不只是用 AI 来通过考试。我们的大学也存在这个问题——我是说计算机科学的教学方式,不过那是个更大的话题。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:是的。我们没有足够的空间放下那个。Olimpiu Pop: Yes. We don't have enough space for that.
Uberto Barbini:是的。不过我觉得有一点我确实不太了解:在 AI 出现之前,基本上有新人加入团队时,都会安排一些简单的工作给他做,并由资深成员给予一定的指导。我认为我们需要重新加强指导,但这里的指导是指教学和辅导,只是有 AI 介入其中后,这并不容易。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:嗯,凡是思考未来的人,都必须进行这番对话,因为变化日新月异,感觉我们在几个方面都在不断跨越阶段。感谢你抽出时间,Uberto。这确实是一次很棒的对话。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:谢谢你,Olimpiu;这次交流真的很愉快。Uberto Barbini: Thank you, Olimpiu; it was really a pleasure.
提及:Mentioned:
- 从对象到函数
- 函数式编程如何帮助你编写更出色、更优雅的 Web 应用程序
- 超越传统验收测试