Software Engineering. Top-Down Design. Bottom-Up Design. Software Process. Top-Down vs. Bottom-Up 13/02/2012

Similar documents
Test-Driven Development

JUnit. Introduction to Unit Testing in Java

Test Driven Development

Secrets to Automation Success. A White Paper by Paul Merrill, Consultant and Trainer at Beaufort Fairmont, LLC

Fail early, fail often, succeed sooner!

Agile.NET Development Test-driven Development using NUnit

Java Unit testing with JUnit 4.x in Eclipse

Testing Rails. by Josh Steiner. thoughtbot

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

Java course - IAG0040. Unit testing & Agile Software Development

Overview. What is software testing? What is unit testing? Why/when to test? What makes a good test? What to test?

Advanced Software Testing

Module 2. Software Life Cycle Model. Version 2 CSE IIT, Kharagpur

Integrated Error-Detection Techniques: Find More Bugs in Java Applications

Software Process for QA

Code Qualities and Coding Practices

Decision Logic: if, if else, switch, Boolean conditions and variables

Testing, What is it Good For? Absolutely Everything!

Agile So)ware Development

Deep Agile Blending Scrum and Extreme Programming. Jeff Sutherland Ron Jeffries

Development Environment and Tools for Java. Brian Hughes IBM

Module 10. Coding and Testing. Version 2 CSE IIT, Kharagpur

Course Goals. Solve Non-Technical Customer problem Server side: Ruby on Rails Client side: HTML, CSS, AJAX, JavaScript Deploy using cloud computing

Testing, Debugging, and Verification

Agile Software Development Methodologies and Its Quality Assurance

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

Research Data Management CODING

And the Models Are System/Software Development Life Cycle. Why Life Cycle Approach for Software?

Backup & Disaster Recovery

Software Development Life Cycle (SDLC)

Extreme Programming and Embedded Software Development

Software Engineering I (02161)

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

Test Driven Development with Continuous Integration: A Literature Review

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

Continuous Integration with Jenkins. Coaching of Programming Teams (EDA270) J. Hembrink and P-G. Stenberg [dt08jh8

Topics covered. Agile methods Plan-driven and agile development Extreme programming Agile project management Scaling agile methods

Basic Trends of Modern Software Development

Pattern Insight Clone Detection

Unit Testing & JUnit

Name of pattern types 1 Process control patterns 2 Logic architectural patterns 3 Organizational patterns 4 Analytic patterns 5 Design patterns 6

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Moving from CS 61A Scheme to CS 61B Java

Life Cycle Models. V. Paúl Pauca. CSC Fall Department of Computer Science Wake Forest University. Object Oriented Software Engineering

Building a test harness is. an effort that often takes. on a life of its own. But it. doesn t have to get wildly out of control.

Tools, Trends and Techniques for Developing Scientific Software

Module 1. Introduction to Software Engineering. Version 2 CSE IIT, Kharagpur

Getting started with API testing

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

A Capability Maturity Model (CMM)

An Example Checklist for ScrumMasters

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

CPSC 330 Software Engineering

Tools for Integration Testing

Introduction to Programming System Design. CSCI 455x (4 Units)

Custom Web Development Guidelines

Advanced Test-Driven Development

Agile Techniques for Object Databases

A How-to Guide By: Riaan Van Der Merwe, General Manager, Dynamics, Neudesic

Agility via Software Engineering Practices

AppendixA1A1. Java Language Coding Guidelines. A1.1 Introduction

Why HTML5 Tests the Limits of Automated Testing Solutions

Software Engineering Introduction & Background. Complaints. General Problems. Department of Computer Science Kent State University

Informatica e Sistemi in Tempo Reale

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

Introduction. Introduction. Software Engineering. Software Engineering. Software Process. Department of Computer Science 1

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Software Continuous Integration & Delivery

Testing. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies. CHAPTER AUTHORS Michael Atmadja Zhang Shuai Richard

Software Engineering. What is a system?

Software Life Cycle. Management of what to do in what order

The Basics of Building Credit

Testing Python. Applying Unit Testing, TDD, BDD and Acceptance Testing

Fruit Machine. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

The Best Software Testing Arie Van Deursen TU Delft, 2011

Line Tracking Basic Lesson

Agile Techniques and Tools. White Paper

Good FORTRAN Programs

Semantic Analysis: Types and Type Checking

Engineering Process Software Qualities Software Architectural Design

Chapter 7: Software Development Stages Test your knowledge - answers

Igniting young minds through computer programming

Programming in Access VBA

Verification and Validation of Software Components and Component Based Software Systems

To debug an embedded system,

Test Driven Development

Crowdsourced Code Review in Programming Classes

Peter Mileff PhD SOFTWARE ENGINEERING. The Basics of Software Engineering. University of Miskolc Department of Information Technology

How Silk Central brings flexibility to agile development

Session 7 Fractions and Decimals

Endo-Testing: Unit Testing with Mock Objects

We ( have extensive experience in enterprise and system architectures, system engineering, project management, and

Evaluation of AgitarOne

Software Engineering and Scientific Computing

Levels of Software Testing. Functional Testing

Agile Testing Intelliware Development Inc. BC

White Paper. The Power of Siebel escripting at Runtime. Bringing Siebel Developer Closer to Rapid Application Development

Advanced Software Engineering. Software Development Processes

Introduction to Python

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Transcription:

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 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 write 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