I want to talk quickly about the idea of clean coal, 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 for even as a function that takes in a number and we're going to hear you say, well, how are
we going to check?
We can use the modulo operator, I can say if no 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.
And if it's not an even number, so let's say now.
To.
Doesn't equal.
Zero.
So this is an odd number, there's always a reminder 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 there because I have to make sure that I do an L if statement here.
We have to print here so less to print.
And click run.
I get true if I do fifty one 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 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 the 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 EL 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, all right, but we can make this even cleaner, right, because right now we're just
returning true or false.
And usually if we 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.
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 in the next Bhabhi.