It’s common for development organizations to declare standards for many different aspects of software design and development. One such aspect is code conventions—i.e. standard practices for the way that code is written and organized: naming and casing of modules, functions, variables, and other entities; formatting of code, beyond that dictated by programming language semantics; commenting guidelines; etc. Even details that may seem quite trivial at first glance—such as the decision to use spaces or tabs for horizontal whitespace—may be addressed explicitly in such conventions.

Ultimately, the aim of such standards is to foster a consistent and maintainable code base. As stated in Java Code Conventions (Sun Microsystems, 1997),

Code conventions are important to programmers for a number of reasons:

  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.
  • If you ship your source code as a product, you need to make sure it is as well packaged and clean as any other product you create.

No single set of coding conventions is best for all organizations or all situations, and conventions vary even between similar programming languages used in a single organization. In the case of Java, many organizations have used the code conventions originally specified by Sun (the most recent revision of which can be accessed at Code Conventions for the Java Programming Language) as a starting point, and then modified, updated, and extended those conventions to reflect the aims and experiences of the organizations.

For our Java coding, we will use (and require the use of) the conventions outlined in the Deep Dive Coding Java + Android Bootcamp Style Guide. The Java portion of this DDC style guide is based on the Google Style Guide. To make applying these conventions to our code as automatic as possible, we’ll install a formatting scheme from the Google Style Guide that implements several of the style guide’s rules in IntelliJ IDEA.

Videos

Installing the IntelliJ IDEA plug-in

  1. Open a Bash terminal (Git Bash on Windows, Terminal on OS X and Linux).

  2. Create and navigate to the documentation directory (if it doesn’t already exist) by executing the following commands:

     mkdir -p ~/bootcamp/docs
     cd ~/bootcamp/docs
    
  3. Clone the Google Style Guide repository by executing this command:

     git clone git@github.com:google/styleguide.git
    

    If you are are asked to type in your passphrase, please type the passphrase you used when creating your SSH key (see “SSH key generation and GitHub configuration”).

  4. In IntelliJ IDEA, use the Customize/All settings link in the welcome screen, or the File/Settings (Windows or Linux) or IntelliJ IDEA/Preferences (OS X) command from the workspace, to open the Settings dialog.

  5. In the Editor/Code Style/Java settings, click the Manage button (this may be displayed as a gear icon, next to the Scheme pull-down control).

  6. In the pull-down or dialog that appears, click Import or Import Scheme, then select “IntelliJ IDEA code style XML”.

  7. Navigate to the bootcamp\docs\styleguide directory, select the intellij-java-google-style.xml file, and click OK. “GoogleStyle” should now appear in the list of of code style schemes.

  8. Close the Code Style Schemes dialog.

  9. Select “GoogleStyle” from the Scheme pull-down list.

  10. Click Ok to save the updated preferences.

Optional task

  • In addition to the Google style guide, we suggest you bookmark the SQL Style Guide, to keep it handy for the SQL exercises and coding work in the bootcamp.