-: I wanna 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 here,
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 num, two, doesn't equal, zero.
So if this is an odd number,
there's 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
elif 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.
Alright.
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 gonna 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.
So, alright, so we cleaned up our code a little bit.
But here's the thing.
Another thing we could do,
is we know that return exits
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 five,
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.
Alright, 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'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,
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, alright,
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.