Last time I wrote about a neat way to get stack traces using Android Studio’s debugger. This week I want to share a few thoughts and tools to help with formatting code.
This past week I was discussing code style with some of my coworkers, and one of them brought up a really good point — it makes it really difficult to review a PR if there are a bunch of formatting changes mixed in with functionality changes.
Therefore, it’s generally best practice to put large-scale formatting changes into a separate commit (or even a separate PR) from behavior changes.
I have long known about the format (⌘+⌥+L) and optimize imports (^+⌥+O) shortcuts in Android Studio, but I had only ever used them on an individual file. However, if you select the top level of your project and press the corresponding keyboard shortcut (or right click and select “Reformat code” or “optimize imports” from the contextual menu), you can in fact apply this to an entire project (or any directory of your choosing)!
In my opinion, there’s never a bad time to optimize imports (unless you’re doing an Android X migration — then the number of things being reordered might get a little messy). However, if you’re getting started with formatting, you should really make a separate PR for formatting the whole codebase. Then, once you’ve done that, either embed the code style into the project, or make sure that every member of your team is using the same style settings so everything stays consistent. There are few things more frustrating than watching formatting changes ping-pong around a team who don’t have unified settings (or the debates that might ensue over what proper formatting style is — but that’s a topic for another post).
You can even check the box in the “Commit Changes” window so it will Reformat or optimize imports for any future commits! There are also other ways to achieve this through hooks in your CI system using ktlint or other similar tools.
Hopefully this new knowledge helps you as much as it has helped me! How does your team handle ensuring consistent code formatting? Let me know in the comments below! And, please follow me on Medium if you’re interested in being notified of future tidbits.
Interested in joining the awesome team here at Intrepid? We’re hiring!
This tidbit was discovered on January 14, 2020.
Top comments (0)