I want to talk quickly about the idea of clean code, something that we've talked about before, and
this is a good example to do with something like a function.
For example, let's say we had a function that perhaps checks if a number is even or odd.
So let's say.
Is odd or even as a function that takes in a number.
And we're going to hear say, well, how are we going to check?
Well, we can use the modulo operator.
I can say if.
Number modulo two, which remember, gives us the remainder.
And if it's an even number, the remainder is always going to be zero.
So this is going to tell us if it's an even number.
So if it's an even number.
Then I want you to return.
True.
And if it's not an even number, so let's say numb.
To.
Doesn't equal.
Zero.
So if this is an odd number, there is always a remainder.
In that case, we return false.
So maybe let's change this function to be more descriptive is even so, it's going to return true.
If it's even false.
If it's not even.
So in here, if I do is even and give it 50 and I run, oh I get an error because I have to make sure
that I do an L if statement here.
We have to print here.
So let's do print.
And Click Run.
I get true if I do 51 and click Run.
I get false.
All right.
It seems to be working, but how can we clean up this code?
Well, here's the thing.
We have some ways that we can actually make this a little bit tighter.
For example, if we're only checking for is even, we don't really need to check this again because.
If number modulo two doesn't equal zero, that is, if a number divided by two doesn't always have a
remainder of zero.
We always know that that's well, it's not going to be even.
So we don't even need this statement.
We can just say else.
Right.
So that if I run this.
This still works.
Sorry.
So we cleaned up our code a little bit.
But here's the thing.
Another thing we could do is we know that return excess the function automatically.
So we don't even need the statement.
I can just say return here because it's only going to get to line five if the statement is not correct.
So if it's not even well, I'm going to skip line four and go to line five so that if I run this.
This still works, all right?
But we can make this even cleaner, right?
Because right now we're just returning.
True or false?
And usually if you have something like this where returning true false directly, there's usually a
better way and that's using an expression that evaluates to a boolean.
What we can actually do is simply say, Hey, remove all of this and I want you to just return.
Like this.
And if we run this.
There's still works.
And if I do 50.
This still works because what we're saying here is I want you to return whatever this value is.
And this is an expression rate that's going to evaluate true false directly.
So we get the exact same result as we had before, but look how much cleaner that is.
That is just nice, simple code.
And as a developer, I can come in here and right away see that?
All right.
Is even is just performing this operation and returning true or false?
So this is a good way to think about clean code.
How can you clean up your code?
And mind you, there's no perfect solution.
You can always clean up code, but thinking like this really allows you to develop the way that advanced
programmers think.
I'll see you in the next one.
Bye bye.