Naming Conventions in Java Programming: Best Practices, Slides of Java Programming

Guidelines on java naming conventions for methods, variables, classes, interfaces, and files. It covers topics such as verb-based method names, underscore suffix for private class variables, generic variable naming, specific naming conventions, and file organization.

Typology: Slides

2011/2012

Uploaded on 08/09/2012

dhanyaa
dhanyaa ๐Ÿ‡ฎ๐Ÿ‡ณ

4.7

(3)

60 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
9/17/2010
22
Naming
`Names re
p
resentin
g
methods must be
pg
verbs and written in mixed case starting
with lower case
`getName(), computeTotalWidth()
`
Abb i ti d h ld t b
`
Abb
rev
i
a
ti
ons an
d
acronyms s
h
ou
ld
no
t
b
e
uppercase when used as name
`exportHtmlSource();
// NOT: exportHTMLSource();
Fall 2010cs42022
openDvdPlayer();
// NOT: openDVDPlayer();
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Naming Conventions in Java Programming: Best Practices and more Slides Java Programming in PDF only on Docsity!

Naming

` Names representing methods must bep g

verbs and written in mixed case starting

with lower case

` getName(), computeTotalWidth()

`` AbbAbb reviations and acronyms should not bei ti d h ld t b

uppercase when used as name

` exportHtmlSource();

// NOT: exportHTMLSource();

22 cs420 Fall 2010

openDvdPlayer();

// NOT: openDVDPlayer();

docsity.com

Naming

` Private class variables should have

underscore suffix

` private String name_;

` A side effect of the underscore naming

convention is that it nicely resolves theconvention is that it nicely resolves the

problem of finding reasonable variable

names for setter methods:

` void setName(String name)

23 cs420 Fall 2010

{ name_ = name; }

docsity.com

Naming

` The name of the object is implicit, and should be

avoided in a method name

` line.getLength(); // NOT: line.getLineLength();

25 cs420 Fall 2010

docsity.com

Specific Naming Conventions

` The terms get/set must be used where an

attribute is accessed directly

` employee.getName(); employee.setName(name);

matrix.getElement(2, 4); matrix.setElement(2, 4, value);

` is prefix should be used for boolean variables

and methods ( has , can and should prefixes)

**isSet, isVisible, isFinished, isFound, isOpen** b l l t ()

26 cs420 Fall 2010

` boolean canEvaluate(); boolean shouldAbort = false;

docsity.com

Specific Naming Conventions

` โ€˜nโ€™ prefix should be used for variables

representing a number of objects

` nPoints, nLines

` No suffix should be used for variables

representing an entity number

` (An elegant alternative is to prefix such variables

with an โ€˜Iโ€™. This effectively makes them named

iterators )

` tableNo, employeeNo

28 cs420 Fall 2010

` iTable, iEmployee

docsity.com

Specific Naming Conventions

` Iterator variables should be called i , j , k etc

` for (Iterator i = points.iterator(); i.hasNext(); ) { : }

` Variables named j , k etc. should be used for

nested loops only.

`` Abbreviations in names should be avoidedAbbreviations in names should be avoided

` computeAverage(); // NOT: compAvg(); ActionEvent event; // NOT: ActionEvent e;

` Negated boolean variable names must be

avoided

29 cs420 Fall 2010

` bool isError; // NOT: isNoError

docsity.com

Specific Naming Conventions

` Singleton classes should return their sole instance

through methodh h h d getInstancetI t

` class UnitManager { private final static UnitManager instance_ = new UnitManager();

private UnitManager() { ... }

public static UnitManager getInstance() // NOT: get() or instance() or unitManager() etc.{ return instance_; } }

31 cs420 Fall 2010

docsity.com

Specific Naming Conventions

` Classes that creates instances on behalf of others

( factories ) can do so through method

new[ClassName]

` class PointFactory { public Point newPoint(...) { ... } }

` Functions (methods returning an object) should be

named after what they return and procedures ( void

methods) after what they do

32 cs420 Fall 2010

docsity.com

Files

` The incompleteness of split lines must be made

obvious

totalSum = a + b + c + d + e; method(param1, param2, param3);

` Break after a comma.

` Break after an operator.

` Align the new line with the beginning of the expression on

the previous line

34 cs420 Fall 2010

docsity.com

Package and Import Statements

` The package statement must be the first statement

of the file. All files should belong to a specificf th fil All fil h ld b l t ifi

package.

` The import statements must follow the package

statement. import statements should be sorted

with the most fundamental packages first andwith the most fundamental packages first, and

grouped with associated packages together and

one blank line between groups.

` import java.io.IOException; import java.net.URL;

35 cs420 Fall 2010

import java.rmi.RmiServer; import java.rmi.server.Server;

import org.linux.apache.server.SoapServer;

docsity.com

Classes and Interfaces

` Class and Interface declarations should be

organized in the following manneri d i th f ll i

1. Class/Interface documentation.

2. class or interface statement.

3. Class (static) variables in the order public, protected,

package (no access modifier), private.

4. Instance variables in the order public, protected,

package (no access modifier), private.

5. Constructors.

6. Methods (no specific order).

37 cs420 Fall 2010

docsity.com

Methods

` Method modifiers should be given in the following

order:

static abstract synchronized

final native

The modifier (if present) must be the first

modifiermodifier

` public static double square(double a); // NOT: static public double square(double a);

38 cs420 Fall 2010

docsity.com

Variables

` Variables should be initialized where they are

declared

` and they should be declared in the smallest scope

possible

` Variables must never have dual meaning

` Class variables should never be declared public

` Variables should be kept alive for as short a time as

possible

40 cs420 Fall 2010

docsity.com

Loops

` Only loop control statements must be included in the

for() construction

` sum = 0; for (i = 0; i < 100; i++) sum += value[i];

//// NOT: for (i = 0, sum = 0; i < 100; i++) // sum += value[i];

41 cs420 Fall 2010

docsity.com