تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Revisiting Loop Refactor0:07

I worry I may have skipped over refactoring loops a few times. Now, I mentioned them way back in avoiding nested code, but they're also a relatively big block of code. Some might have disagreed with me removing the comments in the last video, which summarized what the loop did, so let's revisit that loop and the TwitterClient and also apply the other practices to refactor it even further.

and also apply the other practices to refactor it even further. This is the loop where I removed the comment and my reasoning was that most php developers should be able to spot this as an accumulation loop or something that builds up recent tweets, but that might be an assumption. We could refactor this to improve its readability using our other practices to get started.

Applying Proximity Rule0:50

to improve its readability using our other practices to get started. We could treat this as a big block of code. Now, before doing so, we should move any related code next to it. This practice might exist in programming, but I call it the proximity rule. As programmers, we're often taught habits very early on. One of them is to define your variables at the top of a method, but in doing so, they're disjointed from

One of them is to define your variables at the top of a method, but in doing so, they're disjointed from where they're actually used. This may make the code hard to read or at least hard to follow. It adds to the mental overhead as we have to carry those variables in our own memory until they're actually used. By moving them closer, we're able to see the whole picture and read everything together.

By moving them closer, we're able to see the whole picture and read everything together. Let's do the same for the four foreach loop. We see that it uses tweets, which is defined immediately above. It also uses yesterday and recentTweets, both of which are defined much higher up. So let's apply the proximity rule and move them closer to where they're used. We'll throw on a line break here.

and move them closer to where they're used. We'll throw on a line break here. Now, even just doing this might make it more obvious that this loop is for accumulation. We start with an empty set of recentTweets and we add to it within the loop. This alone might make it easier to see what we're doing, but it's still a relatively big block of code. So we can ask ourself the two questions. Can this be done in a native way

Replacing Loop with array_filter2:18

So we can ask ourself the two questions. Can this be done in a native way and does it belong at this level? As I mentioned, a nested php. PHP comes with dozens of array functions. In this case, we're filtering tweets to those that are after yesterday, so we can use PHP's built-in array_filter function. It takes the tweets or the collection as the first argument, and the second argument is your filtering callback.

It takes the tweets or the collection as the first argument, and the second argument is your filtering callback. In this case, we can use a short closure and take the condition from the if statement. Now array_filter returns the filtered array, so we no longer need to default recentTweets. We can just assign that directly and get rid of the foreach loop. Oh, let's not forget our local variable for tweet. Now, I feel using a native function like array_filter would

Oh, let's not forget our local variable for tweet. Now, I feel using a native function like array_filter would allow even more php developers to read this code and immediately know what it's doing. But if they didn't, they would see that this is a native function and have all the resources available to learn more about what it does. That's an important side point. While these practices are all very fundamental,

Readable, Not Simplistic3:33

That's an important side point. While these practices are all very fundamental, it's not about coding for the lowest common denominator. Said another way. The code should be readable, but doesn't have to be simple. So long as the code's readable, you should feel free to use new features or patterns. Doing so will allow you as well as your fellow programmers to continue to level up getting back into the code. I think we could leave it here,

Using Carbon for Dates3:54

to continue to level up getting back into the code. I think we could leave it here, but this date comparison feels relatively complex to me. Again, we could treat it like a big block of code. Is there a more native way to do this? Well, there's not in php, but there is a popular php library called Carbon. We could use that to parse the date and then see if it is after yesterday. Now, let's actually import this.

after yesterday. Now, let's actually import this. Now, I do think this helps, especially if you're familiar with Carbon, but even if not, we can see that we are parsing the created_at date and ensuring that it's after yesterday. Now that we're using Carbon, we could actually get rid of our own yesterday variable and use Carbon. It comes with a helper for yesterday. That would allow us to remove our yesterday variable,

It comes with a helper for yesterday. That would allow us to remove our yesterday variable, which also has a relatively complex value. Again, most might leave it here, but since we are immediately returning recent tweets and we've already removed the other temporary variable within this method, we could directly return the result from array_filter. This is probably where I would leave the code currently, but to foreshadow a future practice, we could imagine

Extracting to a Method5:19

This is probably where I would leave the code currently, but to foreshadow a future practice, we could imagine that within our Twitter client, this code is used somewhere else. If it were, it may be abstracted into its own method, allowing us to call that directly, and that would yield a final result of our tweets since yesterday. Method looking like this, again, it's not always about reducing the lines of code.

Method looking like this, again, it's not always about reducing the lines of code. What it's about is applying the practices to yield more readable code. That's what we've done here. We've started with a relatively big block of code that was heavily common and lacked some of the fundamental practices we discussed, and we reduced it to four lines of code using native php functions and other methods.

and we reduced it to four lines of code using native php functions and other methods. We abstracted within our code base.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟