stop using for loops RIGHT NOW!!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys it's been a while what's going on guys assalamu alaikum welcome to amigos code in this video i want to talk to you why i avoid using the for reach or loop through lists when i'm trying to map from one daily time to another now have a look at this example here right so we have private set get authorities so in here we have a set which is initially to empty then we get the roll or it should be get rolls by the way in here we go for each row and then we add to the set in here and then we return the authorities now don't do stuff like this right because this is actually defining every single step of the implementation details right so this is the imperative approach of solving problems and whenever you're working with you know lists sets maps you know try and avoid stuff like this now there is a better way there is a better way right so let's let's just improve on this and you'll see the benefit so here instead of us doing all of this and then looping yada yada we'll do this we'll basically say we want to get the user dot and then get row and by the way i need to rename this so just bear with me so oops let me just rename this so if i go into get roll and [Music] okay cool this should be rolls right so f6 rows and go back this is much better now so what we're going to do is the following we're going to say user dot get rows and then here we're going to say dot and then stream dot and then map so here we're going to map the row to so basically what you've done just in here right so just like that so let's just take this command c and then paste it in and then we're going to say dot collect and then collectors to set so this is your return type and now let's just return this and have a look so this is much neater to be honest right so here you can see that we're not having to create a new empty list and then specifying how we're going to loop through it and then add to that oh actually was a set and then add to the set and then at the end return the set so you can see that this is just requesting what we want instead of defining how we want things so try and use declarative approach it's much neater now let's have a look at the example so this also is very simple so if i open bookmarks and then i think it was comments there we go now here the same thing so private void check for forbidden words and then you've got um basically forbidden in here so maybe this should be i would say private final static right so let's just make this as a constant so here we factor and you can introduce a constant for this so forbidden and this core words just like this there we go so then this actually is instantiated once instead of you basically instantiating this list over and over again whenever this private method is invoked yeah so here list forbidden words have a look again for and i so we are defining how we want to loop through this then we specify the initial value for i and then here sometimes is it less or less or equal even myself sometimes i i i i get confused by this right and you can up in situations where you have um index out of bounds exception for example then again here if and then forbidden get and then i so you can see how you are specifying all the implementation details right so instead let's swap this around and not use a a for reach or a for loop and we're just going to tell it what we want that's it right so here forbidden words so here again i think this is you don't want to do this right because it's already an array that you have right so here let's just basically say forbidden words dot and then stream so arrays as stream dot and then any and then match and we have the word so word and basically this is your contains right so here um i think is it even contains yes i think basically what you're doing is yeah so you're saying it contains there so let's just take this contains so word dot contains and then instead of saying forbidden here we just say s in here right so maybe let's just say f w for forbidden word so it's much easier to read and there we go so now this gives us a boolean so let's just say any match and basically if in here any match then you can throw so here you could say if any match then you throw this um exception in here that you've done so have a look so this is much neater right so you can see that we're not basically defining all the implementation details and whatnot but we're just telling it basically what we want so please try and and and and start thinking about how to code like this now the cool thing here is if you want to basically revert this there's a non-match but i think not much for for this implementation wouldn't make sense but in generally i try not to use you know all the for each or four i unless i have to do so because sometimes you might need access to the next element or the two next elements or the previous two elements on it so forth so that's when i i start to think about using for loops but even then as well sometimes you can use an iterator as well right so basically try and use this as much as you can and luckily for you i've got a course on my website which teaches you everything that i'm i've just explained and how you can move away from imperative to declarative programming so if you enjoyed this video give me a thumbs up don't forget to subscribe if you haven't done so and if you're not part of the amigo squad community go ahead and join now and yeah that's all for now i'll catch you on the next one assalamualaikum
Info
Channel: Amigoscode
Views: 114,566
Rating: undefined out of 5
Keywords: amigoscode, java, for loops java, java streams, clean code, clean code java, code refactoring, code refactoring java
Id: -640IYSEnVE
Channel Id: undefined
Length: 7min 6sec (426 seconds)
Published: Mon May 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.