Applying Code Style0:07
All right, we've learned three of the 10 practices for writing more readable code. That's applying a code style, removing dead code, and avoiding nested code. With just these three practices, we can actually achieve that better code sample we saw in the introductory video. So let's do that using these practices. Alright, going back to the sample, the first thing we wanna do is apply a code format. Again, I'm gonna use the Laravel code style, so
the first thing we wanna do is apply a code format. Again, I'm gonna use the Laravel code style, so I'll jump out and run vendor/bin/pint and apply it specifically to this file. Great. It's done. If we jump back to PHPStorm, we'll see that we've automatically applied a conventional code style. Okay, next I wanna check for any dead code. Right away we see PHPStorm is highlighting this break and we've talked about it before.
Removing Dead Code0:53
Right away we see PHPStorm is highlighting this break and we've talked about it before as being unnecessary syntax. So I'm gonna go ahead and remove it. And we actually did this refactor while removing dead code. We learned also in nested code that anytime we see these raw boolean return types, we can simply collapse them and return the condition. So let's do the same thing here. I'll drop this in, remove the if structure.
So let's do the same thing here. I'll drop this in, remove the if structure and we'll see phpstorm is not only highlighting the break statement, but again it's highlighting this as an unreachable statement because all of our cases for our switch now return a value. So let's go ahead and get rid of that. Alright, at this point, I don't think there's any more dead code necessarily, but we do still have a lot of nested code.
Reducing Nested Logic1:40
I don't think there's any more dead code necessarily, but we do still have a lot of nested code. And I wanna start at the top level here with this. If F, because we're returning true, we don't need the else. Again, this is gonna relinquish control. It's going to return from this method. So any code below it is unreachable, provided that this is run. So let's go ahead and remove this L statement.
Evaluating Match Expression2:02
provided that this is run. So let's go ahead and remove this L statement and its closing bracket. I'm gonna jump back out and just run this again. Alright, that should bring everything in line. Okay. Now PHPStorm is guiding us. Again, with this refactor, we can actually convert this to a match expression and I may or may not wanna do that. That part's rather subjective, so let's allow it to go ahead and automate it and see what it looks like.
That part's rather subjective, so let's allow it to go ahead and automate it and see what it looks like. It definitely feels tighter, but remember, just because it's less code doesn't mean that it's necessarily more readable. In this case, this match statement could come off a little bit dense, particularly with this private case compared to the other two, it's much more complex. Again, some may leave the code here, but because of this guard clause, part
Again, some may leave the code here, but because of this guard clause, part of me feels like continuing on with this style of code that is using simple if statements. So what I'd like to try is actually to unpack this match statement and turn it into if statements. Let's see what that looks like. So we'll say if $scp is equal to public, then return true.
So we'll say if SCP is equal to public, then return true. If SCP is equal to private, then we will return this particular expression and finally the default will be returning false. Again, let's jump out to the command line. We'll reformat that. Make sure our coat style is applied consistently. Now I like this a little bit better, but there's one more thing I would do here.
Improving Return Symmetry3:44
Now I like this a little bit better, but there's one more thing I would do here. This is actually a practice we'll talk about at the very end called symmetry. But notice that all of the guard clauses return true, and ultimately, if I get down to the bottom, it returns false. Again, to allow our brains to be lazy. It'd be nice if all of these return statements were simple ends, and I can achieve that.
It'd be nice if all of these return statements were simple ENSs, and I can achieve that by making this if statement a compound conditional or taking the return value and rolling it back into the condition. Then returning true if it's met. Now, this is where I would leave the code. I feel it's very simple, explicit, and easy to scan, all of which to me make it much more readable.
Reviewing Refactor Options4:28
and easy to scan, all of which to me make it much more readable. So we've seen three different versions of this code, all of which make it more readable. Which version you stop at is ultimately up to you. But all of them demonstrate how we can apply these first three practices to achieve much more readable code.
