[go: up one dir, main page]

0% found this document useful (0 votes)
20 views43 pages

JAVA ADVANCE - Code Convention

The document discusses Java code conventions including naming conventions, indentation, whitespace, declarations, and statements. Naming conventions specify that classes, interfaces, and methods use PascalCase while variables use camelCase. Indentation and whitespace guidelines improve readability. Declarations and statements are formatted for clarity.

Uploaded by

Fatin Furoida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views43 pages

JAVA ADVANCE - Code Convention

The document discusses Java code conventions including naming conventions, indentation, whitespace, declarations, and statements. Naming conventions specify that classes, interfaces, and methods use PascalCase while variables use camelCase. Indentation and whitespace guidelines improve readability. Declarations and statements are formatted for clarity.

Uploaded by

Fatin Furoida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

PEMROGRAMAN LANJUT

CODE CONVENTIONS
Oleh Politeknik Elektronika Negeri Surabaya
2018
Why is it important
www.freepik.com
Ease of Maintenance
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.

Improved Readibility; Ease of Collaboration


Code conventions improve the readability of the software,
allowing engineers to understand new code more quickly
and thoroughly.

Why is it important
Code Conventions
Set of guidelines for a specific programming language that recommend
programming style, practices, and methods for each aspect of a program
written in that language.
White Space
Declaration

Indentation

Naming Convention
Statements

Java Code Conventions


White Space
Declaration

Indentation

Naming Convention
Statements

Java Code Conventions


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.

Tidak Praktis
Naming Convention: Classes

• Class names should be nouns, in mixed case


with the first letter of each internal word
capitalized
PascalCase • Try to keep your class names simple and
descriptive
• Use whole words—avoid acronyms and
abbreviations (unless the abbreviation is
much more widely used than the long form,
such as URL or HTML)
Naming Convention: Interfaces

PascalCase
• Interface names should be
capitalized like class names
Naming Convention: Methods

camelCase
• Methods should be verbs
• It is written in mixed case with the
first letter lowercase, with the first
letter of each internal word
capitalized
Naming Convention: Variables
Temporary

• Variable names should be short yet


meaningful
• The choice of a variable name should
be mnemonic— that is, designed to
camelCase indicate to the casual observer the
intent of its use
• One-character variable names should
be avoided except for temporary
“throwaway” variables
Naming Convention: Constants

• The names of variables declared class constants should be all


uppercase
White Space
Declaration

Indentation

Naming Convention
Statements

Java Code Conventions


Indentation: Line Length

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

38 65
Indentation: Wrapping Lines
• Break after a comma
• Break before an operator
• 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 against the right margin, just
indent 8 spaces instead
Indentation: Wrapping Lines

Break after comma

Align the new line with the beginning of the


expression at the same level on the previous line
Indentation: Wrapping Lines

Break before an operator

The first example is preferable, because the break occurs outside the
parenthesized expression
Indentation: Wrapping Lines

The first example presents conventional indentation which align the new line
with the beginning of the expression at the same level on the previous line.
Thus, it can make a very deep indent.
Indentation: Wrapping Lines

To avoid a very deep indent, you can use 8 spaces to indent the new line as
presented in the second example
White Space
Declaration

Indentation

Naming Convention
Statements

Java Code Conventions


Declaration: Variables

• Variables and functions should never be declared on the same line


• Do not put different types on the same line

• Put variables’ declarations only at the beginning of blocks.


• Don’t wait to declare variables until their first use
Declaration: Variables

• Avoid local declarations that hide declarations at higher levels


Declaration: Class and Interface

Open brace “{” appears at the end of the


same line as the declaration statement

Closing brace “}” starts a line by itself


indented to match its corresponding
opening statement, except when it is a
null statement the “}” should appear
immediately after the “{“
Declaration: Class and Interface

No space between a method name and the


parenthesis “(“ starting its parameter list
Declaration: Class and Interface

Methods are separated by a blank line


White Space
Declaration

Indentation

Naming Convention
Statements

Java Code Conventions


Statements

• Simple statement: Each line should contain at most one statement


• Compound statement: Lists of statements enclosed in braces

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.>> K&R Style

Braces are used around all statements


Statements
Statements
White Space
Declaration

Indentation

Naming Convention
Statements

Java Code Conventions


White Space: Blank Lines

• To improve readability by setting off sections of code that are


logically related

1 Blank
line
• Between methods
• Between the local variables in a method and its first statement

2 Blank
lines
• Between sections of a source file
• Between class and interface definitions
White Space: Blank Spaces

• A keyword followed by a parenthesis


should be separated by a space
• A blank space should appear after commas
in argument lists
• All binary operators except . should be
separated from their operands by spaces.
Blank spaces should never separate unary
operators such as unary minus, increment
(“++”), and decrement (“--”) from their
operands
• The expressions in a for statement should
be separated by blank spaces
• Casts should be followed by a blank space
Other Code Conventions in Brief…
Naming Convention

• Full Name
It uses full name instead of a common abbreviation
E.g:
salaryThisMonth, totalSelling, etc
• Hungarian Notation
It uses some common abbreviation to shorten name
E.g:
lOf >> length of
nOf >> number of
arrOf >> array of
Indentation

K&R Allman Haskell / Python


while (x == y) { while (x == y) while (x == y)
something(); { { something()
somethingelse(); something(); ; somethingelse()
} somethingelse(); ;}
}
Indentation: Vertical Code Alignment

Which one is easier to be read?


Indentation: Vertical Code Alignment

The right one is easier to be read, because it is vertically


aligned.
Capitalization: By Capital

Pascal Case Camel Case

Usage: Usage :
❏ Class Name ❏ Variable Name
e.g : ProgrammerManager e.g : salaryThisMonth
❏ Function for C Family ❏ Field Name
e.g : BuatReport() e.g : databaseKaryawan
❏ Function for Java
Family/Scripting Language
e.g : doSorting()
Capitalization: By Capital

Upper Case Lower Case

Usage: Usage :
❏ Constant ❏ Primitive data type
e.g : NUMBER_OF_EMP e.g : int, float, uint
❏ Enumeration ❏ Keyword in Java
e.g : ENUM_BUILDING e.g : private, public, etc
❏ Package name
e.g : database, com, etc
Capitalization: By Capital

Toggle Case Mixed Case / Studly Caps

e.g : mAULIDANgAMES Usage :


❏ Encryption
e.g : aSbHHeels56xS
Capitalization: By Splitter

Snake Case (_) Train Case (-)

Usage : Usage :
❏ Constant ❏ Website Address
e.g : NUM_OF_EMP e.g : pens.ac.id/kontak-
kami
References

• Oracle. 1997. JAVA Code Convention.


• Tim Maulidan Games. 2018. Modul Pelatihan Desain Pattern.

You might also like