Rec. Preface
Java Coding Guidelines: 75 Recommendations for Reliable and Secure Programs provides specific advice to Java programmers. The application of these Java coding guidelines will lead to better systems that are more robust and more resistant to attack. These guidelines cover a wide range of products coded in Java for devices such as PCs, game players, mobile phones, home appliances, and automotive electronics.
Developers in any programming language should follow a set of guidelines to control the structure of their programs over and above what is specified by the programming language definition, and this is no less the case in Java.
Java programmers need more help than that provided by the Java Language Specification (JLS) [JLS 2013] to produce reliable and secure Java programs. Java contains language features and APIs that can easily be misused, and guidance is needed to avoid theses pitfalls.
For a program to be reliable, it must work in all situations and in the face of all possible input. Inevitably, any nontrivial program will encounter a completely unexpected input or situation, and errors will occur. When such errors occur, it is important that the impact is limited, which is best achieved by localizing the error and dealing with it as soon as possible. Programmers can benefit from the experience of others in anticipating unusual input or programming situations and adopting a defensive style of programming.
Some of these guidelines are stylistic, but they are nonetheless important for readability and maintainability of the code. For Java, Oracle provides a set of Code Conventions [Conventions 2009] to help programmers produce a consistent programming style, and these conventions are widely adopted by Java programmers.
The CERT ® Oracle ® Secure Coding Standard for Java
Java™ Coding Guidelines is written by the authors of The CERT ® Oracle ® Secure Coding Standard for Java [Long 2011]. That coding standard provides a set of rules for secure coding in the Java programming language. The goal of those rules is to eliminate insecure coding practices that can lead to exploitable vulnerabilities. The Secure Coding Standard establishes normative requirements for software systems. These software systems can then be evaluated for conformance to the coding standard, for example, by using the Source Code Analysis Laboratory (SCALe) [Seacord 2013]. However, there are poor Java coding practices that, despite not warranting inclusion in a secure coding standard for Java, can lead to unreliable or insecure programs. Java Coding Guidelines serves to document and warn against such coding practices.
Although not included in The CERT ® Oracle ® Secure Coding Standard for Java, these guidelines should not be considered less important. Guidelines must be excluded from a coding standard when it is not possible to form a normative requirement. There are many reasons that a normative requirement cannot be formed. Perhaps the most common is that the rule depends on programmer intent. Such rules cannot be automatically enforced unless it is possible for the programmer’s intent to be specified, in which case, a rule could require consistency between the code and the specified intent. Forming a normative requirement also requires that a violation of that requirement represent a defect in the code. Guidelines have been excluded from the coding standard (but included in this book) in cases where compliance with the guideline is always a good idea, but violating the guideline does not always result in an error. This distinction is made because a system cannot be cited for nonconformance without a specific defect. Consequently, coding rules must be very narrowly defined. Coding guidelines can often have a more far-reaching impact on security and reliability just because they can be more broadly defined.
Many of the guidelines refer to rules in The CERT ® Oracle ® Secure Coding Standard for Java. These references are of the form IDS01-J. Normalize strings before validating them, where the first three letters of the reference identify the appropriate chapter of The CERT ® Oracle ® Secure Coding Standard for Java. For example, IDS refers to Chapter 2, «Input Validation and Data Sanitization (IDS).»
The Secure Coding Standard for Java rules are also available on CERT’s secure coding wiki at https://www.securecoding.cert.org/confluence/display/java/The+CERT+Oracle+Secure+Coding+Standard+for+Java, where they continue to evolve. The CERT ® Oracle ® Secure Coding Standard for Java provides the definition of the rules for conformance testing purposes, but the wiki may contain additional information or insights not included in the book that may help you interpret the meaning of these rules.
Cross references to other guidelines throughout this book are given simply by the number and title of the guideline.
Oracle coding standards for java
Code Conventions for the Java TM Programming Language
The information on this page is for Archive Purposes Only
- Introduction
1.1 Why Have Code Conventions
1.2 Acknowledgments - File Names
2.1 File Suffixes
2.2 Common File Names - File Organization
3.1 Java Source Files
3.1.1 Beginning Comments
3.1.2 Package and Import Statements
3.1.3 Class and Interface Declarations - Indentation
4.1 Line Length
4.2 Wrapping Lines - Comments
5.1 Implementation Comment Formats
5.1.1 Block Comments
5.1.2 Single-Line Comments
5.1.3 Trailing Comments
5.1.4 End-Of-Line Comments
5.2 Documentation Comments - Declarations
6.1 Number Per Line
6.2 Initialization
6.3 Placement
6.4 Class and Interface Declarations - Statements
7.1 Simple Statements
7.2 Compound Statements
7.3 return Statements
7.4 if, if-else, if else-if else Statements
7.5 for Statements
7.6 while Statements
7.7 do-while Statements
7.8 switch Statements
7.9 try-catch Statements - White Space
8.1 Blank Lines
8.2 Blank Spaces - Naming Conventions
- Programming Practices
10.1 Providing Access to Instance and Class Variables
10.2 Referring to Class Variables and Methods
10.3 Constants
10.4 Variable Assignments
10.5 Miscellaneous Practices
10.5.1 Parentheses
10.5.2 Returning Values
10.5.3 Expressions before `?’ in the Conditional Operator
10.5.4 Special Comments - Code Examples
11.1 Java Source File Example