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

Slides:



Advertisements
Similar presentations
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
15-Jun-15 Beginning Style. 2 Be consistent! Most times, you will enter an ongoing project, with established style rules Follow them even if you don’t.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
1 University of Utah – School of Computing Computer Science 1021 "Object-Oriented Programming"
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
1 University of Utah – School of Computing Computer Science 1020 "Object-Oriented Programming"
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Program Style Chapter 22 IB103 Week 12 (part 2). Modularity: the ability to reuse code Encapsulation: hide data access directly but may use methods (the.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Making Interactive Programs with Visual Basic .NET
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
CS 106A, Lecture 4 Introduction to Java
Working with Java.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Computing and Statistical Data Analysis Lecture 2
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
Presented By S.Yamuna AP/IT
Introduction to C++ October 2, 2017.
Python Programming Language
Number and String Operations
Chapter 1: Computer Systems
MSIS 655 Advanced Business Applications Programming
Units with – James tedder
Beginning Style 27-Feb-19.
Python Programming Language
Fundamental Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Unit 3: Variables in Java
Python Programming Language
Just Enough Java 17-May-19.
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

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