clean code clean code blog clean code examples clean code principles Digital Marketing tech

Clean Code Principles –

Clean Code Principles -

Discover why clear code rules are important to each exceptional coder. That is half 12 of methods to make a file supervisor software. This time, shall be fixing the whole lot, cleansing the code, optimizing the structure and getting it ready. We’ll run the appliance and begin on the lookout for some field and the way can we repair them. Lengthy click it and long click once more, which is working. We will delete this file, press in yes and the file is just not being updated. We’ve got to create a way to wash all of these code we have now proper here. We’ll go from messy code to plenty of cleaner code.

How can we do it? Let’s start studying the code from the top. We begin with the on Create technique to set in content material view. We’ve some variable right here, which we use to determine the present state of the appliance, which is nice and helpful.

Once we initialize here our file supervisor, we begin by setting the present Path to the downloads folder, which is beneficial and great. Then we go and set the basis Path to the folder mum or dad of the dad or mum folder of the downloads folder. Then we go and set this directory, so dir = new File(currentPath). Right here we should always be capable of name a way that may do these for us routinely without having to worry that a lot about all of this. Earlier than the on Resume technique, we’ll create a way we’ll name personal void replace Path. What this update Path technique will do is will get these dir = new File(currentPath) and these information = dir.listfiles. We’ll transfer these strains of code right here.

We will see we now have the trail output, which is about to present Path, which is nice. The information Discovered Rely which is information.size. This also needs to be referred to as from the update Path. Principally, every international variable that we use to entry the opposite strategies, we have to do this from the update Path. This file Listing proper here allows us to set the info of the new information proper here. We also needs to name these from the update Path. We also needs to name this selection, this proper right here from the update Path. We’ll reduce this and put it into update Path, which can make our code so much, lot cleaner.
Here we’ve text adapter that isn’t working. What we’ll do even higher is that we’ll set these to the refresh button, which will probably be rather a lot cleaner. We’ll copy all of these of the replace Path and we’ll delete this replace Path and we go to the refresh Button proper here. We now have to call all of this stuff from here.
As you possibly can see, if we paste this proper here, we now have so much to repeat the strains, which is great so we will optimize that. Now, the issue with that is that once we name onClick before we set this, we may have an issue. We’ve to to repair that. I’ll repair that later.

Simply to recollect these, we’ve to– We’ll move these earlier than the other stuffs. The first thing we’ll set will be the refresh Button, or not likely because we will set the refresh Button. After we set this, we set the press Listener, we’ll call it. I’ll say refreshButton.callOnClick simply there so we will refresh it. That will probably be fixing our issues.
Contained in the on Click on listener of the refresh Button, we’ll find a strategy to make it easier. We’ll first change the listing with new present Path, then we’ll record the information, then the information rely, then the information Listing. As you possibly can see proper here, we listing the information, then we set the info with the information Record.
The problem is that once we set this new knowledge, we will have the problem we had on the earlier tutorial. Before we set knowledge there, we now have truly to go and set the choice. Before this, we’ll go and say we’ll set the choice. To try this, we’ll go earlier than information Record and paste this choice = new boolean[filesLength]which we will exchange information size with filesFoundCount, which is more efficient. Now we’re going to say textAdapter1.setSelection, setSelection to selection. Then we will go fileList and we set the adapter to all of this. After the set knowledge, right here we will see that we’ve repetition. All of that is repetition.

The issue here is that once we refresh this, this refresh can be referred to as from all totally different elements of the code. We need to make it working actually, rather well. We’ll comment these right right here. These new ArrayList proper right here doesn’t really must be referred to as every time, so we will put these outdoors the refresh button. Proper here for example.
What we truly have to call is we now have to clear the ArrayList. We’re going to say before these filesList.clear. We truly have to clear this. We clear this and it is going to be it. We’ll run the app and verify that all the things works positive.
We’ve here- we’ll delete this– We’re doing numerous modifications right here to make it work better. We’ll run the app to verify if all the things is working positive. Let me see if it doesn’t crash, hopefully. If I lengthy click, if I along click on once more, which is working nice.

Now we’ll return to the code and try to find errors in the code. We now have the refresh button, which principally does all these superb stuff, which could be very, very clear. The go back button, what happens once we tap the go back button? The first verify, it checks if the present path is the basis path. If it’s the basis path, do nothing and simply return. If it’s not the basis path, then go to the currentPath to this code. This line proper right here does simply that.

Here as an alternative of doing all of these dir = new File(currentPath), we will simply name the refresh Button as a result of this already units this. We will also set the pathOutput.setText on the refresh Button. In concept, we will delete these two strains proper right here and we will go to the refresh Button and get this thing of path Output proper right here and we’ll move that line down here. Down right here shall be nice. On the return button, we have now and we set the brand new present path which is the present directory and we refresh. We just change one thing and we refresh. Now we will delete these different two strains proper here as a result of we are doing all of that in the refresh, so we’re cleaning the code even more, so we just are changing the trail and we are refreshing. Once we refresh, the first thing we do is we set the path output to the brand new path, we create a brand new file with the currentPath, we listing the information inside this path, we listing the information, we set the filesFoundCount, we go and set the selection, we clear the selection. We clear the fileList and we get the newFiles listing. It’s great.

Now, once we click on an item right here, we’ve got to know if the item is a folder or not, which we’re doing right right here. Place should never be larger than file’s size. This was only a quick repair so we will delete these strains right right here. If the file is a directory, we’ll change to this new current path, we get file’s position and get absolute path. We’re just altering the path so we will delete all of these strains right right here as a result of all of those is being finished from the refreshButton. We will delete all of those strains and just say refreshButton. callOnClick. We’re cleansing much more our code. On listView on LongClickListener, we are just choosing this. We set the longClick flag to true. This line right here, it says principally if it’s selected, then unselect it. If it’s unselected, then choose it. Now we set the selection to this new selection proper here. We are simply altering the choice right here so we don’t should name the refresh as a result of we aren’t refreshing every little thing however solely the choice. selectionCount proper here to know how many gadgets are chosen, so selectionCount is bigger than zero, which suggests a minimum of one was selected. We verify if it’s one, we’ll clear, we’ll present the rename functionality as a result of we are renaming just one file. If it’s multiple, then disguise the rename functionality however all the time present the bottomBar. If it’s not one, so anybody it’s chosen or if one thing is unselected, we cover the bottomBar. Great.

After this, we set and clear this longClick flag, which is nice. The first button, which is the delete is about, “Do you actually need to delete this?” and it says, “Delete file or folder?” and we delete it. Now we’ll depart it right here and run the app to examine all the things is working wonderful. We’ll examine particularly the delete performance. We’ll lengthy click on these and we’ll press del, delete and yes. It works great, proper? We’ll delete the folder right now. Delete, sure, it really works nice. It’s working nice. Proper now, we go to the create New Folder and verify if it’s working also great. We go to new Folder and press NF, and we’ll create folder check, press on okay, and we have now a folder, check folder. If we go inside, we return, if we long click and delete. “Do you actually need to delete it?” If I say no, this ought to be unselected. On the delete technique, once we say no, we’ve got to unselect this. Right right here aside from hiding the dialog, we’ve to unselect the current choice. To try this, we’ll unselect principally every thing. The straightforward approach of doing is simply calling the refresh, so we’re going to call the refresh button that decision on click on.

We’ll check if that works. Numerous tiny stuff we now have to check to ensure the app works great. We long click, delete, we say no, and we go. Now as you possibly can see, it’s fastened and if I long click this, delete, and say yes, then it’s deleted. If I need to delete the rows item right here, hopefully it really works, delete, it works. It’s nice, working actually, really nice. The delete performance is okay, the create a brand new folder is okay. Now we’ll check the rename functionality to see if it’s working fantastic or not, so we’re going to pick the file, go to rename, and rename from img_lights to image_lights123, for instance. Rename, we’ve got image_lights123 right here. Now if I’m going and choose this again, I press rename, and if I cancel this, then this is not working as anticipated. To try this, we will even set the adverse button, the rename dialog right right here, so you possibly can say renameDialog.setNegativeButton. The adverse button will say rename or it can say cancel perhaps, cancel can be nice. Cancel and comma, we’ve got to enter this semicolon, comma. Right here we’ll say, what was that, “new dialogueInterface onClickListener,” so you’ll be able to say, “new dialogueInterface.onclicklistener.” Once we say cancel aside from setting the dialogue to be hidden as we did proper here, dialog.cancel.
Right here, we’ll say dialog.cancel and we’ll refresh, so on rename right right here, we’ll say dialog.cancel and we cancel and say refreshButton.callOnClick. Now we’ll run the app to verify if this works high quality or not. We’re operating the app, we long click something, we press rename, and we place cancel, and it’s not selected, so it’s now fastened, which is nice.
Now, we go and we’ve got our copy button proper right here. Now, let’s see what it does. Our copy button principally sets then saves the trail. Principally, that’s what it does, it saves the trail, however now we aren’t supporting a listing if we copy a listing. If we copy a listing, we’ll return for now. If information chosen File Index is directory, we’ll return and do nothing. We’ll truly do something, which will probably be refreshButton.callOnClick. I feel that might be okay. Now we’ll try to copy a listing because we’re not having that functionality but.

We’ll lengthy click this folder check and we press copy, then we refresh, and nothing happens, but the bar continues to be proven, so we’ve to repair that too, and even better, once we present the bar, we’ll verify if we have now a folder. If we now have one, we’ll disable the copy choice and just have the delete and rename choice. isDirectory callOnClick, so we don’t actually need to examine this right here. What we’ll do is go and examine on the onLongClickListener of our ListView, onLongClickListener. Once we present the bar right here, we’ll present the bar, however we’ll examine something else and perhaps we have now to hide if the place is pointing to a folder. We’ll disguise the copy button. Principally, we’ll say, if information selected merchandise index are– Okay, we’re enabling just these two copy. If the selection is one and isn’t a folder, we’ll enable the copy button. We go and say information. If information choose merchandise index, straightforward listing. We need to verify if not directory. If it’s not a directory, we’ll present the copy button. Discover view by ID artwork that ID that replicate and we’ll say, set visibility seen. We’ll set these to visible. If not, then we’ll set the visible to gone, finally ready our ID that replicate. Set visibility to gone. Now we’ll run the app and verify that every thing works as we would like. Once we long click a folder, the copy choice shouldn’t appear. We long click on and the copy choice appears, so what’s happening here? We’ve to vary the copy choice from on the format to gone. You’re going to say visibility gone. We’ll run the app once more to verify if it works now.

Proper now we long click a folder and we will only delete the folder or rename the folder. If we long click a file, we will copy the file. For instance, copy and we create a brand new folder and on this new folder we’ll go and say, to stick, for instance. We open to stick and we’ll paste it here. It’s not working that high quality, however we’ll repair that simply now. Once we are pasting inside a new folder, it doesn’t work, we’ll run once more and check what occurs if we paste outdoors. Perhaps we have to refresh, if I’m not mistaken. We’ll lengthy click on this copy and we’ll open to paste. You’ve got here it as a result of we haven’t refreshed. We received paste and refresh and we’ve paste. We copy these pictures that JPEG after which we’re going to click on and replica then inside to paste. I’ll say paste and refresh and picture is that JPEG appears here. We have now that routinely. We now have to fix that in order for us to not being pressured to click on the refresh button every time we do it. We’re going to fix that on the paste technique. We now have- that is nice, great folder is great, rename button is nice, copy button is great. Paste button, here we’ve to do one thing else, we now have to refresh.

We will, once more, delete all of those strains right here and simply go and say refresh button that decision on click on. Earlier than we call on click on the refresh button, we’ve to vary the current path, is that right? Not likely. Let’s run to see if it works. If copy and paste works, now we’ll copy this photograph, this massive file, giant identify, copy and go inside to stick and paste, and it crashes. What’s happening? Let’s see what’s happening. We now have a error proper right here, let’s verify the error we are having.
Here we’ve 245 and right here we now have round right here the error. We obtained to figure out why is this occurring. In rename, did I see it appropriately? I received to take a look out to ensure we’re truly doing the right stuff here. We need to show all our purposes, so now we will have higher concept of what’s happening. Index out of bounds, rename path, rename button. We’ll run the app again, perhaps I misclicked a button. We’re going to go lengthy click, copy to paste, paste and we’ve an error right here. Now we’ll examine what’s happening proper right here. When this error appeared was once we press on paste.

We go and say paste, set visibility gone, we get the vacation spot path. We copy the file. We are calling the rename button, we have now to call the refresh button. It’s typing error truly, which is great to know. Refresh button. Now we’ll run the app once more. It’s a typo there that brought on the error, which is straightforward to repair now. Lengthy click this or lengthy click on, perhaps I will rename these pictures that JPEG first to pictures checks, for example, rename, seems great. Photographs check, now we’ll copy it and go to paste and paste it right here. Right here we’ve got the pictures check simply there, which is working, which is nice. Now the paste bottom is working, superior. We have now the textual content adapter and every part else is working nice. That’s it for this video. I hope you take pleasure in it and I see you in a future publish. Bye. Thanks very much for finishing this tutorial. What you’ve watched is a part of the complete course named Code an Android File Manager and Explorer app, you will get the hyperlink within the description, you can even just go to Uthena and purchase it.

The easiest value is, if you need this complete course and also you’re studying to be an Android or iOS developer, you’ll be able to see this course has something like 12 videos in it, but you will get all the Android cellular app developer bundle for life. You will get this one bundle for $48, this is linked within the description also. The thought with this bundle is you purchase this bundle as soon as and all courses we add for life are included in it. At present, there’s 25 plus hours of movies from 2019. I think about over the subsequent eight months we’ll add 75 plus extra hours.

We’ve multiple developers working virtually daily to movie new videos for you to offer you an impressive experience and one place you possibly can go from a total newbie to having an impressive profession as a developer online. Thanks for becoming a member of us right here. We respect the prospect to share instructional content with you totally free day-after-day that we hope is useful. You buying the bundle or the course helps us to pay the developers to offer you even more. Once you need to watch extra videos with us, will you please hit that subscribe button on YouTube and go just like the page on Facebook.
Put C first in your news feed, go really crazy. Since you’ve acquired this far into this video, I think about you’ll love all the opposite videos we share for you each day right here on my YouTube channel and on my Facebook web page.

Get personal label rights to promote this course yourself as a companion!

Thank you for making it to the top of this submit.

I really like you.

You’re superior.


Jerry Banfield