Okay, so let's start a little bit earlier today so that we can catch up also with the
exercise. You're all recorded again, just to let you know. Remember recursion from last
time, right? Last time I made a mistake here and just didn't call the function. That's
why it didn't work. Thank you for telling me. Yeah, that happens. That's a fact. So
what will happen in this code? Any ideas? Sorry? This code, so it's the function that
takes a parameter here. It prints, I'm here. If X is zero, it returns and does nothing.
Then it does X minus one and then it calls the function again. What will it do? I have
a base case here. So, okay, let's do induction here. Oh, no, let's just do reasoning. That's
probably easier here. So I start the function at some value, then every time the function
is called, that value is reduced if it's not zero already. And it goes down every time
I call this function by one. And at some point it will be zero and return, right? So what
will it do? What do you think? So I call it with five as a parameter. I am here. Four
times, five times. Yes, we are not doing indexing here and it's also starting like that. So
I do it here. Means I go five, four, three, two, one, zero. And I print exactly, I print
it before the return statement, before the base case. So it does it actually six times.
Yeah. So this is also something to be aware of. It depends where you do it. You need to
be careful. Now, can you break my code? How would you break this code? Make it do something
unexpected. Minus one. Minus one. Good idea. Okay. So I call that thing with minus one
now. And you just found a bug. And you get a recursion error at some point. Apparently
something went wrong here. Maximum recursion depth exceeded while calling a Python object.
Oh, wow. Actually, that's cool, right? In a Jupyter notebook or a Google Collab, I can
directly Google for it. Here. What does this mean? Okay. Some example code. Okay. Apparently
Python ends any recursion 999 course and you can set this recursion limit here. So you
can change this if you have a recursion that needs more. But apparently I have a problem
with my recursion. So I would need to add more tests here. I can scroll up a thousand
times I'm here. So I will need to make an assumption here. Test at least that this X
is positive as an integer because apparently when I program this just before, I assume
X will be a positive integer. With this one, right? I think somebody is discussing this
float. What will this do? Let's see. Sometimes best is just to try. Okay. It works with a
float apparently. But I did not, I probably did not plan for floating point values here.
So I would also need some mechanism probably to tell a user of this function that I'm expecting
an integer and that integer should be positive. And yeah, it should work with a float. But
so let's try another float here. See what this does. What do you think? No, no. It again
broke my recursion and going a thousand times through I'm here. Why do you think this is
the case here? Because it's not zero. Sorry. One by one, one by one. It won't be zero.
Yeah, very good. So it goes like five point six. It's not zero minus one minus one minus
one. At some point it's at zero point six. Right. And that's minus zero. Oh no. And it
stays afloat because I haven't defined this here anywhere. So I just found another back.
It works for floats, but only if you don't have something behind the decimal. So there
are lots and lots of ways to break your code. And as a programmer, you should actually do
this. You should try and break your code. And that's also what brings me to this week's
topics. It's just one of them. And it's, I could, I could bring this program at any point.
Just do it now. Because now you're in already in a position that you can write your own
programs. Somebody's on the train. So your programs might not be printed, but they might
do a useful thing. That's totally okay. But at that point you'll encounter what we know
as computer scientists is bugs. And there's a nice analogy. So first of all, you have
probably already realized that computer scientists like kind of weird expressions. I mean, why
is it called a bug? I don't know how familiar you are with computing history, but in the
olden days, maybe 80 years ago, so computers were hardwired things. Every program was hardwired.
So this was filling rooms and required a lot of space, a lot of soldering and a lot of
Presenters
Zugänglich über
Offener Zugang
Dauer
02:55:49 Min
Aufnahmedatum
2022-11-28
Hochgeladen am
2022-11-28 21:46:04
Sprache
en-US
debugging and good code practice
start with object oriented programming