DEV Community

Discussion on: 5 things I wish I knew before starting serious development (a journey into code perfection)

Collapse
 
cicirello profile image
Vincent A. Cicirello

On your formatting point.... Once you've decided upon a style, in some cases there may be a tool to automate compliance. For example, if you've adopted Google's Java style, there is a tool that will reformat your code:

GitHub logo google / google-java-format

Reformats Java source code to comply with Google Java Style.

google-java-format

google-java-format is a program that reformats Java source code to comply with Google Java Style.

Using the formatter

from the command-line

Download the formatter and run it with:

java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...]

The formatter can act on whole files, on limited lines (--lines), on specific offsets (--offset), passing through to standard-out (default) or altered in-place (--replace).

To reformat changed lines in a specific patch, use google-java-format-diff.py.

Note: There is no configurability as to the formatter's algorithm for formatting. This is a deliberate design decision to unify our code formatting on a single format.

IntelliJ, Android Studio, and other JetBrains IDEs

A google-java-format IntelliJ plugin is available from the plugin repository. To install it, go to your IDE's settings and select the Plugins category. Click the Marketplace tab, search for the google-java-format plugin, and click the Install button.

The plugin will…

Collapse
 
dcichorski profile image
Dariusz Cichorski

Great point! There are also other tools worth mentioning, e.g. if you use JavaScript/TypeScript there's a powerful combo of ESLint and Prettier that helps you respecting formatting rules and code style in general. Moreover, it's also able to reformat your code according to those rules, just like the tool you've mentioned.