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 no modulo 2 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 now 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 I get a narrow 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 no
module or two doesn't equal 0.
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 gonna be.
Even so we don't even need the statement.
We can just say else right.
So that if I run this this still works.
Sorry.
So we've cleaned up our code a little bit.
But here's the thing.
Another thing we could do is we know that return access the function automatically so we don't even
need the else statement.
I can just say return here because it's only going to get to line 5 if this 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 we're returning true false directly.
There is usually a better way and that's using an expression that evaluates to a Boolean.
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 this 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 right 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 room.
Bob.