1 University of Utah – School of Computing Computer Science 1021 "Programming with Style"
University of Utah – School of Computing University of Utah 2 No one programs in a vacuum.
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
University of Utah – School of Computing University of Utah 4 So... Make your programs easy to understand Don't be too “clever!”
University of Utah – School of Computing University of Utah 5 Style Tip #1 Indent consistently
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());}}
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());}}
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()); }
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
University of Utah – School of Computing University of Utah 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()); }
University of Utah – School of Computing University of Utah Style Tip #3 Don't make methods too long
University of Utah – School of Computing University of Utah 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 Style Tip #5 Variables on top Methods below
University of Utah – School of Computing University of Utah 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; }
University of Utah – School of Computing University of Utah 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 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 Style Tip #7 Comment your code!
University of Utah – School of Computing University of Utah Comment Formats // Single-line comment /* Multiple Line Comment */ /* Easier * to read * Multiple * Line * comment */
University of Utah – School of Computing University of Utah Comment Risk #1 // Store the value 5 // into the variable x x = 5;
University of Utah – School of Computing University of Utah 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 Style Tip #8 Avoid “magic numbers”!
University of Utah – School of Computing University of Utah 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 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 Style Tip #9 Use nested “if” statements carefully
University of Utah – School of Computing University of Utah 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 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 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 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 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 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 Style Tip #10 Don't optimize prematurely
University of Utah – School of Computing University of Utah Style Tip #11 Documentation! -Just do it.
University of Utah – School of Computing University of Utah Style Tip #12 Consistency -dontMix_styles -indent the same everywhere
University of Utah – School of Computing University of Utah Style Tip #13 Don't be too clever.
University of Utah – School of Computing University of Utah 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 Example: Operator Precedence
University of Utah – School of Computing University of Utah 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 And now for something completely different!
University of Utah – School of Computing University of Utah 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 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 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 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 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 Coming Tomorrow: More advanced features!
University of Utah – School of Computing University of Utah Reminders Homework 8 due tomorrow, July 31 FINAL EXAM 07:30