15
Style guide in JAVA From a lecture by Dr. Rahman

Style guide in JAVA From a lecture by Dr. Rahman

Embed Size (px)

Citation preview

Page 1: Style guide in JAVA From a lecture by Dr. Rahman

Style guide in JAVA

From a lecture by Dr. Rahman

Page 2: Style guide in JAVA From a lecture by Dr. Rahman

Followup from lab Could you have done this without integer division?

Is it easier with integer division?

If you have not yet done this lab…make sure you do so before part 2. You will not get credit for part 2 if you do not get 10 points on part 1.

Page 3: Style guide in JAVA From a lecture by Dr. Rahman

public class anyclass{public static void main (String [] args) {int a; double b;

final double c=2.54; Scanner d ; d=new Scanner(System.in) ;

a=d.nextInt(); System.out.print(" How many inches ?"); b=a*c; System.out.print(a+ “ in= “) ;System.out.println(b+“ cm”);}}

Joe’s code has a bug, now fix it

Page 4: Style guide in JAVA From a lecture by Dr. Rahman

/*** Application converts inches to centimeters.** @author Farzana Rahman* @version 09/13/2014*/ public class ConvertInches { public static void main ( String [] args )

{ int inch; double cent; final double CENT_PER_INCH; CENT_PER_INCH = 2.54; // create a scanner for standard input Scanner keyboard; keyboard = new Scanner( System.in ); // prompt the user and get the value System.out.print(" How many inches ? "); inch = keyboard.nextInt(); // convert and output the result cent = inch * CENT_PER_INCH; System.out.print( inch + " in = "); System.out.println( cent + " cm"); }}

Page 5: Style guide in JAVA From a lecture by Dr. Rahman

Which do you prefer? Why?

Page 6: Style guide in JAVA From a lecture by Dr. Rahman

What are Coding Standards

• Coding standards are guidelines for code style and documentation.

• This course will more or less follow industry standard Code Conventions for the Java.

• Some portion of the grade for PA is based on conformance to these standards.

• Program Design• Naming Conventions• Formatting Conventions• Documentation• Declaration…

Page 7: Style guide in JAVA From a lecture by Dr. Rahman

Our class Style Guide is found in the Resources tab of Java

• The checklist in your packet follows this exactly.

Page 8: Style guide in JAVA From a lecture by Dr. Rahman

Style guide highlights• A.1 All names should be descriptive and readable.

• B.1 In CS 139, do NOT combine declaration and assignment.

• B.2 All constants/variables must be declared at the top.

No No OK

int h; int hrs; int hours;

int m; int mns; int minutes;

int s; int scs; int seconds;

NO OK

Scanner input = new Scanner(System.in);int total = input.nextInt();

Scanner input;int total;

input = new Scanner(System.in);total = input.nextInt();

Page 9: Style guide in JAVA From a lecture by Dr. Rahman

Other notes

• Constants – any value that you can describe should be a constant. For this program, the constants would be either the 60 conversion factor if you used it only or both the 3600 and the 60 you used.

• Constants make a program more readable than the corresponding literal.

• Constants should be declared and initialized at the top of the program.

Page 10: Style guide in JAVA From a lecture by Dr. Rahman

Why Have Coding Standards

• Greater consistency between developers

• Easier to develop and maintain

• Saves time and money

Page 11: Style guide in JAVA From a lecture by Dr. Rahman

In your groups

• Look at the three payroll solutions. They all do the same thing.

• What do you like better and why?

• What is an example of one good practice?

• What is an example of one bad practice?

Page 12: Style guide in JAVA From a lecture by Dr. Rahman

Apply the checklist to the last problem.

• Given one point for each instance of a problem for a maximum of 2 and 5 points for any one category, how would you score it.

• Put answers on the board. Presenter, be prepared to defend your answer.

Page 13: Style guide in JAVA From a lecture by Dr. Rahman

In your groups

• Look at the one or two examples of SecondsToHours. • Each pair (or solo) should score their program. Then share your code and score with the

group. • Each team should try to help the others on the team achieve a perfect style score.

• What common errors did you find?

Page 14: Style guide in JAVA From a lecture by Dr. Rahman

Homework

• Resubmit your SecondsToHours.java program to WebCAT, taking care to both get the 100% correct and to follow the Style Guide.

• The assignment to submit to is SecondsToHours V2.

• This assignment will be graded just like we would grade a PA for Style.

• You will be able to see the feedback in WebCAT.

• No credit will be given for Lab 2 if you do not get 10 points on V1. (no credit for V1 late, but its your entrance pass to V2).

Page 15: Style guide in JAVA From a lecture by Dr. Rahman

As you leave

• Erase the whiteboard and fill in the exit pass for today.

• What is still fuzzy about operations, data, and now Style?

• What is going well? OR What did you learn?