Code convention java oracle

Code convention java oracle

The information on this page is for Archive Purposes Only

4 — Indentation

Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

4.1 Line Length

Avoid lines longer than 80 characters, since they’re not handled well by many terminals and tools.

Note: Examples for use in documentation should have a shorter line length-generally no more than 70 characters.

4.2 Wrapping Lines

When an expression will not fit on a single line, break it according to these general principles:

  • Break after a comma.
  • Break before an operator.
  • Prefer higher-level breaks to lower-level breaks.
  • Align the new line with the beginning of the expression at the same level on the previous line.
  • If the above rules lead to confusing code or to code that’s squished up against the right margin, just indent 8 spaces instead.

Here are some examples of breaking method calls:

someMethod(longExpression1, longExpression2, longExpression3, longExpression4, longExpression5); var = someMethod1(longExpression1, someMethod2(longExpression2, longExpression3)); 

Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level.

longName1 = longName2 * (longName3 + longName4 - longName5)+ 4 * longname6; // PREFER longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // AVOID 

Following are two examples of indenting method declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces.

//CONVENTIONAL INDENTATIONsomeMethod(int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother) < . >//INDENT 8 SPACES TO AVOID VERY DEEP INDENTS private static synchronized horkingLongMethodName(int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother)

Line wrapping for if statements should generally use the 8-space rule, since conventional (4 space) indentation makes seeing the body difficult. For example:

//DON'T USE THIS INDENTATIONif ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) < //BAD WRAPS doSomethingAboutIt(); //MAKE THIS LINE EASY TO MISS >//USE THIS INDENTATION INSTEAD if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) < doSomethingAboutIt(); >//OR USE THIS if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6))

Here are three acceptable ways to format ternary expressions:

alpha = (aLongBooleanExpression) ? beta : gamma; alpha = (aLongBooleanExpression) ? beta : gamma; alpha = (aLongBooleanExpression) ? beta : gamma; 

Источник

Code convention java oracle

Code Conventions for the Java TM Programming Language

The information on this page is for Archive Purposes Only

  1. Introduction
    1.1 Why Have Code Conventions
    1.2 Acknowledgments
  2. File Names
    2.1 File Suffixes
    2.2 Common File Names
  3. 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
  4. Indentation
    4.1 Line Length
    4.2 Wrapping Lines
  5. 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
  6. Declarations
    6.1 Number Per Line
    6.2 Initialization
    6.3 Placement
    6.4 Class and Interface Declarations
  7. 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
  8. White Space
    8.1 Blank Lines
    8.2 Blank Spaces
  9. Naming Conventions
  10. 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
  11. Code Examples
    11.1 Java Source File Example

Источник

Code convention java oracle

The information on this page is for Archive Purposes Only

7 — Statements

7.1 Simple Statements

Each line should contain at most one statement. Example:

argv++; // Correct argc--; // Correct argv++; argc--; // AVOID! 

7.2 Compound Statements

Compound statements are statements that contain lists of statements enclosed in braces » < statements >«. See the following sections for examples.

  • The enclosed statements should be indented one more level than the compound statement.
  • The opening brace should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement.
  • Braces are used around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

7.3 return Statements

A return statement with a value should not use parentheses unless they make the return value more obvious in some way. Example:

return;return myDisk.size(); return (size ? size : defaultSize); 

7.4 if, if-else, if else-if else Statements

The if-else class of statements should have the following form:

if (condition) if (condition) < statements; >else < statements; >if (condition) < statements; >else if (condition) < statements; >else

Note: if statements always use braces, <> . Avoid the following error-prone form:

if (condition) //AVOID! THIS OMITS THE BRACES <>!statement; 

7.5 for Statements

A for statement should have the following form:

for (initialization; condition; update)

An empty for statement (one in which all the work is done in the initialization, condition, and update clauses) should have the following form:

for (initialization; condition; update);

When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables. If needed, use separate statements before the for loop (for the initialization clause) or at the end of the loop (for the update clause).

7.6 while Statements

A while statement should have the following form:

An empty while statement should have the following form:

7.7 do-while Statements

A do-while statement should have the following form:

7.8 switch Statements

A switch statement should have the following form:

Every time a case falls through (doesn’t include a break statement), add a comment where the break statement would normally be. This is shown in the preceding code example with the /* falls through */ comment.

Every switch statement should include a default case. The break in the default case is redundant, but it prevents a fall-through error if later another case is added.

7.9 try-catch Statements

A try-catch statement should have the following format:

try catch (ExceptionClass e)

A try-catch statement may also be followed by finally , which executes regardless of whether or not the try block has completed successfully.

try catch (ExceptionClass e) < statements; >finally

Источник

Code convention java oracle

The information on this page is for Archive Purposes Only

3 — File Organization

A file consists of sections that should be separated by blank lines and an optional comment identifying each section.

Files longer than 2000 lines are cumbersome and should be avoided.

For an example of a Java program properly formatted, see «Java Source File Example» on page 19.

3.1 Java Source Files

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

Java source files have the following ordering:

  • Beginning comments (see «Beginning Comments» on page 4)
  • Package and Import statements
  • Class and interface declarations (see «Class and Interface Declarations» on page 4)

3.1.1 Beginning Comments

All source files should begin with a c-style comment that lists the class name, version information, date, and copyright notice:

/* * Classname * * Version information * * Date * * Copyright notice */ 

3.1.2 Package and Import Statements

The first non-comment line of most Java source files is a package statement. After that, import statements can follow. For example:

package java.awt;import java.awt.peer.CanvasPeer; 

3.1.3 Class and Interface Declarations

The following table describes the parts of a class or interface declaration, in the order that they should appear. See «Java Source File Example» on page 19 for an example that includes comments.

Источник

Читайте также:  Php local server xampp
Оцените статью