JAVA ADVANCE - Code Convention
JAVA ADVANCE - Code Convention
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.
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
Indentation
Naming Convention
Statements
Tidak Praktis
Naming Convention: Classes
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
Indentation
Naming Convention
Statements
• 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
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
Indentation
Naming Convention
Statements
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
Indentation
Naming Convention
Statements
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
• 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
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
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
Usage : Usage :
❏ Constant ❏ Website Address
e.g : NUM_OF_EMP e.g : pens.ac.id/kontak-
kami
References