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

In this course, Introduction to Programming in Java, we learned all programming concepts and implement them in java. Key points in these lecture slides are: Coding Style, Guidelines, Good Java Coding, Marking Projects, Exams, Class-Related Guidelines, Method-Related Guidelines, Comment-Related Guidelines, Layout-Related Guidelines, More Information

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

102 documents

1 / 45

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Objectives
to give guidelines for good Java coding
I use these guidelines when marking projects,
exams, etc.
16. Coding Style
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

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