In Section 2 you will discuss the importance of code style. While some elements of style can be subjective, others are justified because experience has shown that they reduce bugs and make code easier to review and maintain. Above all, coding style should be consistent within a project or organization. Read more about the benefits of style guides in Chapter 8, Style Guides and Rules, of Software Engineering at Google.
In CS 2110 we expect you to follow sound stylistic practices when writing code, especially code submitted for programming assignments. This means choosing clear variable names, ordering and decomposing computations logically, minimizing scope and access, and writing thorough specifications and sufficient implementation comments. It also means adhering to our class formatting rules. Fortunately, the latter can be handled automatically by IDEA.
Please download our code style scheme (right-click, Save link as… or Download Linked File) and import it into IDEA using the following procedure:
(For reference, our formatting rules are based on Google’s Java Style Guide, but with a block indentation of 4 spaces, which is more common. Read their style guide to learn about their justifications as well as some higher-level guidelines that cannot be enforced by auto-formatting.)
Now, when you are editing some code, you can select Code → Reformat Code to apply the style rules to the current file. Take note of the keyboard shortcut so you can do this quickly as you write. You can also configure IDEA to automatically reformat your code whenever you save it. Go to File → Settings, then select Tools > Actions on Save, then check Reformat code and click OK.
Practicing good style (both low-level formatting and higher-level guidelines) is to your advantage for more than just style credit. It will make it easier for consultants to diagnose problems in your code, and it will make it easier for graders to evaluate your submissions. You may also find that it’s easier for you to pick up where you left off when working on a long assignment. With code as with English, don’t let poor presentation undermine good content.