Software Engineering Techniques



Similar documents
Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Software Construction

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Design by Contract beyond class modelling

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Computer Programming I

The Rules 1. One level of indentation per method 2. Don t use the ELSE keyword 3. Wrap all primitives and Strings

GContracts Programming by Contract with Groovy. Andre Steingress

J a v a Quiz (Unit 3, Test 0 Practice)

Approach of Unit testing with the help of JUnit

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Chapter 4: Design Principles I: Correctness and Robustness

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Designing and Writing a Program DESIGNING, CODING, AND DOCUMENTING. The Design-Code-Debug Cycle. Divide and Conquer! Documentation is Code

Chapter 1: Key Concepts of Programming and Software Engineering

Tutorial on Writing Modular Programs in Scala

General Software Development Standards and Guidelines Version 3.5

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Course Title: Software Development

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Install Java Development Kit (JDK) 1.8

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Tool-Assisted Unit-Test Generation and Selection Based on Operational Abstractions

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Introduction to Triggers using SQL

Stacks. Linear data structures

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Crash Course in Java

7.1 Our Current Model

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

CS193j, Stanford Handout #10 OOP 3

Basic Programming and PC Skills: Basic Programming and PC Skills:

Computing Concepts with Java Essentials

LAB4 Making Classes and Objects

Unit-testing with JML

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Programming Languages CIS 443

CS 241 Data Organization Coding Standards

CS 111 Classes I 1. Software Organization View to this point:

Inheritance, overloading and overriding

Lecture 9. Semantic Analysis Scoping and Symbol Table

Programming by Contract vs. Defensive Programming: A Comparison of Run-time Performance and Complexity

Unit 6. Loop statements

Java Interview Questions and Answers

INSTALL NOTES Elements Environments Windows 95 Users

On the Relation between Design Contracts and Errors: A Software Development Strategy

AP Computer Science Java Subset

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Chapter 12 Programming Concepts and Languages

Java CPD (I) Frans Coenen Department of Computer Science

Rigorous Software Development CSCI-GA

Java Coding Practices for Improved Application Performance

The C Programming Language course syllabus associate level

Quotes from Object-Oriented Software Construction

CS 106 Introduction to Computer Science I

PL/SQL Practicum #2: Assertions, Exceptions and Module Stability

TECHNICAL UNIVERSITY OF CRETE DATA STRUCTURES FILE STRUCTURES

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Fundamentals of Java Programming

Facebook Twitter YouTube Google Plus Website

Assignment # 2: Design Patterns and GUIs

Chapter 8 Software Testing

BUSINESS RULES CONCEPTS... 2 BUSINESS RULE ENGINE ARCHITECTURE By using the RETE Algorithm Benefits of RETE Algorithm...

Jonathan Worthington Scarborough Linux User Group

LINKED DATA STRUCTURES

Data Structures in the Java API

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Code Refactoring and Defects

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Official Android Coding Style Conventions

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

CSC 551: Web Programming. Fall 2001

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

vector vec double # in # cl in ude <s ude tdexcept> tdexcept> // std::ou std t_of ::ou _range t_of class class V Vector { ector {

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Curriculum Map. Discipline: Computer Science Course: C++

Java Programming Fundamentals

Moving from CS 61A Scheme to CS 61B Java

Software Design. Software Design. Software design is the process that adds implementation details to the requirements.

Lewis, Loftus, and Cocking. Java Software Solutions for AP Computer Science 3rd Edition. Boston, Mass. Addison-Wesley, 2011.

Transcription:

Software Engineering Techniques Low level design issues for programming-in-the-large. Software Quality Design by contract Pre- and post conditions Class invariants Ten do Ten do nots Another type of summary OOP: Software Engineering Techniques 1

Software Quality Correctness: Is the ability of software to exactly perform their tasks, as defined by the requirements and specifications. Robustness: Is the ability of software to function even in abnormal conditions. Extendibility: Is the ease with which software may be adapted to changes of specifications. Reusability: Is the ability of software to be reused, in whole or in part for new applications. Compatible: Is the ease with which software may be combined with others software. OOP: Software Engineering Techniques 2

Other Software Quality Efficiency: Is the good use of hardware resources. Portability: Is the ease with which software may be transferred to various hardware and software environments. Verifiability: Is the ease of preparing acceptance procedures, e.g., test data and methods for finding bugs and tracing the bugs. Integrity: Is the ability of software to protect its components against unauthorized access and modification. Ease of use: Is the ease of learning how to use the software, operating it, preparing input data, interpreting results and recovering from errors. OOP: Software Engineering Techniques 3

Design By Contract Purpose: To increase software quality by giving each part of a software product certain obligations and benefits. Without contract All parts of a program take a huge responsibility All parts of a program check for all possible error possibilities (called defensive programming). This makes a large program larger and more complicated With contracts Methods can make assumptions Fewer checks for errors possibilities This makes a large program simpler. OOP: Software Engineering Techniques 4

Design By Contract, Example A stack example the push method. Client programmer Obligation: Only call push(x) on a non-full stack Benefit: Gets x added on top of stack. Class programmer Obligation: Make sure that x is pushed on the stack. Benefit: No need to check for the case that the stack is already full Think Win-Win! OOP: Software Engineering Techniques 5

Pre and Postconditions A precondition expresses the constraints under which a method will function properly. The responsibility of the caller to fulfill the precondition. A postcondition expresses properties of the state resulting from a method's execution. The responsibility of the method to fulfill the postcondition Both preconditions and postconditions are expressed using logical expressions also called assertions. Other issues Class invariantss Loop invariants OOP: Software Engineering Techniques 6

Java 1.4's assert Keyword An assertion is a boolean expression that a developer specifically proclaims to be true during program runtime execution [Source: java.sun.com]. New to Java 1.4. Used for expressing both pre- and postconditions. Syntax: assert expression1; assert expression1 : expression2; OOP: Software Engineering Techniques 7

Java 1.4's assert Keyword, cont. Evaluation of an assert statement. Evaluate expression1 if true no further action else if expression2 exists Evaluate expression2 and use the result in a single-parameter form of the AssertionError constructor else Use the default AssertionError constructor OOP: Software Engineering Techniques 8

assert, Examples assert 0 <= value; assert 0 <= value : "Value must be positive " + value; assert ref!= null; assert ref!= null : "Ref is null in myfunc"; assert newcount == (oldcount + 1); assert myobject.myfunc(myparam1, myparam1 ); OOP: Software Engineering Techniques 9

Pre- and Postcondition, Example import java.util.*; public class AStack{ private LinkedList stck = new LinkedList(); private final int no = 42; public boolean full() { if (stck.size() >= no) return true; else return false; public boolean empty() { return!full(); public void push(object v) { // precondition assert!full(): "Stack is full"; stck.addfirst(v); // postconditions assert!empty(); assert top().equals(v); // check no of elements increase by one OOP: Software Engineering Techniques 10

Pre- and Postcondition, Example public Object top() { assert!empty(); return stck.getfirst(); // no post conditions public Object pop() { assert!empty(); return stck.removefirst(); assert!full(); // check no of elements decrease by one public static void main(string[] args) { AStack as = new AStack(); OOP: Software Engineering Techniques 11

assert and Inheritance class Base{ public void mymethod (boolean val){ assert val : "Assertion failed: val is " + val; System.out.println ("OK"); public class Derived extends Base { public void mymethod (boolean val){ assert val : "Assertion failed: val is " + val; System.out.println ("OK"); public static void main (String[] args){ try { Derived derived = new Derived();... OOP: Software Engineering Techniques 12

assert and Inheritance, cont Preconditions cannot be strengthened in subclasses. Postconditions cannot be weakened in subclasses. OOP: Software Engineering Techniques 13

Class Invariant A class invariant is an expression that must be fulfilled by all objects of the class at all stable times in the lifespan of an object After object creation Before execution a public method After execution of a public method A class invariant is extra requirement on the pre and postconditions of methods. Class invariants can be used to express consistency checks between the data representation and the method of a class, e.g., after if a stack is empty then size of the linked list is zero. Class invariants cannot be weakened in subclasses. Not supported in Java. OOP: Software Engineering Techniques 14

Logical naming Ten Dos Class name p3452 vs. class name Vehicle The foundation for reuse! Symmetry If a get method then also a set method If an insert method then also a delete method Makes testing easier. To avoid "surprises" for the clients. Add extra parameters to increase flexibility split (string str) vs. split (string str, char ch default ' ') To anticipate "small" changes. OOP: Software Engineering Techniques 15

Ten Dos Set a maximum line size (80-100 characters) To avoid more the one thing being done in the same line of code To be able to print the code with out wrapping. For code reviews Set the maximum of lines for a method What can be shown on a screen (30-60 lines) To increase readability To increase modularity Indent your code Increases readability Avoid side-effects If a method refers to an object in a database and the object does not exist then raise and error do not create the object. Make program logic impossible to understand OOP: Software Engineering Techniques 16

Add comments in method Ten Dos Comment where you are puzzled yourself or is puzzled the day after you wrote the code Do not comment the obvious! Look at (and comment on) other peoples code Code reviews are a good investment Increases readability of code A good way to learn from each other Be consistent Can automate global changes with scripts OOP: Software Engineering Techniques 17

Ten Do Nots Make a method do more than one thing split_and_store (string str, char ch) vs. split (string str, char ch) and store (string_array) Makes the method more complicated Decreases reuse Make a method take more than 7±2 parameters Can parameters be clustered in objects? Make more than 4 level of nesting in a method if {if{if{if{if Decreases readability Make use of "magic" numbers WHERE employee.status = '1' vs WHERE employee.status = global.open OOP: Software Engineering Techniques 18

Ten Do Nots Make use of Copy-and-Paste facilities Redundant code Make a new method or use inheritance Become mad and aggressive if some one suggest changes to your code. Have more than one return statement in a function Skip exception handling Skip testing Assume the requirement specification is stable OOP: Software Engineering Techniques 19

Summary Any fool can write code that a computer can understand. Good programmers write code that humans can understand. (Fowler) Debug only code - comments can lie. If you have too many special cases, you are doing it wrong. Get your data structures correct first, and the rest of the program will write itself. Testing can show the presence of bugs, but not their absence. The first step in fixing a broken program is getting it to fail repeatedly. The fastest algorithm can frequently be replaced by one that is almost as fast and much easier to understand. OOP: Software Engineering Techniques 20

Summary, cont. The cheapest, fastest, and most reliable components of a computer system are those that are not there. Good judgement comes from experience, and experience comes from bad judgement. Do not use the computer to do things that can be done efficiently by hand. It is faster to make a four-inch mirror then a six-inch mirror than to make a six-inch mirror. [Thompson's Rule for first-time telescope makers] If you lie to the computer, it will get you. Inside of every large program is a small program struggling to get out. OOP: Software Engineering Techniques 21