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.
16. Coding Style
Overview
- Class-related Guidelines
- Method-related Guidelines
- Comment-related Guidelines
- Layout-related Guidelines
- More Information
1. Class-related Guidelines
- 1 .1. Class Names
- 1 .2. Object Names
- 1 .3. Class Data
- 1 .4. Static for Constants
- 1 .5. Initializing Variables
- 1 .6. Few Global Variables
- 1 .7. Get and Set
- 1 .8. Change Date Locally
- 1 .9. Classes in Files
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
an object name refers to a single thing
starts with a lowercase letter
1.3. Class Data
- Always define data as private
- Avoid the keywords public, protected
- Always include a visibility keyword
- e.g. int x; // bad: unclear, lazy
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
1.6. Few Global Variables
- Keep the number of global variables in a class to a minimum.
- Bad Code: private int x; // global in class public void calc() { x = 0; bar(5); System.out.println(x); } private void bar(int y) { x = x + y; }
continued Docsity.com
- 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 Docsity.com
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()
- The code is better since x is local to the method that uses it, and is passed as a parameter to the other method
- Note how x is updated by using return
- the following code will not change x in calcX(): private void addX(int x, int y) { x = x+y; }
continued Docsity.com
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 Docsity.com
- The method names must be set and get.
- Only include a set method if the variable needs to be changed.
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
1.9. Classes in Files
- If a class is longer than 2 pages of code then it should be saved in its own Java text file.
- There should not be more than 3 classes in a single Java text file.
- Having multiple files makes it easier to find code, and (re-)compilation is quicker.
continued Docsity.com
- 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
2.1. Method Names
- A method name should have the form verbNoun(…) - e.g. makeGraph(), printFile()
- Comments should not ‘echo’ the method name: private void makeGraph() // make the graph {...} (^) useless since it adds no new information
2.2. Method Visibility
- Methods should be made private unless they will be called from outside the object.
- In basic Java programming, you will not need to use the protected keyword.
2.3. Method Length
- No method should be longer than 1 page of print-out - e.g. large constructor methods are bad
- No method should be smaller than 5 lines
- except for main() which should be as small as possible
2.4. Keep Anonymous Classes Small
- An anonymous class inside a method should be less than 10 lines long.
- Lots of small anonymous classes in a class is bad style.
- A separate handler class is better style for large size listener code.
3. Comment-related Guidelines
- 3 .1. Project Details
- 3 .2. Project Overview
- 3 .3. Class/Method Comments
- 3 .4. Other People’s Code
- 3 .5. Line Comments
- 3 .6. Comment Style