1 university of utah – school of computing computer science 1021 "programming with...

45
1 University of Utah – School of Computing Computer Science 1021 "Programming with Style"

Upload: martin-brisley

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

11University of Utah – School of Computing

Computer Science 1021 "Programming with Style"

University of Utah – School of Computing

University

of Utah

22

No oneprograms

in avacuum.

University of Utah – School of Computing

University

of Utah

33

In Real Life...

• Other people will read your code• Other people will maintain your code• Other people will reuse your code in new

programs

University of Utah – School of Computing

University

of Utah

44

So...

• Make your programs easy to understand• Don't be too “clever!”

University of Utah – School of Computing

University

of Utah

55

Style Tip #1

• Indent consistently

University of Utah – School of Computing

University

of Utah

66

No Indentationpublic void mousePressed(MouseEvent e) {mousePos = e.getPoint();prevMousePos = mousePos;if (e.getButton() == 1) {if (lassoRect.contains(mousePos)) {return;} for (WhiteboardTeam wt : teams) {WhiteboardCard c = wt.getCardAtPosition(mousePos);if (c != null) {select(c);dragging = true;dnd = false;c.setPrevPosition(c.getPosition());return;}} lassoPos = new Point(mousePos);lassoRect.setLocation(lassoPos);lassoRect.setSize(0,0);setCursor(Cursor.getDefaultCursor());}}

University of Utah – School of Computing

University

of Utah

77

Bad Indentation public void mousePressed(MouseEvent e) { mousePos = e.getPoint(); prevMousePos = mousePos; if (e.getButton() == 1) { if (lassoRect.contains(mousePos)) { return; } for (WhiteboardTeam wt : teams) { WhiteboardCard c = wt.getCardAtPosition(mousePos); if (c != null) {select(c); dragging = true; dnd = false; c.setPrevPosition(c.getPosition()); return; }} lassoPos = new Point(mousePos); lassoRect.setLocation(lassoPos); lassoRect.setSize(0,0); setCursor(Cursor.getDefaultCursor());}}

University of Utah – School of Computing

University

of Utah

88

Good Indentationpublic void mousePressed(MouseEvent e) { mousePos = e.getPoint(); prevMousePos = mousePos; if (e.getButton() == 1) { if (lassoRect.contains(mousePos)) { return; } for (WhiteboardTeam wt : teams) { WhiteboardCard c = wt.getCardAtPosition(mousePos); if (c != null) { select(c); dragging = true; dnd = false; c.setPrevPosition(c.getPosition()); return; } } lassoPos = new Point(mousePos); lassoRect.setLocation(lassoPos); lassoRect.setSize(0,0); setCursor(Cursor.getDefaultCursor()); }}

University of Utah – School of Computing

University

of Utah

99

Style Tip #2

• Use blank lines wisely- between each method- between each important part of a method

University of Utah – School of Computing

University

of Utah

1010

Spacingpublic void mousePressed(MouseEvent e) { mousePos = e.getPoint(); prevMousePos = mousePos;

if (e.getButton() == 1) { if (lassoRect.contains(mousePos)) { return; }

for (WhiteboardTeam wt : teams) { WhiteboardCard c = wt.getCardAtPosition(mousePos); if (c != null) { select(c); dragging = true; dnd = false; c.setPrevPosition(c.getPosition()); return; } }

lassoPos = new Point(mousePos); lassoRect.setLocation(lassoPos); lassoRect.setSize(0,0); setCursor(Cursor.getDefaultCursor()); }}

University of Utah – School of Computing

University

of Utah

1111

Style Tip #3

• Don't make methods too long

University of Utah – School of Computing

University

of Utah

1212

Style Tip #4

• Use standard naming conventions- Class names: First letter capitalized- Variable names: First letter lowercase- “final” variables: ALL_CAPS

University of Utah – School of Computing

University

of Utah

1313

Style Tip #5

• Variables on top• Methods below

University of Utah – School of Computing

University

of Utah

1414

Not wrong, just weirdpublic class Person {

public void setMother(Person m) {mother = m;

}

public void setFather(Person f) {father = f;

}

public Person(String n) {name = n;

}

private String name;private Person mother;private Person father;

}

University of Utah – School of Computing

University

of Utah

1515

Ahh... That's better!public class Person {

private String name;private Person mother;private Person father;

public Person(String n) {name = n;

}

public void setMother(Person m) {mother = m;

}

public void setFather(Person f) {father = f;

}}

University of Utah – School of Computing

University

of Utah

1616

Style Tip #6

• Turn big problems into small problems- Divide your program into classes- Put each action in its own method- Minimize public variables

University of Utah – School of Computing

University

of Utah

1717

Style Tip #7

• Comment your code!

University of Utah – School of Computing

University

of Utah

1818

Comment Formats// Single-line comment

/* Multiple Line Comment*/

/* Easier * to read * Multiple * Line * comment */

University of Utah – School of Computing

University

of Utah

1919

Comment Risk #1

// Store the value 5// into the variable x

x = 5;

University of Utah – School of Computing

University

of Utah

2020

Comment Risk #2/* Get the two values from the user. * Convert the String input into floating-point * numbers. * We use Java's built-in “parse” methods * to do this. */

String temp = side1TextField.getText();int side1 = Integer.parseInt(temp);temp = side2TextField.getText();float side2 = Float.parseFloat(temp);

/* Now, we compute the hypotenuse of a * right triangle by squaring both sides, * adding them up, then taking the square * root of the sum. */

float side1Squared = side1 * side1;float side2Squared = side1 * side2;float sum = side1Squared + side2Squared;double hypotenuse = Math.sqrt(sum);

University of Utah – School of Computing

University

of Utah

2121

Style Tip #8

• Avoid “magic numbers”!

University of Utah – School of Computing

University

of Utah

2222

Magic Numbers

String message = “”;if (roomNumber == 1)

message = “Welcome to the White Room.”;if (roomNumber == 2)

message = “You tiptoe into the Office.”;if (roomNumber == 3)

message = “This is the Kitchen. Are you hungry?”;if (roomNumber == 4)

message = “You enter the Parlor. Light music is playing.”;if (roomNumber == 5)

message = “Sweating profusely, you enter the Warm Room.”;

University of Utah – School of Computing

University

of Utah

2323

Magic Numbers

final int WHITE_ROOM = 1;final int OFFICE = 2;final int KITCHEN = 3;final int PARLOR = 4;final int WARM_ROOM = 5;

String message = “”;if (roomNumber == WHITE_ROOM)

message = “Welcome to the White Room.”;if (roomNumber == OFFICE)

message = “You tiptoe into the Office.”;if (roomNumber == KITCHEN)

message = “This is the Kitchen. Are you hungry?”;if (roomNumber == PARLOR)

message = “You enter the Parlor. Light music is playing.”;if (roomNumber == WARM_ROOM)

message = “Sweating profusely, you enter the Warm Room.”;

University of Utah – School of Computing

University

of Utah

2424

Style Tip #9

• Use nested “if” statements carefully

University of Utah – School of Computing

University

of Utah

2525

For example...

if (a > b) {if (a > c) {

largest = a;}else {

largest = c;}

}else {

if (b > c) {largest = b;

}else {

largest = c;}

}

University of Utah – School of Computing

University

of Utah

2626

Better!

if (a > b && a > c) {largest = a;

}

if (b > a && b > c) {largest = b;

}

if (c > a && c > b) {largest = c;

}

University of Utah – School of Computing

University

of Utah

2727

Another example...

if (h == 1)house = “Gryffindor”;

else if (h == 2)house = “Slytherin”;

else if (h == 3)house = “Ravenclaw”;

else if (h == 4)house = “Hufflepuff”;

University of Utah – School of Computing

University

of Utah

2828

Improvement?

if (h == 1)house = “Gryffindor”;

else if (h == 2)house = “Slytherin”;

else if (h == 3)house = “Ravenclaw”;

else if (h == 4)house = “Hufflepuff”;

University of Utah – School of Computing

University

of Utah

2929

Still Better...

if (h == 1) {house = “Gryffindor”;

}if (h == 2) {

house = “Slytherin”;}if (h == 3) {

house = “Ravenclaw”;}if (h == 4) {

house = “Hufflepuff”;}

University of Utah – School of Computing

University

of Utah

3030

Yet another way

switch (h) {case 1:house = “Gryffindor”;break;

case 2:house = “Slytherin”;break;

case 3:house = “Ravenclaw”;break;

case 4:house = “Hufflepuff”;break;

}

University of Utah – School of Computing

University

of Utah

3131

Style Tip #10

• Don't optimize prematurely

University of Utah – School of Computing

University

of Utah

3232

Style Tip #11

• Documentation!- Just do it.

University of Utah – School of Computing

University

of Utah

3333

Style Tip #12

• Consistency- dontMix_styles- indent the same everywhere

University of Utah – School of Computing

University

of Utah

3434

Style Tip #13

• Don't be too clever.

University of Utah – School of Computing

University

of Utah

3535

A Quote

• "Consider two programs. One was written by a clever programmer, using all the tricks. The program contains no comments, but it works. The other is nicely commented and well-structured, but doesn't work. Which program is more useful? In the long run, the "broken" one is more useful because it can be fixed and maintained easily. Although the clever one works now, sooner or later it will have to be modified. The hardest work you will ever have to do is modifying a cleverly written program."

From page XV of "Practical C++ Programming" by Steve Oualline

University of Utah – School of Computing

University

of Utah

3636

Example:Operator Precedence

Operators Precedence

postfix expr++ expr--

unary ++expr --expr +expr -expr ~ !

multiplicative * / %

additive + -

shift << >> >>>

relational < > <= >= instanceof

equality == !=

bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

logical AND &&

logical OR ||

ternary ? :

assignment = += -= *= /= %= &= ^= |= <<=>>= >>>=

University of Utah – School of Computing

University

of Utah

3737

Operator Precedence:Simplifed!

• Two rules:- Multiplication and division before addition

and subtraction- Use parentheses for everything else

University of Utah – School of Computing

University

of Utah

3838

And nowfor something

completelydifferent!

University of Utah – School of Computing

University

of Utah

3939

Magic Numbers:Revisited

final int WHITE_ROOM = 1;final int OFFICE = 2;final int KITCHEN = 3;final int PARLOR = 4;final int WARM_ROOM = 5;

String message = “”;if (roomNumber == WHITE_ROOM)

message = “Welcome to the White Room.”;if (roomNumber == OFFICE)

message = “You tiptoe into the Office.”;if (roomNumber == KITCHEN)

message = “This is the Kitchen. Are you hungry?”;if (roomNumber == PARLOR)

message = “You enter the Parlor. Light music is playing.”;if (roomNumber == WARM_ROOM)

message = “Sweating profusely, you enter the Warm Room.”;

University of Utah – School of Computing

University

of Utah

4040

Magic Numbers:Revisited

final int WHITE_ROOM = 1;final int OFFICE = 2;final int KITCHEN = 3;final int PARLOR = 4;final int WARM_ROOM = 5;

String message = “”;if (roomNumber == WHITE_ROOM)

message = “Welcome to the White Room.”;if (roomNumber == OFFICE)

message = “You tiptoe into the Office.”;if (roomNumber == KITCHEN)

message = “This is the Kitchen. Are you hungry?”;if (roomNumber == PARLOR)

message = “You enter the Parlor. Light music is playing.”;if (roomNumber == WARM_ROOM)

message = “Sweating profusely, you enter the Warm Room.”;

roomNumber = -1;

University of Utah – School of Computing

University

of Utah

4141

Magic Numbers:Revisited

final int WHITE_ROOM = 1;final int OFFICE = 2;final int KITCHEN = 3;final int PARLOR = 4;final int WARM_ROOM = 5;

String message = “”;if (roomNumber == WHITE_ROOM)

message = “Welcome to the White Room.”;if (roomNumber == OFFICE)

message = “You tiptoe into the Office.”;if (roomNumber == KITCHEN)

message = “This is the Kitchen. Are you hungry?”;if (roomNumber == PARLOR)

message = “You enter the Parlor. Light music is playing.”;if (roomNumber == WARM_ROOM)

message = “Sweating profusely, you enter the Warm Room.”;

int weird = OFFICE + KITCHEN;

University of Utah – School of Computing

University

of Utah

4242

A Better Way

enum Room { WHITE_ROOM, OFFICE, KITCHEN, PARLOR, WARM_ROOM

}

String message = “”;if (roomID == Room.WHITE_ROOM)

message = “Welcome to the White Room.”;if (roomID == Room.OFFICE)

message = “You tiptoe into the Office.”;if (roomID == Room.KITCHEN)

message = “This is the Kitchen. Are you hungry?”;if (roomID == Room.PARLOR)

message = “You enter the Parlor. Light music is playing.”;if (roomID == Room.WARM_ROOM)

message = “Sweating profusely, you enter the Warm Room.”;

University of Utah – School of Computing

University

of Utah

4343

Enums are safer

Room room1 = Room.OFFICE;Room room2 = Room.KITCHEN;

Room weird = room1 + room2; //ILLEGAL

Room room3 = 3; //ILLEGAL

Room bureau = “OFFICE”; //ILLEGAL

University of Utah – School of Computing

University

of Utah

4444

Coming Tomorrow:

• More advanced features!

University of Utah – School of Computing

University

of Utah

4545

Reminders

• Homework 8 due tomorrow, July 31• FINAL EXAM THURSDAY @ 07:30