Design Issues. Where to put class definitions  What goes in a source file? At most 1 public class At most 1 public class Other “helper” classes as needed.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
Chapter 1: Computer Systems
Written by: Dr. JJ Shepherd
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Utilities (Part 3) Implementing static features 1.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Information Hiding and Encapsulation
1 Doc Comment Conventions. 2 Write for your audience Rule 32: Write documentation for– those who must use your code Users should not need to care how.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
CSC 395 – Software Engineering Lecture 21: Overview of the Term & What Goes in a Data Dictionary.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMING PRACTICES API documentation.
Computer Science 340 Software Design & Testing Design By Contract.
Component-Based Software Engineering Components and Interfaces Paul Krause.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
JavaDoc1 JavaDoc DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY July 24, 2006 by Emil Vassev & Joey Paquet revision 1.2 –
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
The Java Programming Language
1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Utilities (Part 2) Implementing static features 1.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Documentation javadoc. Documentation not a programmer's first love lives in a separate file somewhere usually a deliverable on the schedule often not.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
JavaDoc and Contracts Spring Documenting Contracts with JavaDoc Contract model for methods Preconditions Postconditions JavaDoc Industry standard.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Written by: Dr. JJ Shepherd
“Discipline is the refining fire by which talent becomes ability.” – Roy L. Smith Thought for the Day.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Information and Computer Sciences University of Hawaii, Manoa
Advanced Programing practices
Working with Java.
Chapter 6 CS 3370 – C++ Functions.
Logger, Assert and Invariants
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
Design by Contract Fall 2016 Version.
CSC 480 Software Engineering
Chapter 1: Computer Systems
CSE 1030: Data Structure Mark Shtern.
Defining Classes and Methods
JavaDoc and Contracts Fall 2008.
Advanced Programing practices
Focus of the Course Object-Oriented Software Development
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Chap 2. Identifiers, Keywords, and Types
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Design Issues

Where to put class definitions  What goes in a source file? At most 1 public class At most 1 public class Other “helper” classes as needed (list/node) Other “helper” classes as needed (list/node) All classes have a unique.class file All classes have a unique.class file  What goes in a package? Related classes, duh Related classes, duh Many files, though Many files, though

Arranging Class Members  Pick a style and stick with it In other words, who cares? In other words, who cares?  Many like public-to-private continuum So can see class interface first So can see class interface first  With javadoc, these aren’t crucial issues  Be readable! Use white space (indent, etc.) Use white space (indent, etc.) Not too many comments! (“Say it in code”) Not too many comments! (“Say it in code”)

Avoid Public Fields  At all costs! Except static final primitives (constants) Except static final primitives (constants)  Don’t be tempted to use struct-like records  Will cause race conditions with threading  Beware setter methods! Can throw off object state Can throw off object state

javadoc  “javadoc” command  Documents classes by reading /**-style comments Immediately before class and method definitions Immediately before class and method definitions  Recognizes

Invariants  Conditions that do not vary At certain points in your code At certain points in your code  Loop invariants True at beginning of loop and after termination True at beginning of loop and after termination  Class invariants True before and after each method call True before and after each method call  Method invariants Pre- and post conditions Pre- and post conditions Part of “Design-by-contract” Part of “Design-by-contract”

Class Invariants  All constructors should place their object in a valid state  All methods should leave their object in a valid state The pre-condition and post-condition together should guarantee this The pre-condition and post-condition together should guarantee this Better than just blind coding and testing! Better than just blind coding and testing!  Example: Rational: denominator > 0

Loop Invariants  Part of program correctness proofs Mostly an academic exercise Mostly an academic exercise  Often conceptual Should be used more often! Should be used more often! Must be commented instead of tested Must be commented instead of tested  Example: file merge

Enforcing Invariants  Remember, they are design invariants Don’t depend on user Don’t depend on user If they fail, you have a bug (find early!) If they fail, you have a bug (find early!)  Use Assertions assert statement assert statement Takes a boolean expression Takes a boolean expression If false, throws an exception If false, throws an exception Since JDK 1.4 (javac –source 1.4 …) Since JDK 1.4 (javac –source 1.4 …)

Assertion Example public void add(Object o) { if (count == data.length) { // Grow the array Object[] newData = new Object[data.length + CHUNK]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; } assert count < data.length : "stupid programmer error"; data[count++] = o; }

Class “Canonical Form”  Useful for creating “value types” Concrete objects that can be assigned, copied, read, written, etc. Concrete objects that can be assigned, copied, read, written, etc. Not for all classes Not for all classes  Include: No-arg constructor No-arg constructor Override of Object.equals and Object.hashCode Override of Object.equals and Object.hashCode Override of Object.toString Override of Object.toString Implement Cloneable and Serializable Implement Cloneable and Serializable

Design Patterns  Codifications of solutions for common design problems How to allow only one object of a class (Singleton)How to allow only one object of a class (Singleton) How to automate polymorphic object creation (Factory)How to automate polymorphic object creation (Factory) How to handle recursive containment relationships (directories/files; Composite)How to handle recursive containment relationships (directories/files; Composite)  Language independent  Object-oriented

The Proxy Design Pattern  A Proxy class “stands in” for an actual implementation class Holds a reference to the real implementation Holds a reference to the real implementation Can switch implementations at runtime Can switch implementations at runtime  Both Proxy and the real implementations implement the interface of interest The proxy just forwards the work to the implementation The proxy just forwards the work to the implementation

Looking at Proxy See file ProxyTest.java

Unit Testing  The fundamental unit is the class  What a programmer does to gain confidence that his classes meet requirements and are well behaved  Test everything that could possibly break Rarely test getters and setters Rarely test getters and setters Rarely should have them!Rarely should have them!  Should be automated Don’t inspect detailed output Don’t inspect detailed output Let the computer do the testing for you! Let the computer do the testing for you!