Cleaner If Statements


00:05 We've all written code like this maybe when we were a starting developer or

00:09 maybe even recently by proxy,

00:11 by approving a pr because we've realized that all code is shared and none of it

00:14 matters in the long term anyways, but a part of us still wants to do better.

00:19 So let's look at a few ways how we can clean this code up all relating to how it

00:23 handles if conditions.

00:25 One obvious reason how this code is dirty is the fact that our core intent of

00:29 updating the person's experience and age is buried deeply within some if

00:33 conditions.

00:33 One crystal clear cleanup that we can do is that we can combine the first two if

00:37 conditions into a single if condition with a logical,

00:40 and you can do this with any two nested if conditions as long as they don't have

00:44 code outside of the IF block and don't have matching else conditions. Now,

00:49 this is not true for the other two if conditions as they do have else blocks.

00:52 So we will stop our logical combination here.

00:55 Let's focus in on the functions behavior outside of this main IF condition. Now,

01:00 there are no statements outside of this IF block,

01:02 which means that this function will pretty much execute nothing and return well,

01:06 we can code that up as a separate if condition.

01:09 We can invert this condition such that button is not equal to one or if there is

01:13 not a person and do a simple return.

01:17 This kind of returning from a function in case of any excluded conditions is

01:21 known as an early return. And overall,

01:23 this pattern of excluding certain conditions upfront within a function body is

01:28 called a guard clause.

01:29 The other common guard clause is where you throw an exception,

01:33 and we can see that in our next IF condition.

01:36 If the person's age is equal to null, then we simply throw a new error.

01:40 We can take this condition and move it at the root level.

01:43 So we don't need to have the next if ELs block and the next,

01:47 if ELs block follows the same button. That is,

01:49 if the person's experience is equal to null,

01:51 we simply throw an error and we can repeat the same process that we did for

01:55 person age. That is, if the person not experience is equal to null,

01:59 we simply throw an error upfront within the function body.

02:02 And this gives us a neat dented function body where the purpose of the function

02:06 is much easier to understand than it was before. Now as a final thought,

02:10 let's talk about how you have to invert the conditions for your car clauses.

02:14 In most cases, inverting a condition is quite easy. For example,

02:18 if you are checking for person age, not equal to null,

02:20 it's a simple matter of replacing it with person H equals to null.

02:24 And the same is true for other things. For example,

02:26 button equal to one becomes button not equal to one.

02:30 And if you're checking for a truthy value call person,

02:32 then it becomes simply not person. However,

02:36 it's a bit more involved When you have a combination of conditions, for example,

02:39 button equals to one and person. In this particular case,

02:43 you can simply invert the individual member conditions. For example,

02:46 this becomes button and not equal to one,

02:48 and this becomes not person and all the combinators get inverted as well.

02:52 That is all the S would turn into S. And if we had any Ss,

02:56 we would turn them to S. And if you are curious,

03:00 this is something that comes from D Morgan's law.

03:02 And even if you're not going to be using guard clauses,

03:04 just knowing about this particular d Morgan law is going to save you a ton of

03:09 mental gymnastics when working with boo conditions.

03:12 And this is saying the same thing that we just demonstrated that is inward,

03:15 the individual members, and turn an or into an and and an and into an or.

03:22 A key takeaway from this lesson is that whenever you're about to nest

03:25 conditions, take a moment and think if perhaps there's a better way.

03:29 Thank you for joining me and I'll see you in the next one.