Functional Programming with the Aid of AI - Venkat Subramaniam
About this talk
This talk explores the principles of functional programming and its relationship with declarative programming, illustrating the importance of abstraction in code. The speaker contrasts imperative programming, where developers specify both 'what' and 'how', with declarative programming, which focuses solely on 'what', leaving the implementation details to the underlying functions. Through practical examples in Java, the speaker highlights the pitfalls of complex imperative code and advocates for the use of higher-order functions that enable functional programming. The talk also emphasizes the significance of understanding immutability and lazy evaluation in functional programming. The speaker warns about the limitations of AI-generated code, stressing that while AI can assist in writing code, it may produce suboptimal solutions if not guided by quality programming principles.
Full transcript
I want to talk about uh two things in here. One is about of course functional programming and the other is with the aid of AI. So, we're going to be doing two parts in one. And and we'll see why we need to really do two parts in one as we go through this. Uh so, I'll talk about a few things for a while and of course along
the way if you have any questions, please don't hesitate uh draw my attention, ask ask your question, and then we'll shift from my talking part to a group of exercises we're going to go through, and at that time I'll ask for your input and then participate along the way as well. So, with that said, we are talking about different styles of programming. Now, way back in time,
we have been doing procedural programming and some of us may have written code in languages like, you know, way back in time, right? VB or potentially JavaScript, you may have written code with functions, you may have written code in C programming language at some point. Those are all very procedural in nature. But then over the past about 30 years or so, maybe a little longer, we've been
doing a lot more of object-oriented But whether you do uh object-oriented programming or not, the styles of programming can be really distinguished into a few fundamentals. And one of them is the so-called imperative style of programming. So, the question is what is, you could say, imperative uh style uh style of programming. So, the answer to that is imperative style, you can say imperative uh style uh is
where you tell what to do and also along the way um how to do it. So, essentially you tell what to do, but you also have to tell how to do it. So, this is your typical imperative style of programming. So, imperative style of programming is where you have to tell every single step, every single detail. So, this is like going to somebody and saying, "Do me
a favor, take this envelope, take this stamp, lick it, and take this envelope and put it on that left side and stick it." So, you're saying every single step of what to do, that is your imperative style of programming. Now, if you think about languages like Java, so in Java and and many other languages, we did OOP plus imperative style for a very long time. So, even
if you use objects, how do you iterate over objects? How do you perform a group of operations on objects? Those things were really imperative in nature. So, then along the way, we started looking at declarative style of programming. So, what is declarative style of programming? Well, in this case, declarative style is where you tell what to do, but not how to do it. So, you tell what
to do, but you don't say how to actually do it. And then you of course say, "Wait a minute, if you don't tell me how to do it, then how do how does it do it?" Uh we depend on underlying, if you will, library, you library of code uh to do things. So, essentially, you tell what, but the how is delegated to other functions. And that's declarative
style of programming. So, let's think about this difference for a minute. So, suppose, let's say, suppose you have a collection of names, uh basically, a list of strings, let's say. And you want to know if our good friend Nemo is in there. So, how do I tell you if Nemo is in this collection of names? So, you know how to do this, right? So, in the imperative
style, you're going to do this. This is what we are all very familiar with, right? So, what do you say? For int i equal to zero, i less than the names. dot Is it size? Or is it length? See, I don't know. Oh, it's size. So, then, oh, wait. Is it less than or less than or equal to? Have you ever thought about it? Every single time,
right? So, somebody says, "What is the symbol?" That is the international symbol for being confused. Because every time you write it, you're like, "Oh, wait. Is it less than or less than or equal to, right?" And if you thought that was the only thing you had to do, you say, "I plus plus." And somebody says, "Why not plus plus I?" And why is it I plus plus?
So, notice how you get tangled up in all these details that you have to deal with, right? So, you are doing this, and that is too verbose. So, instead, you say, "For string name coming from names." Oh, what a relief, right? You didn't have to write all that code. That's much better. Then what do you do? If name is equal to Nemo, and the minute you write
this, somebody says, "Wait, that's not right." They say, "It should be name.equals Nemo." See how much of the burden you carry with you? And you have to make that decision as well. And then you say, "If we found the Nemo, we're going to say in this case found is equal to true in here." And so we'll start with Boolean, let's say found equal to false. And then,
what are we going to do? If you do this, then you say, "If found, then you can simply say in this case, output Nemo found." We can play a nice little music. We can celebrate finding Nemo. If not, we can play a very sad music and say Nemo not found. Now, when I'm typing this, I know 70% of the people in the room are waiting to scream
and say, "Wait, you forgot to put a you know, break over here." Because if you don't put a break, it's going to go through the loop unnecessarily. But if you put a break, it gets out of it. Now, when you look at this code, how does this feel? It feels very familiar, But what is the problem with this code? To know what the code is doing, you
have to read everything. So, writing this is not hard, but reading this is a lot of work. So, working with code like this, how does this feel? I'll tell you how this feels. Because everyone knows this, because every family has this. You know that uncle that you never want to talk to? Because there was one time you said hello to this uncle and the next 3 hours
of your life was gone. So, what do you do? You're in a event, a wedding, a party, you open the door, you see that uncle, you take the other door, right? That's how this code feels like, right? This is like the uninvited uncle because it drags you in, beats you down, and you're like, "Gosh, it took all that effort to know." So, when you look at an
imperative style code, the problem with an imperative style code is the details are on your face. So, essentially, you cannot escape the details. When you start reading it, it just pours down on you, right? That's imperative style code. It has a lot of accidental complexity in here. So, how do you write this code differently? In declarative style, you can simply say, "names.contains Nemo." Right? So, if names.contains
then you can simply say the output, you can simply say output, and you can say Nemo found, and otherwise, else, you can say simply sadly, "Nemo not found." Now, compare this code to the code we wrote in the top. And when you look at the code in the top, we did all this work in the top. Whereas here, we just simply called the contains method. So, you
don't tell how to find it. You just said, "Hey, go do this for me." And how do you do it? That's your job, contains. I don't have to worry about it. So, if I ask you, can you, don't look at the computer, can somebody tell do its work? Anyone want to try? It's the same as in the top. That's one good answer. Any other answers? Maybe it's
running it in parallel, Okay. Anything else? Maybe a hash map. how about care. You say, "Mhm, that's rude." Okay, sorry. Let me say it politely. It's encapsulated. It's encapsulated. Can we translate the word encapsulated to English? I don't care. Because that's what it is, right? Because you're like, "Do this for me. I don't care." Okay, I see what you're thinking. You're like, "Oh, Ranker, that's not true.
I do care. Because it may have consequences. I mean, I want to know the performance." Aha, but here's the difference. The contains function the contains method says, "I do this work, but I won't bother you with the details." But if you really need to know, press that alt B, and I will take you into the function and I'll show it to you. So, in other words, you
can zoom in and zoom out, but the details are not bothering you unless you ask for it. There's a name for this, right? What is What is this called? What is the top one doesn't do, the bottom one does? This is called SLAP. SLAP stands for single level of abstraction principle. So, we read about all these principles, But when you look at what you do, you're like,
"Aha, now I see It's a single level of abstraction because at my level of the code, I just say, is it there or not? How do you find if it's there? Hey, that's not my level of detail." I'm going to focus on my level. If you need to go lower level, dig into that function, but don't bother me with it at this level. So, imperative style code
fails. Slap. Declarative style code is honoring the slap principle. So, that's how declarative style code reduces the accidental complexity. So, when you want to reduce the complexity in code, you want to rely upon a higher level of abstraction. You say, "All right, wait a minute. You're saying declarative style code has less accidental complexity." That's correct. But then you say, "But wait a minute. I thought I came
to a session on functional programming. You've not even talked about that. You've been ranting about imperative style or declarative style. What's going on, right?" As it turns out, functional style is equal to declarative, uh if you uh think about the the relationship, declarative style plus the use of higher order functions. That's where the relationship really comes in. So, the reason why functional programming shines is that it's
built on top of declarative style of programming. So, if you think about it, not all things declarative are functional, but everything that's functional is declarative. So, contains is not functional. Contains is declarative. But a functional style code is also declarative, but not everything declarative is functional in nature. So, the question is, what does functional programming really provide for us? Functional programming delegates to low-level functions things to
do, but it also uses higher-order functions. But then the question is, what are higher-order functions? So, typically, we work with, you know, a regular, you know, ordinary, right, functions. So, what do these do? We may we can pass data to functions, right? So, we can do this. We can pass data to functions. We can create data in functions. We can return data from functions. So, we are
all used to this. You create a function, you can pass a data to it. You can create a data in a function, you can return a data from a function, right? We're used to doing this. But that brings the question, what are then higher- order functions? So, once we know what these functions are, the question is, what are higher-order functions? And the answer is, we can pass
functions to We can create functions in functions. We can return functions from functions as That's what makes a function a higher-order function. So, essentially, you are bridging these abstraction, and you're saying, I can model using these functions, and what if a function can receive a function as an argument? What if a function can create a function and or return a function as a result? So, if we
can do that, what happens then? We can we can then pass around functions like we pass around data. So, this is what people mean when they say code as data. So, when code becomes data, just like you pass data around, you can pass code around. So, this gives you the flexibility to model with functions just like we model with objects. So, typically we do what is called
object decomposition. So, what does object decomposition really mean? So, for example, if I ask you what is this, if you're able to see this, you're going to say, "Oh, Venkat, that's a pen." And you're absolutely correct. But, we humans are so good at abstraction. So, if I say, "What's this?" you say, "That's pen." But, what I can do is, as as you can see, I can kind
of remove this, and you can see there's a little piece of metal here, right? You see that? I can then pull this out, and you can see that is a spring. Uh when I say spring, I'm talking about the metal spring, not the Java framework And then you see this little thing that holds the ink. Now I'm asking you, what is this? And you say, "My gosh,
that's a metal you have on your hand. That's a spring. That's a tube with liquid in it. Oh, there is a little tip. I can remove this. It's going to make a mess, and they're going to kick me out if I do." But, we can keep going this way, and suddenly you can say, "Oh my goodness, this contains maybe billions of atoms and electrons and protons." And
we can go crazy going deeper, further level down. Or I say, I drop this in here, I put the spring back in here, and then I'm going to, of course, roll this back on top of it. And then I can ask you, "What is this?" And you say, "Gosh, you got a pen on your hand." This is your abstraction. What we just saw here is object composition.
So, we took these small objects, right? We took a little metal, we little take a little spring, we took a little tube with liquid in it, and we composed it, and we have an object which I can use to write. That is object composition. In a similar way, what if I give you a few functions, but this time not objects with functions, and you compose that functions
to create a bigger function. Just like in you can compose objects to create bigger objects. So, just like we can do object composition, we can do functional composition as well. So, this gives a different way of thinking, a different way of modeling what we are trying to do. So, this gives us ability to compose a system not just using objects, but using functions as well, and that
gives us the capability to do a bit of a different modeling. Now, when it comes to functional programming though, there's a lot of misconception about functional programming. And people often are fearful of this. People say, "Oh my gosh, functional programming is really hard. The code is hard to read." And this is one of the problems we carry as humans. What is the problem? If you If you've
never done functional programming, and if I show you a functional pipeline where I say names.stream.filter, and I say given a name, name.length is equal to if I say equal to five, and then map, I say string double colon to upper case, if I say something like this, right? And if I write the code like so, and then I say, "I'm going to then call maybe another function
here and another one here." And you're going to look at this and say, "My goodness, this is really hard." But why do you say it's hard? We tend to do this quite often. This is our problem as humans. We often confuse, and we do this very often, we often confuse with simple and unfamiliar uh with complex. So, this is one of the problems we often face. So,
I often do this to programmers when I work with them. When they say, "Oh my gosh, this is complex." I say, "Excuse me, please. Hang on a second. Are you saying it's or are you saying it's unfamiliar to you?" There is a big difference, right? If it is unfamiliar, it may be complex, it may be simple, I have no way to evaluate it. Right? If I'm walking
on the streets over here, and I see all these quickly characters on the sides of the road, and I say, "Wow, these people are very complex people. Look at all the scripts they have. This is absolutely ridiculous." And you're going to look at me and say, "Do you understand Kannada?" I'm like, "No." Then how can you judge whether it's simple or complex? That's ignorance on my part,
not an evaluation fair evaluation. So, but on the other hand, if I really know it, and I look at it and say, "That's not a elegant way to say it. There's a better way to say it." So, now we are causing the evaluation between what is better quality versus poor quality. So, the first thing is to become familiar with things. Then we can evaluate complexity versus simplicity.
But often times our fearful mind says, "What's not familiar is complex, and what's familiar, even though could be very complex, we try to claim that it is simple." So, that is one of the things to keep in mind is in general functional programming can lead to simpler code, but that requires us to be familiar as But, given this, there are a few tenets that you want to
really think about. The first uh tenet is that a functional pipeline uh so, pipeline pattern. So, a functional pipeline pattern, you write a pipeline of functions. So, in other words, how does this really work? So, we model or we design in a way, in this case, that data flows and transforms uh through a pipeline. So, if you think about this as a pipeline, it's a transformation of
data. Just like how data flows, you open the pipe and the water flows, and you're like, "Oh, nice. This is nice warm water." But, how are you getting warm water in here? Well, the water was taken, it was sent through a filter where they purified the water, they sent it through a heater, which heated the water, and now you get a hot water through the pipe. So,
just like that, the data transformed through the pipe, and here the filter and the heater are the functions that you have in the pipeline. So, this functional pipeline pattern, it's a pipeline, all right, but it's a pipeline, you can say a pipeline uh uh uh sorry, pipeline of functions uh in this case, uh through which data flows. That's a very first way of thinking. The second thing
is the pipeline is lazy uh in evaluation. This is one of the most fundamentals of functional programming. In functional programming, evaluation is lazy. So, earlier I said functional programming is declarative with the use of higher-order functions. Now, I'm going to say functional programming is equal to functional That is the very first thing. I talked about functional composition a few minutes ago. I said just like you can
compose objects, you can compose functions. It's function composition plus really lazy evaluation. That's what really makes functional programming really effective. That's one of the things. But, in order to support this lazy evaluation, there is something you have to do. Functional programming, you can say, relies on So, relies on lazy evaluation of far efficiency. So, if you really want the pipeline to be efficient, it relies on lazy
evaluation. Why is that? When you draw a functional pipeline, if it's going to execute every data in the functional pipeline, it may not be very efficient. It may be very slow. It may lead to more garbage that it needs to collect. On the other hand, it says, "I'm going to minimally execute this code, and if something doesn't have to be executed, I'm not going to run it."
So, it saves effort by not doing any work that's not necessary. But, as it turns out, and lazy evaluation relies on you can say relies on immutability for correctness. So, this is why in functional programming, we should program for immutability. So, we emphasize immutability not because it's fashionable, because it's cool. It is an existential problem. If you encourage mutability or side effects in functional programming, it will
run really efficiently to give you the wrong results. What's the point in doing it? So, correctness depends on immutability. So, there are these tenets we need to really understand when we do functional Why am I emphasizing this? The reason I'm emphasizing this is that not everyone who writes functional style code understands these tenets. Now, if I don't understand this tenant, what am I going to do? I'm
going to write functional style code, which I think is functional, but it is doing the wrong things. Now, let's say you hire a new junior employee who doesn't know anything. And you send that poor person to me and say, "Learn from this dude." And I'm going to teach them all the bad ways to write functional programming, and they're going to go off thinking they know how to
write functional style code, but they are writing really bad You know who this young un- you know, little person who doesn't know anything, but he's learning very quickly. Who is this? That's the AI of the world. So, we have trained the AI on garbage that people have written over time. And poor AI doesn't know what is a good quality functional style code and a bad quality functional
style So, when you go to AI and say, "Create me functional style code." it's going to throw something at you. But is it a good code or is it a bad code? And the answer is who knows? It's non-deterministic. It could If you're lucky some days, it gives you good code. And most of the other days, it may give you code that's not really good style code.
So, what that means is we have to be able to evaluate and say, "Hey, that's not a good code. Go try again." Or "That's a reasonable code. Good job, AI. You wrote it." And that's one of the reasons we need to really understand what a good quality code is and what is not. So, we talked about some of these tenets. So, how do we go about creating
functional style code? So, we want to walk through a few exercises and we're going to see how we can create functional style code and we're going to along the way evaluate whether that's a good code or not. So, let's start with our very first example here. So, given a list of numbers, return a list of numbers where the list contains a number before and a number after
each of the numbers in the input. So, given some numbers, give me a list where this list is going to contain a number before and a number after this given number, right? So, for example, if I say seven, let's say 12, and let's say 21. The result should be a six and a eight, a 11 and a 13, and a 20 and a 22. Fair enough? But,
of course, anytime you describe a problem, this is where our life comes in, right? Because descriptions don't always convey all the details. Now, we're going to ask the question, "Well, wait a minute. What if my number was 12 and 14?" Do you give 13 once or do you give 13 twice? The statement is not clarifying that. This is why we have conversations so we can determine what
it should be. And if you don't clarify it, surprises come up later. So what do you have to typically do? You Well, let me say what do you typically should do, right? What you do it depends on what random stuff we come up with. Some programmers like, "Oh, don't worry about it. I got this figured out." And they will do whatever comes to their mind. And you
say, "Well, what about the business? What about the users?" And they are like, "We don't care about users, right? If it doesn't work, they will tell us eventually we'll fix it." But others, "Oh, wait, wait, wait. Let me go talk to the business. Let me find out what they want and what the needs are and I'll code based on that." So that's something we need to do.
But let's ignore that for a minute. So given a list of return a list of numbers where the list contains these numbers before and after. How are we going to do this? So I'm going to go ahead and start in here. Let's go to Claude. For example, you can do this from your IDEs, but I'm just going to do this from the browser. So using So in
Java, right? So I'm going to use Java. Given a list of write a function, right? So write a function that given list of numbers returns a numbers before and after of the given number. So I'm going to ask it to generate it. And while it's generating it, I'm going to go to ChatGPT as well. I'm going to ask the same exact question for from here as well
and say go do that for me. Let's take a look at what it produced. So let's grab this code. Let's come back here and and see what it wrote for us. So, it said, "Hey, I've got an array list and a list right here. And and I'm going to bring those two And then it says, "I am going to uh you know, expand with neighbors." Hey, nice
name for a function, right? Expand with neighbors. And then what does it What does that function actually do is the question. So, that function says, "I am going to take a array list and do this as a for loop." You're like, "Oh, duh. I forgot to mention, right?" So, I'm going to go back and uh can you write that in functional, right? Uh functional style. So, I'm
asking it write that in functional style. What does this do? That also that did imperative style. And I'm going to say, "Uh can you write that in functional style?" I'm going to ask it. But while it's doing it, here's a question for you. Why it did it write in imperative style? Didn't it Didn't it know? Venkat was saying imperative style is more complex, functional style is simpler,
has less complexity. Anyone wants to guess? Why What's that? >> Data. >> Because that's what most people did. That's the We need to understand what AI does. The AI doesn't have a quality judgment. The AI doesn't say, "Dude, this is easier or better." It says, "That's what most people did. That's what I've learned." So, the more data skews on one side, that's what it is going to
tell you. That's how AI works. >> [snorts] >> And it once it we It's important to understand that AI doesn't tell you something because that's better for you. AI tells something because that's what it's been trained on. AI only cares about one thing. Itself. Not you. It doesn't say, "Ooh, let me care about the user. I will give you less complex code." AI is like, "That's what
I learned. I'm going to give you what I know. I'm not going to think about what's better for you, right?" So, essentially it gave you that because And this time around, this is why, folks, you write the skills file. What do you do in the skills file? You're telling in the skills file, "Hey AI, they're going to do all of this, but I don't want you to
do that. My skills file is going to say, 'Where possible, give me functional style code. create code with more immutability. use really good variable names and method names. If you don't say it, what is it going to do? It's going to create variables with single letters. It I'm surprised and thankful that it didn't do here. Otherwise, this would have been A, B, C, and D, right? And
and how do you feel when variable names are completely meaningless? So, you need to put the skills file and say, "Don't give me stupid variable names. Give me nice variable names." Why? Because it's learned massive amount of poor quality code. That's the reason for it. So, in this case we're saying, "Go ahead and give this to me in a functional style." And guess what it did? Look
at this beautiful thing it did. It "This is going to be a function uh where I'm going to use a stream, as you can see here. So, there comes your stream, and then it says, in the stream, I'm going to return numbers.stream, flatMap, stream of n minus 1 and n plus 1, and to list. Question for you. Is the score correct? No. Why not? Because notice it
included You said before and after. It's like, yeah, I'll give you the number as well. Like, no, that's not right. So, you need to also look for correctness. let's give credit where it's due. It figured out this is a flatMap problem. With no disrespect, how many of you, when you saw the problem, and it should be written in functional how many of us here that is a
flatMap problem? Anybody? A few of us. Not many, right? Because that is not something we quickly identify. It requires a little bit more experience with functional style before you think of a flatMap. But, AI, to its credit, really identified this as a flatMap. It's not going to do this all the time. the sun is out, the weather is nice, and so AI said, "Hey, this looks like
a nice day to go write some good code." And I did that. So, it's inconsistent, but this time it worked out really well. Even though it was not correct, it is close enough, and it brought us to that point. That is our Claude, if I'm not mistaken. Let's see how our friend ChatGPT is doing. ChatGPT said, "Ooh, look at this. Before and after." and the before and
after function is doing that job. It said, "Let me apply the SLAP principle." I think it's listening to me, right? Oh, Venkat said SLAP. So, I'm going to write this function as a separate function. And and wrote that extra function. It doesn't have that bug the other code did. Uh and you can see it produced that. It even gave you a little example to try Now, you
can see the code is not all consistent. This is one of the reasons I often recommend try to use at least three different models. Because they're not consistent, but when you look at a couple of different solutions, you can converge towards something that is better than the other. So, in this one, it did a fairly good job in my opinion. Uh when you ask it to write
it flat map, it it did a good job. So, kudos. I like what you did. Thank you for that. Next one. Given a list of words count the number of words that start with the same letter. Uh for example, given Sam, Paul, Peter, Bob, Bruce, one starts with S. Three starts with P. Is it right? Did I make a mistake? Is it three or two? Two, right?
And two starts with uh B. Similarly, this one, they are two and two. So, you can say given a a list of words, right? So, you can say given a So, we're going to say, uh you know, you in Java, you could say, "Write a function that when counts uh oh, and then of course, uh write the function in functional style, right? So, we could say uh
So, we could say in Java using functional style and you can tell that to do it. So, let's go back to Claude asked that question. Go back to chat GPT asked that question. Let's see what Claude tells It says, "The key is um Well, that's old one. Let it expand. Let's see it run. Let's see what chat GPT does in the meantime. In this case, it says
Here is the code. Uh and and it says is an example, apple and banana, bat and cat. And A is two, B is two, C is one. Good example. And what does the code do? Let's take a So, here's the code that chat GPT wrote. So, it says by starting letter and then words. And it says, "Words start stream." And what does it do? Filter W arrow
W. And this is where you could say, "Please don't use single-letter parameter names. Use good variable names." That's what should go into your skill. Otherwise, it'll be X, Y, and Z, right? So, W where W is not equal to null and W is not equal to empty. It says, "Grouping by" and it says, "W character to lower case." And then W.charat and collectors.counting. So, this is one
other problem you're going to see with AI. will write more code than you need. Why does AI write more code than you need? Can anybody tell me? Because programmers write more code than they need. Would you agree? Raise your hand if you think programmers write more code than than they need, right? There are two problems with humans. We eat more than we should eat and we write
more than we should write. If we reduce what we eat and reduce what we write, world will be a better place. And so, humans write more code than we are supposed. How many times are they being called? Hey, why are you writing all this code? Ooh, I want to make the code extensible. Extensible for what? I don't know, but it's extensible. So, we write more code and
AI says, I guess that's the thing I should do, too. They all wrote more code, I'll write more code. That's one reason. Another reason, why do we write more code? We are like, if you write more code, we can impress more people. If you write I mean, this is true, right? You are you are you are creating a design, you're writing some code. You go to your
colleague and say, can you review this, please? And your colleague looks at it and says, Yeah. It's simple. It It works. And you feel let down. You're like, give it to me. I'll be back tomorrow. And you write this code in the most complex way. And they look at it and say, "Wow!" Does it work? Yeah. This is amazing. Right? I don't know how this works, but
it's Wow, I need some time to think about it. You're like, "Hey, this is great." And then guess what? I love interns. You hire an intern and they come and work with you in the summer and they go back go back to college and you got to go with them and listen to what they say. And they go back to campus and say, "Hey, how was your
internship?" "Oh my god, I'll tell you, the people I worked with they are amazing. I don't understand anything they do." That's what we take pride in, right? You create a complex thing, that's job security. Right? They're like, "Go away, you take away. Don't touch John. Why? Because nobody understands what John is doing. What if what he's doing is important, right? This is the way we create job
security. The problem is one day you don't like the job, but you're stuck with it. So so the point really is we have ground complexity into systems. We threw a lot of code and poor AI says "Gosh, just like you try to impress people around you, I've got a world full of people to impress. So I'm going to write you all that code." And you look at
this and say, "Ooh, this is awesome. It does a null check. It does a check for uh you know, empty. It even converts to lower case. Isn't that awesome?" And like, "Does the requirement say any of that?" "Who cares, right? Would you like some french fries with it, please?" So the point is it throws these things to you and you need to really ask, "Do I really
need it?" Well, let's see how our other friend did in the meantime. So here is Claude. And what does Claude say? Claude says the word is not empty, group by the character and counting. What do you think? Which one is better in this case? Did Claude do a better job or ChatGPT do a better job? Claude, right? So Claude rocks. Let's go to the previous example. Which
one did a better job, Claude or ChatGPT? ChatGPT. What did we learn from that experience? They all are good and they all suck. There's no consistency, right? That's exactly what AI is. It is enormously powerful but grossly inconsistent. So, that's one of the reasons you try different models and you pick and choose which one of them is better and then you iterate on it to go towards
something that is acceptable. But if you don't know that and if you don't have the patience to evaluate it, the first garbage you receive is what gets shipped. And this is one of the things to be very careful about. So, so essentially we need to evaluate the quality of what it produces. So, in here given the word, it's removing the empty words and in this case, I
don't see why it needs to remove the empty word. That's But characters grouping by given the word, give me the word a letter the first letter and group based on that. And what do I want for each of the group? I want the count, which is exactly what it's going to produce for So, let's grab this one because that's a better quality code after all. So, let's
go over here, uh paste that code. So, this is the function, pardon me. So, that is the function it wrote. So, I'm going to say here comes my sample uh class and I'm going to remove all of that for a minute and I'm going to simply say here is the is the function I'm going to create and let's just take these three, put that in here. And
then this is the uh count function that it wrote and we can then say, "Hey, I want to really call that and see what it does." So, let's output Oops, pardon me. So, we're going to say this is uh let's say in here So, here is the main function. And what are we going to do in the main function? In the main function, we say uh uh
you know, output uh count words by letters. And it takes a list of words. So, list of let's say here is a Sam, let's provide Paul, let's provide uh Sarah, let's provide let's say uh you know, Peter. Uh let's provide let's say Shiva. And let's provide, you know, Ralph. And let's ask for the result of it. So, P is two, R is one, S is three. It's
doing the, you know, right job to give us the results. But of course, you may want to write enough unit test and verify it's doing the right job. You can ask AI to write the unit test and then review it as well. So, that way you can you can look at what it's doing. But the key here is this. You need to really be the person examining
and verifying if what it is doing is good or really terrible. Okay. So, let's go a little further now. Given a word, find the first letter that is repeated. So, given a word, So, in Java using functional style, finds uh sorry, given a word, finds the first letter that is repeated. So, if I go back here to Claude, ask it to write go to chat GPT, ask
it to What let's see what it writes. So, it's thinking about implementing clarifying definition of the first word repeated And then it's going through some notion. And here is the function and it wrote our very first function. So, right there is the very first code we saw it, right? So, let's go ahead and put put this function here. So, so here is let's say here is our
public class, let's say a sample. And within this class, we have this function it wrote in here. And then once it finishes that function, right? So, that's the return result. And let's go ahead and write our main method. And what do we do in the main method? I am going to say this is the first repeated letter. So, first repeated And what does the first repeated do?
I've got a little example here to try this out. So, this is saying All right, for our first repeated uh for our first, pardon me, repeated we're going to use that function right there. So, let's go ahead and say that's my first repeated letter, right? So, we're going to use this one in here. But what are we going to do with it? When you look at the
code, it creates a hash map, cha- characters map to object, filter, add, and find first, and return this. Fantastic. Ship it. is something bothering you? It's checking all the letters, you're saying? Can you look at this code? appreciate this code? Or would you scream if it you saw one of your colleagues writing this? You should be able to jump out of your seat at this point. You
should topple a few chairs and say a few curse words at this point. What's so wrong here? What this is This is breaking a tenant. Something very evil. What is it that it is doing you should never do? A filter is a read-only operation. Who in the right mind mutate data in a filter? Think about it for a minute. and if you do this on my team
people will notice that I walk out with you to the parking lot and they will never hear or see you because it's such a crime to write a code like This is insane. Mutating data in a filter. Ugh. Unbelievable. If this is the code you want to comment into your repository you don't need to be a programmer. No, you don't have to be a programmer. this is
completely not understanding what this garbage is. That's the key. You must be able to look at it. The smell of this should be so intense that you cover your face. Like, my gosh. This is bad. That is the key. that is one of the things that is very scary, right? When you use AI to generate code, where it dragged this code from. It's a bit scary. Somebody
out there wrote it. The only good news is, I don't know who that person is. And and they are happy that I don't know that is something to evaluate. Let's take a look, right? Let's go back the good code that you wrote? Huh? Let it answer. Let's come back here. What is this doing? I have um a word stream, filter that out, grouping by. No, is that
what it is does? Why is it grouping by? That's a count No, that's not the function. Sorry. This is the one. Find repeated. What is this doing? grouping by, identity, link, counting, entry set, filter, What is your instinct? What is your gut feeling? This code should be framed and put into a pistol a ped- and you have to look at it every day. Will you take this
and show to Mom? Mom, look at the code I wrote today with the help of AI. Will Mom invite you into the kitchen? Will she say, "My child, this is fantastic work you have done today. Here is some more food for you." Would that happen? Would you do that if your child comes and says, "That's the code you wrote." You'll still probably feed the child, but you're
not going to appreciate, right? And say, "I'm you're making me proud." Will you say this? You're going to say, "Child, hide that, No, this this should be hidden from humanity. Nobody should see this. Because if people see this, this is death to the field of programming, right? Because you look at this code and what is the gut feeling? You're like, "I don't know enough, but I can
tell you this is wrong." Because this looks too complex. So, this both the tools failed miserably. This one failed less in my opinion. At least it wrote a complex code. That one was violating the tenets. While this is happening in this world, let's quickly go to the other world. What is it saying? Honestly, wow, isn't that great? We are not now being honest. By the way, this
is basically lessons I've learned in my life. When somebody says, "Let me be honest." I'm like, "Why would you not be?" I don't understand. Let me be honest with you. Like, dude, why would you not be? Why am I talking to you so long? When somebody says, "I will give you a simple solution." You lost my trust already. Never trust a library that has a name simple
in it. Never trust a a a a a know, class name that has a symbol in it. Every time here's a simple way it's Let me honest about it. What is your honesty? Honestly, both versions have noticeable flaws worth calling out. Calling out, you did it. The hash version works, but it violates and the most honest answer. Woohoo, Is that the problem is a poor fit for
Java. Oh my goodness, I always bring That's fantastic, right? The problem is not me. It's also a really bad language. I have to wait in the corner, So, a Java's functional style, it's inherently stateful. No, it's not. Sad. Really sad. And more sad. You can say, really? You want to blame than your lack of knowledge? What was that? I'm like, damn. Because it sucks. >> [laughter] >>
So, what does it say? You're right. That's a fair criticism. now you know. This is about comedy at work. You're like, you know what? My programming job has become boring. I've been writing code like this and they said we'll give you AI and now you can just have some laughter at work, right? You can do this and you know, look at what it did. And now you
can say okay, thank you. Let me get back to The problem here is unless you are knowledgeable to know you're going to go people and say, yeah, Java is not the right language for this. Why? Because AI told me that. The point really is it's a powerful tool but extremely inconsistent, right? So what's happening here? Look at that. word.characters if the index is not equal to last
index It is almost correct. You know what it did a few minutes ago? Java cannot do this, folks. Java cannot handle statefulness. It gave you all these lies. And you're going home thinking, oh my gosh. This language is not going to work. I got to go tell my boss we all need to move to Python. And next thing you have in the conversation, we are moving from
Java to Python. Why? Because AI told us that this is not solvable. And look at what it did. It's almost correct. You don't need You can just use a stream of. You can reduce the complexity. And all he had to do was to check if this letter is repeated more than once. And how do you know if this letter is repeated more than once? Check the last
index. Check the index. If they are the same, it appears once. If there's more than if it's not the same it has been repeated at least once. Simple logic. How did it know? Because it's seen this code also. But what does the inference engine do? It rushes in, finds the first thing, and boom, here you go. And you're like, that's not a good code. Oh, here you
go. But that's not a good code. And it didn't say here you go, it said, oh, the language is bad. Oh, you're the statefulness or statelessness. But if you're not qualified to assess you're going to go with what it says. This is the point I want you to think When you are at work, when we are told, just just just worst word, just just use AI. No,
I'm not going to just use AI, because I need to know what it is creating. And if I'm not knowledgeable, and if I'm going to ask it to create you created something that's completely a nonsense. And how can you rely on it? So, yes, it's a very good tool in the hands of someone So, I want all you to leave with a few thoughts here. Use AI
for ideas, not for solutions, because it's not reliable. Second, the more examples we can give, the better it is. Third, just like what we do, make it conversational with AI. Just don't say, give me an input, give me an output. be very careful. It learns from your code. You write bad code, it writes more bad You write good code, it writes more good Scary. It reflects what
we do. Five. The problem is not that AI has not been trained. The problem is it's been trained without any indication to the quality of what it's been trained on. Garbage in, garbage out. Six. It is really great about analyzing code, but the quality of the code is very questionable. That is something for us to keep in So, evaluate it thoroughly. Seven. AI is a tool. And
remember the good old saying, a fool with a tool is a dangerous fool. And that's one of the things we have to I hope that was useful. Thank you. >> [music]
More from this event
See all 126 talks →
AI Is Not the Risk. Architectural Drift Is - Sunil Kalkunte
17:39
Breaking the Monolith: Tesco’s Journey to Federated GraphQL with xAPI - Vishwas Chandrashekar
29:13
A Practical Introduction to LangChain4j - Venkat Subramaniam
1:01:28
Beyond the AI Models: How Lowe’s is Building the Store That Knows - Swaroop Shivaram
13:59