Software Engineering. Top-Down Design. Bottom-Up Design. Software Process. Top-Down vs. Bottom-Up 15/06/2011

Similar documents
So#ware Engineering I. Based on materials by Ken Birman, Cornell

Overview CS Our Challenge. But it isn t always so easy! making display easy. A more complex example 23/08/2009

CS Software Design Principles I

SOFTWARE ENGINEERING. Lecture 4 CS2110 Spring 2013

... picking up where we stopped SOFTWARE ENGINEERING. Interfaces 28/01/2013. Example: Overriding tostring. Example: Overriding tostring

Test-Driven Development (TDD)

Test First Software Development

Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (testing whatever occurs to you at

These are notes for the third lecture; if statements and loops.

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

Stage 11 Array Practice With. Zip Code Encoding

Unit Testing as Hypothesis Testing

EECS 4313 Software Engineering Testing

Introduction to Programming

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

A few more things about Agile and SE. Could help in interviews, but don t try to bluff your way through

Test Driven Development TDD

Exercises: Instructions and Advice

Tools for Unit Test - JUnit

Credit where Credit is Due. Lecture 29: Test-Driven Development. Test-Driven Development. Goals for this lecture

Software Testing Lecture 1. Justin Pearson

3 Continuous Integration 3. Automated system finding bugs is better than people

1: Introduction to Object (1)

Iteration Preview of Coming Attractions The Need to Get Your Act Together A Process for Developing While Statements

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012

The compiler is spewing error messages.

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

Test Driven Development (TDD)

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Administrivia. Added 20 more so far. Software Process. Only one TA so far. CS169 Lecture 2. Start thinking about project proposal

Refactoring. Section (JIA s) OTHER SOURCES

Agile Software Development. Lecture 7: Software Testing

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

Analysis of the Test Driven Development by Example

Thinking Functionally

Unit Testing as Hypothesis Testing

JUnit in EDA Introduction. 2 JUnit 4.3

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

CS 161 Computer Security

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Software Engineering I (02161)

Test-driven development

Tools for Unit Test JUnit

Badge#8: Geek Refactoring

The Dynamic Typing Interlude

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Software Engineering I (02161)

Project 3: Implementing a List Map

Lab Exercise Test First using JUnit

Introduction to Programming Style

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing


09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Testing. UW CSE 160 Winter 2016

Lecture 4 CSE July 1992

Introduction to Extreme Programming

Lecture 5: Implementing Lists, Version 1

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

COURSE 11 DESIGN PATTERNS

TEST-DRIVEN DEVELOPMENT

EXAMINING THE CODE. 1. Examining the Design and Code 2. Formal Review: 3. Coding Standards and Guidelines: 4. Generic Code Review Checklist:

Data Structure Series

Reliable programming

All code must follow best practices. Part (but not all) of this is adhering to the following guidelines:

INTRODUCTION TO SOFTWARE ENGINEERING

Testing. My favourite testing quote: Program testing can be used to show the presence of bugs, but never to show their absence!

Building in Quality: The Beauty of Behavior Driven Development (BDD) Larry Apke - Agile Coach

Topics in Software Testing

Software Engineering I (02161)

Test-Driven Development

Testing. Topics. Types of Testing. Types of Testing

Object Oriented Software Design - I

QUIZ Friends class Y;

Credit where Credit is Due. Lecture 25: Refactoring. Goals for this lecture. Last Lecture

void printowing(double amount) { printbanner(); printdetails(); void printdetails(double amount) {

Program development plan

CSE 326: Data Structures. Section notes, 4/9/2009

xtreme Programming (summary of Kent Beck s XP book) Stefan Resmerita, WS2015

6.001 Notes: Section 8.1

Unit Testing In Python

Debugging distributed systems. CS514: Intermediate Course in Operating Systems. Today s topic. Waterfall Model. V Model.

Chapter 15. Software Testing The assert Statement

Chapter 5 Errors. Bjarne Stroustrup

Refactoring Without Ropes

This assignment requires that you complete the following tasks (in no particular order).

CSE 331 Software Design & Implementation. Hal Perkins Fall 2016 Debugging

Lecture 15 Software Testing

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Overview. State-of-the-Art. Relative cost of error correction. CS 619 Introduction to OO Design and Development. Testing.

CS 520 Theory and Practice of Software Engineering Fall 2018

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Regression testing. Whenever you find a bug. Why is this a good idea?

Appendix A Design. User-Friendly Web Pages

Programming in the Real World. Dr. Baldassano Yu s Elite Education

Computer Science and Software Engineering University of Wisconsin - Platteville 9-Software Testing, Verification and Validation

CS201 - Assignment 3, Part 1 Due: Friday February 28, at the beginning of class

Practice CS106B Midterm Solutions

CS3205 HCI IN SOFTWARE DEVELOPMENT INTRODUCTION TO PROTOTYPING. Tom Horton. * Material from: Floryan (UVa) Klemmer (UCSD, was at Stanford)

Chapter 9. Software Testing

1 Getting used to Python

Transcription:

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2011 Thorsten Joachims Lecture 7: Software Design Software Engineering The art by which we start with a problem statement and gradually evolve a solution. There are whole books on this topic and most companies try to use a fairly uniform approach that all employees are expected to follow. The IDE can help by standardizing the steps. Building a Search Engine: Search Engine Top-Down Design Just the opposite: start with parts: Bottom-Up Design Search Engine Crawler Indexer User Interface Crawler Indexer User Interface Queue Client Database Inverted Index Ranking Function Spelling Correction Server Queue Client Database Inverted Index Ranking Function Spelling Correction Server Hashmap Pagerank Term Weighting Refine the design at each step Decomposition / Divide and Conquer List Hashmap List Pagerank Composition Build-It-Yourself (e.g. IKEA furniture) Term Weighting Top-Down vs. Bottom-Up Is one of these ways better? Not really! It s sometimes good to alternate By coming to a problem from multiple angles you might notice something you had previously overlooked Not the only ways to go about it With Top-Down it s harder to test early because parts needed may not have been designed yet With Bottom-Up, you may end up needing things different from how you built them Software Process For simple programs, a simple process Waterfall But to use this process, you need to be sure that the requirements are fixed and well understood! Many software problems are not like that Often customer refines the requirements when you try to deliver the initial solution! 1

Incremental & Iterative Deliver versions of the system in several small cycles: Recognizes that for some settings, software development is like gardening. You plant seeds see what does well then replace the plants that did poorly. TESTING AND TEST-DRIVEN DEVELOPMENT The Importance of Testing Famous last words Its all done, I just have not tested it yet. Many people Write code without being sure it will work Press run and pray If it fails, they change something random Never work, and ruins weekend social plans. Test-Driven Development! A collection class containing up to N objects (hence small ) typical operations: add contains size adds item is item in the set? # items we ll implement add(), size() The Example Test Driven Development We ll go about in small iterations 1.add a test 2.run all tests and watch the new one fail 3.make a small change 4.run all tests and see them all succeed 5.refactor (as needed) We ll use JUnit What do JUnit tests look like?.java package edu.cornell.cs.cs2110; public class { JUnit Test.java package edu.cornell.cs.cs2110; import org.junit.test; import static org.junit.assert.*; public class Test { @Test public void testfoo() { s = new (); asserttrue(); @Test public void testbar() { 2

A List of Tests We start by thinking about how to test, not how to implement size=0 on empty set size=n after adding N distinct elements adding element already in set doesn t change size throw exception if adding too many Each test verifies a certain feature We pick a feature and test it: class { Test class Test { @Test public void testemptysetsize() { s = new (); assertequals(0, s.size()); A First Test This doesn t compile: size()is undefined But that s all right: we ve started designing the interface by using it Red Bar A test can be defined before the code is written class { public int size() { return 42; Running the test yields a red bar indicating failure: If we add the size function and re-run the the test, it works! Green Bar What s the simplest way to make a test pass? class { public int size() { return 0; Fake it till you make it Re-running yields the legendary JUnit Green Bar: Move on with the next feature Adding Items To implement adding items, we first test for it: Test class Test { @Test public void testemptysetsize() @Test public void testaddone() { s = new (); assertequals(1, s.size()); add() is undefined, so to run the test we define it: public int size() Adding Items The test now fails as expected: It seems obvious we need to count the number of items: private int _size = 0; public int size() { return 0; return _size; And we get a green bar: 3

Adding Something Again So what if we added an item already in the set? Test class Test { @Test public void testemptysetsize() @Test public void testaddone() @Test public void testaddalreadyinset() { s = new (); Object o = new Object(); s.add(o); s.add(o); assertequals(1, s.size()); As expected, the test fails Remember that Item? We need to remember which items are in the set private int _size = 0; public static final int MAX = 10; private Object _items[] = new Object[MAX]; for (int i=0; i < MAX; i++) { if (_items[i] == o) { return; All tests pass, so we can refactor that loop Refactoring FOR-loop doesn t speak to us as it could (before) for (int i=0; i < MAX; i++) { if (_items[i] == o) { return; (after) private boolean inset(object o) { for (int i=0; i < MAX; i++) { if (_items[i] == o) { return true; return false; if (!inset(o)) { All tests still pass, so we didn t break it! Too Many What if we try to add more than can hold? Test @Test public void testaddtoomany() { s = new (); for (int i=0; i <.MAX; i++) { The test fails with an error: ArrayIndexOutOfBoundsException We know why this occurred, but it should bother us: ArrayIndex isn t a sensible error for a set We first have add() check the size, if (!inset(o) && _size < MAX) { re-run the tests, check for green, define our own exception re-run the tests, check for green, and Size Matters FullException public class FullException extends Error { finally test for our exception: Testing for Exceptions Test @Test public void testaddtoomany() { s = new (); for (int i=0; i <.MAX; i++) { try { fail( FullException expected ); catch (FullException e) { The test fails as expected, so now we fix it 4

Testing for Exceptions so now we modify add() to throw: if (!inset(o) && _size < MAX) { if (_size >= MAX) { throw new FullException(); All tests now pass, so we re done: After all Tests are Passed Is the code is correct? Yes, if we wrote the right tests. Is the code efficient? Probably used simplest solution first. Replace simple data structures with better data structures. New ideas on how to compute the same while doing less work. Is the code readable, elegant, and easy to maintain? It is very common to find some chunk of working code, make a replica, and then edit the replica. But this makes your software fragile Later changes have to be done on all instances, or some become inconsistent Duplication can arise in many ways: constants (repeated magic numbers ) code vs. comment within an object s state DRY Principle Don t Repeat Yourself A nice goal is to have each piece of knowledge live in one place But don t go crazy over it DRYing up at any cost can increase dependencies between code 3 strikes and you refactor (i.e., clean up) Simple Refactoring Renaming variables, methods, classes for readability. Explicitly defining constants: public double weight(double mass) { return mass * 9.80665; static final double GRAVITY = 9.80665; public double weight(double mass) { return mass * GRAVITY; If your application later gets used as part of a Nasa mission to Mars, it won t make mistakes Every place that the gravitational constant shows up in your program a reader will realize that this is what she is looking at The compiler may actually produce better code Extract Method A comment explaining what is being done usually indicates the need to extract a method public double totalarea() { // now add the circle area += PI * pow(radius,2); public double totalarea() { area += circlearea(radius); private double circlearea(double radius) { return PI * pow(radius, 2); One of the most common refactorings Extract Method Simplifying conditionals with Extract Method before if (date.before(summer_start) date.after(summer_end)) { charge = quantity * _winterrate + _winterservicecharge; else { charge = quantity * _summerrate; after if (issummer(date)) { charge = summercharge(quantity); else { charge = wintercharge(quantity); 5

Review Started with a to do list of tests / features could have been expanded as we thought of more tests / features Added features in small iterations a feature without a test doesn t exist Is testing obligatory? When you write code in professional settings with teammates, definitely! In such settings, failing to test your code just means you are inflicting errors you could have caught on teammates! People get fired for this sort of thing! So in industry test or perish! But what if code is just for yourself? Testing can still help you debug, and if you go to the trouble of doing the test, JUnit helps you keep it for re-use later. I have never written a program that was correct before I tested and debugged it. Prof. Joachims Fixing a Bug What if after releasing we found a bug? A bug can reveal a missing test but can also reveal that the specification was faulty in the first place, or incomplete Code evolves and some changing conditions can trigger buggy behavior This isn t your fault or the client s fault but finger pointing is common Great testing dramatically reduces bug rates And can make fixing bugs way easier But can t solve everything: Paradise isn t attainable in the software industry Famous last words: It works! Reasons for TDD By writing the tests first, we test the tests design the interface by using it ensure the code is testable ensure good test coverage By looking for the simplest way to make tests pass, the code becomes as simple as possible, but no simpler may be simpler than you thought! Not the Whole Story There s a lot more worth knowing about TDD What to test / not to test» e.g.: external libraries? How to refactor tests Fixtures Mock Objects Crash Test Dummies Beck, Kent: Test-Driven Development: By Example 6

How people big really big programs When applications are small, you can understand every element of the system But as systems get very large and complex, you increasingly need to think in terms of interfaces, documentation that defines how modules work, and your code is more fragmented This forces you into a more experimental style Testing is a part of that style! Once you no longer know how big parts of the system even work (or if they work), you instead begin to think in terms of Code you ve written yourself. You tested it and know that it works! Modules you make use of. You wrote experiments to confirm that they work the way you need them to work Tests of the entire complete system, to detect issues visible only when the whole thing is running or only under heavy load Junit testing isn t enough For example, many systems suffer from leaks Such as adding more and more objects to an ArrayList The amount of memory just grows and grows Some systems have issues triggered only in big deployments, like cloud computing settings Sometimes the application specification was flawed, and a correct implementation of the specification will look erroneous to the end user But a thorough test plan can reveal all such problems The Q/A cycle Real companies have quality assurance teams They take the code and refuse to listen to all the long-winded explanations of why it works Then they do their own, independent, testing And then they send back the broken code with a long list of known bugs! Separating development from Q/A really helps Why is Q/A a cycle? Each new revision may fix bugs but could also break things that were previously working Moreover, during the lifetime of a complex application, new features will often be added and those can also require Q/A Thus companies think of software as having a very long life cycle. Developing the first version is only the beginning of a long road! Even with fantastic Q/A The best code written by professionals will still have some rate of bugs They reflect design oversights, or bugs that Q/A somehow didn t catch Evolutionary change in requirements Incompatibilities between modules developed by different people, or enhancements made by people who didn t fully understand the original logic So never believe that software will be flawless Our goal in cs2110 is to do as well as possible In later CS courses we ll study fault tolerance! 7