Code Clarity - Comments, Preconditions and Postconditions Cmput 115 - Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.

Slides:



Advertisements
Similar presentations
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Advertisements

Types and Arithmetic Operators
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Software Engineering and Design Principles Chapter 1.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Data Abstraction - Interfaces and Implementations Cmput Lecture 1 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Exceptions Cmput Lecture 3 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Fall 2007CS 2251 Software Engineering Intro. Fall 2007CS 2252 Topics Software challenge Life-cycle models Design Issues Documentation Abstraction.
Information Hiding and Encapsulation
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Describing Syntax and Semantics
UNIT II Decision Making And Branching Decision Making And Looping
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Ranga Rodrigo. Class is central to object oriented programming.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
CS Fall 2007 Dr. Barbara Boucher Owens. CS 2 Text –Main, Michael. Data Structures & Other Objects in Java Third Edition Objectives –Master building.
The Java Programming Language
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
Lecture #5 Introduction to C++
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Programming with Visual C++: Concepts and Projects Chapter 2B: Reading, Processing and Displaying Data (Tutorial)
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Part II Describing Syntax and Semantics.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Unit 1 Review By: Mr. Jacobs.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Windows Programming Lecture 03. Pointers and Arrays.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
CS0007: Introduction to Computer Programming
Chapter 6: Modular Programming
Classes and Objects 2nd Lecture
Slides by Steve Armstrong LeTourneau University Longview, TX
CMSC 202 Java Primer 2.
Introduction to Primitive Data types
Building Java Programs
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Building Java Programs
Variables and Constants
Presentation transcript:

Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 12/16/99

©Duane Szafron About This Lecture In this lecture we will learn how to write Java code that is easy to understand, both as it is being written and later when it is being read.

©Duane Szafron Outline Comments Preconditions and Postconditions Assertions

©Duane Szafron Comments A comment is a an annotation that is added to program code that is ignored by the compiler and run-time system. For example: public class Ratio { /* an object for storing a fraction */ The first comment indicates that the code has been taken from the textbook. The second comment describes the purpose of the class. code based on Bailey pg. 8

©Duane Szafron Good Comments and Bad Comments A good comment is one that is cherished by a programmer for the clarity it brings. public class Ratio { /* an object for storing a fraction like 2/3 */ code based on Bailey pg. 8 A neutral comment is one that neither helps nor hinders the programmer. protected int numerator; // numerator of ratio A bad comment is one that confuses or inhibits the programmer. public class Ratio { /* this class is tricky: talk to Bob, it was his brainwave.*/

©Duane Szafron The Purpose of Comments A comment should describe what a program segment should do and under what circumstances it should do it (when). Comments should be created as soon as a designer or programmer knows the functionality of the segment being designed or implemented, not after! A comment allows a programmer to check for the desired functionality as the code is being written.

©Duane Szafron Sample Comment - What and When public class Ratio { /* an object for storing a fraction like 2/3 */ public Ratio(int top, int bottom) /* Initialize the receiver to be the fraction whose numerator is the top and whose denominator is the bottom. The bottom cannot be zero. */ what when code based on Bailey pg. 8

©Duane Szafron good name, no comment required Comment Granularity - variables poor name, comment required Should every variable declaration be commented? –If the name of a variable makes its purpose clear, it does not need a comment. –If the name does not make it clear, consider a better name rather than a comment. –The name “follows” a variable to all of its uses, the comment does not. protected int n; // numerator of ratio protected int numerator; code based on Bailey pg. 8

©Duane Szafron Comment Granularity - code lines 2 Should every line of code be commented? –If the names of variables and names of methods make the purpose clear, it does not need a comment. for (int i = 0; i < c; i++) l[++e] = s.l[s.s++]; for (int index = 0; index < count; index++) this.list[++this.end] = source.list[source.start++];

©Duane Szafron Comment Granularity - code lines 3 –If a line is still complex enough to need a comment, consider splitting it into multiple lines. for (int index = 0; index < count; index++) this.list[++this.end] = source.list[source.start++]; for (int index = 0; index < count; index++) { this.end++; this.list[this.end] = source.list[source.start]; this.start++; }

©Duane Szafron Comment Granularity - classes and methods Should every class be commented? –Yes! Should every method be commented? –Yes!

©Duane Szafron The Language for Comments Natural language is not very precise at describing program functionality. Natural language is also verbose. Mathematics is more precise, but not always as easy to understand. In this course, we will use a semi-formal notation called preconditions and postconditions for our method comments.

©Duane Szafron Preconditions A precondition tells when (under what circumstances) a method can be called: code based on Bailey pg. 8 public Ratio(int top, int bottom) /* Initialize the receiver to be the fraction whose numerator is the top and whose denominator is the bottom. The bottom cannot be zero. pre: bottom != 0 */

©Duane Szafron Postconditions A postcondition tells what a method must do: code based on Bailey pg. 8 public Ratio(int top, int bottom) /* Initialize the receiver to be the fraction whose numerator is the top and whose denominator is the bottom. pre: bottom != 0 post: constructs a ratio equivalent to top/bottom */

©Duane Szafron Method Comment Format The preconditions and postconditions form the comment for the method. code based on Bailey pg. 8 public Ratio(int top, int bottom) /* pre: bottom != 0 post: constructs a ratio equivalent to top/bottom */

©Duane Szafron Assertions An assertion is a statement about the state of your program that must be true. Preconditions and postconditions can be expressed as assertions. Some assertions may be represented as boolean expressions that can actually be evaluated when the program is run.

©Duane Szafron Checking Assertions at run-time In this course, we will use a class called Assert to check assertions. Four static methods are provided in this class: –pre(boolean, String) –post(boolean, String) –condition(boolean, String) –fail(String)

©Duane Szafron Assert Class static public void post(boolean test, String message) //pre: result of postcondition test //post: does nothing if test is true, otherwise // abort w/message code based on Bailey pg. 27 Public class Assert { static public void pre(boolean test, String message) //pre: result of precondition test //post: does nothing if test is true, otherwise // abort w/message

©Duane Szafron Assert Class - continued Public class Assert static public void condition(boolean test, String message) //pre: result of general condition test //post: does nothing if test is true, otherwise abort w/message static public void fail(String message) //post: throws error w/message } code based on Bailey pg. 27

©Duane Szafron Using the Assert Class public Ratio(int top, int bottom) { /* pre: bottom != 0 post: constructs a ratio equivalent to top/bottom */ Assert.pre(bottom != 0, "Denominator must not be 0"); this.numerator = top; this.denominator = bottom; } code based on Bailey pg. 8

©Duane Szafron Assertion Failure Example _exceptionOccurred: structure.FailedPrecondition ( Assertion that failed: A precondition: dr >= 0 or abs(dr) <= r) structure.FailedPrecondition: Assertion that failed: A precondition: dr >= 0 or abs(dr) <= r at structure.Assert.pre(Assert.java) at PolarPoint.transform(PolarPoint.java) at TestPlanarPoint.testTransform(TestPlanarPoint.java) at TestPlanarPoint.main(TestPlanarPoint.java)

©Duane Szafron Key Principle from the Textbook 5. Test assertions in your code. principles from Bailey ch. 2