Docsity

Coding Style - Introduction to Programming in Java - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Coding Style - Introduction to Programming in Java - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Objectives

  • to give guidelines for good Java coding
    • I use these guidelines when marking projects,

exams, etc.

  1. Coding Style

Overview

1. Class-related Guidelines

2. Method-related Guidelines

3. Comment-related Guidelines

4. Layout-related Guidelines

5. More Information

1.1. Class Names

• A class name should be a noun

– the noun is often a ‘category’ word

  • e.g. Plane, Car, Database

– long names may use several words

  • e.g. JumboJet, Toyota, StudentDb

starts with

an uppercase

letter

each word

starts with

an uppercase

letter

1.2. Object Names

• An object name should be a noun

– the noun is often the category word and a number

  • e.g. plane1 car2, database

– but try to use names with more meaning

  • e.g. andrewDb, coeCar

an object name refers to

a single thing

starts with

a lowercase

letter

1.4. Static for Constants

• Only use static to declare constants:

private static final int MAXLEN = 120;

– always write static final

– always write the constant name in uppercase

• Never use static with methods

( except for main())

1.5. Initializing Variables

• Global objects and variables should be

initialised inside a method (often the

constructor):

private int counter; // global

public Matrix()

{ counter = 0 ; ...}

• It may be okay to initialise simple global

variables (e.g. ints, doubles) globally:

private int counter = 0; // global

  • The code is bad because a person who

looks at a class will need to check every line

of code to find out where x is used.

  • The use of globals often means that a

student is not sure how to change variables

or objects inside Java methods

  • or is too lazy to code in a good way

continued

  • Good Code: public void calcX() { int x = 0 ; x = addX(x,5); // x gets new value System.out.println(“x= “ + x); } private int addX(int x, int y) { return x+y; }

continued

the global x

has gone

x in addX() is a

copy of the x in

calcX()

  • Good code with objects: public void calcMatrix() { Matrix m1 = new Matrix(); processMatrix(m1); m1.print(); } private void processMatrix(Matrix m) { // change m (really m1) }

objects are

passed using

call by reference,

so do not need

to be returned

1.7. Get and Set

• Private data which will be accessible outside

an object should be given set and get

methods

public class BankAccount { private double balance; public double getBalance () { return balance; } public void setBalance (double b) { balance = b; } : }

continued

1.8. Change Data Locally

• Methods that change data should be in the

class where the data is declared.

• Bad code:

public static void main(String args[]) { BankAccount myacc = new BankAccount(); double bal = myacc.getBalance(); bal = bal * 1.15; // calculate interest myacc.setBalance(bal); }

continued

change done in main()

  • Good code (in BankAccount): public void applyInterest(double interest) { bal = bal * interest; } in main(): BankAccount myacc = new BankAccount(); myacc.applyInterest(1.15);

change done in the object

  • I use Windows Grep to search for text

inside multiple files (and directories)

  • free from http://www.wingrep.com

2. Method-related Guidelines

• 2 .1. Method Names

• 2 .2. Method Visibility

• 2 .3. Method Length

• 2 .4. Keep Anonymous Classes Small