Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 University of Utah – School of Computing Computer Science 1021 "Programming with Style""— Presentation transcript:

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

2 University of Utah – School of Computing University of Utah 2 No one programs in a vacuum.

3 University of Utah – School of Computing University of Utah 3 In Real Life... Other people will read your code Other people will maintain your code Other people will reuse your code in new programs

4 University of Utah – School of Computing University of Utah 4 So... Make your programs easy to understand Don't be too “clever!”

5 University of Utah – School of Computing University of Utah 5 Style Tip #1 Indent consistently

6 University of Utah – School of Computing University of Utah 6 No 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());}}

7 University of Utah – School of Computing University of Utah 7 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());}}

8 University of Utah – School of Computing University of Utah 8 Good 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()); }

9 University of Utah – School of Computing University of Utah 9 Style Tip #2 Use blank lines wisely -between each method -between each important part of a method

10 University of Utah – School of Computing University of Utah 10 10 Spacing 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()); }

11 University of Utah – School of Computing University of Utah 11 11 Style Tip #3 Don't make methods too long

12 University of Utah – School of Computing University of Utah 12 12 Style Tip #4 Use standard naming conventions -Class names: First letter capitalized -Variable names: First letter lowercase -“final” variables: ALL_CAPS

13 University of Utah – School of Computing University of Utah 13 13 Style Tip #5 Variables on top Methods below

14 University of Utah – School of Computing University of Utah 14 14 Not wrong, just weird public 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; }

15 University of Utah – School of Computing University of Utah 15 15 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; }

16 University of Utah – School of Computing University of Utah 16 16 Style Tip #6 Turn big problems into small problems -Divide your program into classes -Put each action in its own method -Minimize public variables

17 University of Utah – School of Computing University of Utah 17 17 Style Tip #7 Comment your code!

18 University of Utah – School of Computing University of Utah 18 18 Comment Formats // Single-line comment /* Multiple Line Comment */ /* Easier * to read * Multiple * Line * comment */

19 University of Utah – School of Computing University of Utah 19 19 Comment Risk #1 // Store the value 5 // into the variable x x = 5;

20 University of Utah – School of Computing University of Utah 20 20 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);

21 University of Utah – School of Computing University of Utah 21 21 Style Tip #8 Avoid “magic numbers”!

22 University of Utah – School of Computing University of Utah 22 22 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.”;

23 University of Utah – School of Computing University of Utah 23 23 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.”;

24 University of Utah – School of Computing University of Utah 24 24 Style Tip #9 Use nested “if” statements carefully

25 University of Utah – School of Computing University of Utah 25 25 For example... if (a > b) { if (a > c) { largest = a; } else { largest = c; } else { if (b > c) { largest = b; } else { largest = c; }

26 University of Utah – School of Computing University of Utah 26 26 Better! if (a > b && a > c) { largest = a; } if (b > a && b > c) { largest = b; } if (c > a && c > b) { largest = c; }

27 University of Utah – School of Computing University of Utah 27 27 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”;

28 University of Utah – School of Computing University of Utah 28 28 Improvement? if (h == 1) house = “Gryffindor”; else if (h == 2) house = “Slytherin”; else if (h == 3) house = “Ravenclaw”; else if (h == 4) house = “Hufflepuff”;

29 University of Utah – School of Computing University of Utah 29 29 Still Better... if (h == 1) { house = “Gryffindor”; } if (h == 2) { house = “Slytherin”; } if (h == 3) { house = “Ravenclaw”; } if (h == 4) { house = “Hufflepuff”; }

30 University of Utah – School of Computing University of Utah 30 30 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; }

31 University of Utah – School of Computing University of Utah 31 31 Style Tip #10 Don't optimize prematurely

32 University of Utah – School of Computing University of Utah 32 32 Style Tip #11 Documentation! -Just do it.

33 University of Utah – School of Computing University of Utah 33 33 Style Tip #12 Consistency -dontMix_styles -indent the same everywhere

34 University of Utah – School of Computing University of Utah 34 34 Style Tip #13 Don't be too clever.

35 University of Utah – School of Computing University of Utah 35 35 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

36 University of Utah – School of Computing University of Utah 36 36 Example: Operator Precedence

37 University of Utah – School of Computing University of Utah 37 37 Operator Precedence: Simplifed! Two rules: -Multiplication and division before addition and subtraction -Use parentheses for everything else

38 University of Utah – School of Computing University of Utah 38 38 And now for something completely different!

39 University of Utah – School of Computing University of Utah 39 39 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.”;

40 University of Utah – School of Computing University of Utah 40 40 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;

41 University of Utah – School of Computing University of Utah 41 41 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;

42 University of Utah – School of Computing University of Utah 42 42 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.”;

43 University of Utah – School of Computing University of Utah 43 43 Enums are safer Room room1 = Room.OFFICE; Room room2 = Room.KITCHEN; Room weird = room1 + room2; //ILLEGAL Room room3 = 3;//ILLEGAL Room bureau = “OFFICE”;//ILLEGAL

44 University of Utah – School of Computing University of Utah 44 44 Coming Tomorrow: More advanced features!

45 University of Utah – School of Computing University of Utah 45 45 Reminders Homework 8 due tomorrow, July 31 FINAL EXAM THURSDAY @ 07:30


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

Similar presentations


Ads by Google