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)