Java Coding Conventions
Java Coding Conventions
Objectives
Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit Demonstrate Validating Method Arguments Describe Using Testing Framework Distinguish Checked Vs. Unchecked Exceptions Describe File Organization
If a method returns an array which can be empty, do not allow it to return null. Instead, return a zero-length array. This simplifies the client, in that it never has to check for null values.
10
Methods are not obligated to establish a policy for the unchecked exceptions thrown by their implementation.
11
12
13
14
Variable Assignments
Avoid assigning several variables to the same value in a single statement. It is hard to read.
Example:
15
17
Key Points
Learn the traditional formats for naming variables, methods and other components of coding in Java. Notice the guidelines on indentation. Remember that you will be working on projects in teams, and following these conventions and standards will help to make your work much more readable for the other members of your team. Declare local variables immediately before use. Avoid declaring or assigning values of more than one variable per line of code. Fields should be private, with setters and getters if needed. Use System.exit with care. Use an interface for constants. Validate method arguments. Use a testing framework. Use zero-length arrays instead of null. Choose the best collection framework. Iterate without an index.
18
Resources
Helpful Websites:
19