As Android Studio user, this is the very basic thing to do, yet I often forget myself. And often also found some others not knowing it.
The 2 habits (that I still am developing as well ;P) to have a clean code (formatting), is to type the two shortcut as below before you leave each of your code file to another file.
- Alt-Cmd-L : automatic realign your code indentation, remove additional spaces, realign your parenthesis etc. No matter how neat a coder you are, you’ll be surprise what it’ll find.
- Ctrl-Alt-O : automatic remove unused imported packages. It’s quite common some of these packages are automatically imported and left there. So some house cleaning would help.
Do note this also applies to the XML files.
Have fun forming the habit :). I hope Android Studio will make it default (or there’s a way to auto execute this) whenever I move away from a file. Or at least, make this a single short-cut ;P
Do share if you have better ways of doing this :)