The STREAM process – programming in practice. RHS – SOC 2 What are we trying to do? Tools Language Specifications Intellect Programs that work… HOW?

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
STREAM STREAM A software process The Mañana Principle The Mañana Principle Don’t try to everything at once! Static Methods Static Methods Stateless Utility.
Introduction to Methods
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Object Oriented Software Development
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 3 Introduction to Collections – Stacks Modified
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
PHP meets MySQL.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Discussion 5. Lab 3 Not really using rectangle class Return type of getPoint is String instead of Point You are not able to retrieve the point if you.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Intermediate 2 Computing Unit 2 - Software Development.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
PROGRAMMING TESTING B MODULE 2: SOFTWARE SYSTEMS 22 NOVEMBER 2013.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 8-Exception Handling/ Robust Programming.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
What is this? SE-2030 Dr. Mark L. Hornick 1. Same images with different levels of detail SE-2030 Dr. Mark L. Hornick 2.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
The need for Programming Languages
3.1 Fundamentals of algorithms
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Objects First with Java
Writing Methods AP Computer Science A.
MSIS 655 Advanced Business Applications Programming
Java Programming Arrays
Java Inheritance.
Java Programming Language
slides created by Ethan Apter and Marty Stepp
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

The STREAM process – programming in practice

RHS – SOC 2 What are we trying to do? Tools Language Specifications Intellect Programs that work… HOW?

RHS – SOC 3 The programming process What elements are involved in writing a (correct) program? In the ideal world: –Understand – read the specification until you are sure you understand it completely –Think – now think hard about how to solve the program –Code – now write the program! A simple, linear process…but it does not work that way in real life

RHS – SOC 4 The ideal world ThinkUnderstand Code

RHS – SOC 5 The real world Think Understand Code Test

RHS – SOC 6 How to get started We will not address the problem of understanding – and develop- ing – the specification here (Software Design) Given a reasonable under- standing of the problem to solve, how do we get started at all? The traditional approach is stepwise refinement

RHS – SOC 7 Stepwise refinement public class MyMainClass { public void solveProblem() { // Just add code here… }

RHS – SOC 8 Stepwise refinement From this point, simply break down the problem into smaller and smaller pieces Code evolves from being abstract to becoming concrete Not a bad strategy, but still too naive –Assumes perfect knowledge of problem –Still a linear process A more realistic process is stepwise improvement

RHS – SOC 9 Stepwise improvement The Stepwise improvement process adds two more activities to the Refinement process Extension: we extend the code to cover more of the specification – for instance include an additional case Restructuring: we change the structure of the code, without changing the functionality of the code

RHS – SOC 10 Activity diagram Extension Refinement Restructuring The Goal You are here!

RHS – SOC 11 Stepwise refinement Extension Refinement Restructuring The Goal Now we understand the problem com- pletely, start coding!

RHS – SOC 12 Stepwise improvement Extension Refinement Restructuring The Goal

RHS – SOC 13 Embrace change… Is Stepwise Improvement completely chaotic!? No, but it can appear confusing There is no given order of the three types of activities, they can ”happen” at any time during program development Notice, however, that they are separate activities We can provide advice about how to perform each activity

RHS – SOC 14 Extension In real life, a full specification is often very large, hard to under- stand, incomplete and ambiguous Does not really make sense to try to make a complete program in one attempt Instead, select a part of the functionality, and make it work E.g a single Use Case

RHS – SOC 15 Extension Once the selected part of the specification works, we can proceed to including more parts of the specification Before that, we may possibly do some refinement and restructuring The specification itself might change as well…

RHS – SOC 16 Refinement In this activity, we try to actually implement some part of the specification This activity only involves programming Do not try to be too clever here! Refinement is complicated enough in itself The STREAM process is a simplified process for the refinement activity

RHS – SOC 17 The STREAM process Stubs Test Representations Evaluation Attributes Methods

RHS – SOC 18 Stubs What are ”stubs” – methods which do not yet have code inside them We assume that at this point, we are about to implement some well-known classes Well-known: The expected behavior of the classes is known We should be able to figure out the public interface to the classes

RHS – SOC 19 Stubs public class BankAccount { public void deposit() {} public void withdraw() {} public double getBalance() { return 0.0; }

RHS – SOC 20 Stubs We can create classes with stub methods for all classes under consideration Our program will compile…but do nothing This state of a program is often called a walking skeleton

RHS – SOC 21 Test We assume we fully understand the classes under consideration We should thus be able to write test cases for them, before proceeding further Gives us a clear target – once the program passes our tests, we are done with the activity Usual considerations about test still apply…

RHS – SOC 22 Representation During the ”stub” part, we did not pay any attention to data representation Data representation is not part of the public interface Need to make a choice, however… Think about alternatives for represen- tation – you should come up with at least two alternatives

RHS – SOC 23 Evaluation Given some alternatives for representation, we must now evaluate them Evaluate: How hard will it be to implement the methods in the public interface, given that we use some specific representation Will of course be a subjective evaluation Evaluate each representation alternative for each public method REM: Representation Evaluation Matrix

RHS – SOC 24 Evaluation REM doubleString depositEasyAverage withdrawEasyAverage getBalanceTrivialEasy

RHS – SOC 25 Attributes Now we have chosen a representation for our data, so implement the representation That is: –Define instance fields –Set proper values in class constructors This does not mean that we are done with representation – we may need more instance fields when implementing methods Can still use REM

RHS – SOC 26 Attributes public class BankAccount { double balance; public BankAccount() { balance = 0.0; } public void deposit() {} public void withdraw() {} public double getBalance() { return 0.0; } }

RHS – SOC 27 Methods All that remains now is to fill in the code in the public methods… …but that is also the hardest part! In a sense, we try to re-apply parts of the STREAM steps when implementing a method We have a set of method implementation rules to assist us during implementation

RHS – SOC 28 Methods Again, the guiding principle is to manage the complexity, by breaking problems into smaller, more manageable pieces In practice – push your problems ahead of you! Keep method implementations simple, by inventing new methods as you go along Implement the new methods as stubs, and return to them later on The mañana principle (or the Good Fairy)

RHS – SOC 29 The mañana principle public void convertFromInchesToCm() { double inches = getInchesFromUser(); double cm = calculateCmFromInches(inches); presentCmToUser(cm); } private double getInchesFromUser() { return 0.0; } private double calculateCmFromInches(double inches) { return 0.0; } private void presentCmToUser(double cm) {}

RHS – SOC 30 The mañana principle When to use the mañana principle –Special case: Move implementation of a special case to a separate method –Nested loop: If you have a nested loop, move the inner loop to a separate method –Hard problem: If you need the answer to a problem that you cannot immediately solve, make it a separate method –Heavy functionality: If a sequence of statements or an expression becomes long or complicated, move some of it into a separate method

RHS – SOC 31 Methods The mañana principle is not a rule, but a principle – apply when necessary The ”threshold” for when to apply it is individual Rule-of-thumb: Methods which cannot fit into a single screen are too long, and should be divided into more methods Remember we still have the test cases to guide us – when the tests are passed, we are done

RHS – SOC 32 Keeping the compiler happy This is a very practical advice Compilers try to find all errors in the code, but often get it wrong… Error messages from the compiler can be quite confusing Only try to fix those errors which make sense Fortunately, compilation is fast!

RHS – SOC 33 Keeping the compiler happy Compile often! Try to keep the program free of syntax errors at all times Fix errors immediately If the compiler only reports one or a few errors, they are much easier to fix Error(s) must be in the code you have just entered…

RHS – SOC 34 Example: a Date class We are given the task of implementing a Date class, specified as follows: –The Date class represents a date, given by year, month number and day number –When created, a Date object is initialised with a year, month and day (it can be assumed that the input data always represents a valid date) –It must be possible to advance the date in a Date object to the next valid date –It must be possible to retrieve the data as a String

RHS – SOC 35 Date class – Stubs public class Date { public Date(int year, int month, int day) { } public void advanceToNextDate() { } public String toString() { return null; }

RHS – SOC 36 Date class – Test We will not detail this now, but there are actually a lot of cases to test –Simple advance of date –Advance of date across month –Advance of date across year –Leap years –…etc. But we can just write up the test cases, without worrying about implementation

RHS – SOC 37 Date class – Representation What are possible – and reasonable – represen- tations of a date? –String – we need to get it back as a String –Multiple integers – one for day, one for month and one for year –One integer – days since Jan 1st, 0001 We evaluate the alternatives using the REM (Representation Evaluation Matrix)

RHS – SOC 38 Date class – Evaluation REM StringMulti IntOne Int Date EasyTrivialHard advance… HardChallengingTrivial toString TrivialEasyHard

RHS – SOC 39 Date class – Attributes public class Date { private int year; private int month; private int day; public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public void advanceToNextDate() { } public String toString() { return year + ”-” + month + ”-” day; } }

RHS – SOC 40 Date class – methods The only task left now is to implement the advanceToNextDate() method First attempt: public void advanceToNextDate() { date = date + 1; }

RHS – SOC 41 Date class – methods A little naive, but actually works in 97 % of all cases We do need to handle special cases Next attempt: public void advanceToNextDate() { date = date +1; handleDayOverflow(); }

RHS – SOC 42 Date class – methods Now advanceToNextDate() is done, assuming that handleDayOverflow() works! The mañana principle at work! Now on to handleDayOverflow() private void handleDayOverflow() { if (day > 30) { day = 1; month = month + 1; }

RHS – SOC 43 Date class – methods This will work, except for two situations –Month does not have 30 days –Across a new year Once again, we fix this by inventing some new methods and implement them later: –daysInMonth() –handleMonthOverflow()

RHS – SOC 44 Date class – methods private void handleDayOverflow() { if (day > daysInMonth()) { day = 1; month = month +1; handleMonthOverflow() } } private int daysInMonth() { return 30; } private void handleMonthOverflow() {}

RHS – SOC 45 Date class – methods And we just keep going like this… The final implementation will – of course – handle all cases correctly, including leap years Manage the complexity – only try to solve one problem at a time! Do not think too much about whether or not the code is optimally structured – this is adressed during the restructuring activity

RHS – SOC 46 Restructuring During a refinement activity, focus is on achieving functionality Should not be too concerned with code structure During a restructuring activity, we restructure code but preserve functionality Easy to forget/neglect this activity in real life…

RHS – SOC 47 Restructuring How do I know what to restructure…? Not an easy task, requires some experience You have to develop an ability to ”smell bad code” What does bad code smell like?

RHS – SOC 48 Smelly code… private void hDO() { if (d > dIM()) { d = 1; m++; hMO() } private int dIM() { return stdDiM; } Hmm, bad choice of names…?

RHS – SOC 49 Restructuring It could be as simple as that… In the heat of the fight, we might have chosen names which are ”non-informative” Similar ”clean-up” activities could be: –Non-consistent naming –Replace magic numbers with constants –Fixing alignments –Eliminate unused parameters Remember, no change in functionality!

RHS – SOC 50 Smelly code… public void printWorkerInfo(Employee worker) { System.out.println(”Worker information”); System.out.println(” ”); System.out.println(worker.GetName()); System.out.println(worker.GetAddress()); } public void printManagerInfo(Employee manager) { System.out.println(”Manager information”); System.out.println(” ”); System.out.println(manager.GetName()); System.out.println(manager.GetAddress()); }

RHS – SOC 51 Nice, clean code… public void printWorkerInfo(Employee worker) { printEmployeeInfo(worker, ”Worker information”); } public void printManagerInfo(Employee manager) { printEmployeeInfo(manager, ”Manager information”); } public void printEmployeeInfo(Employee person, String header) { System.out.println(header); System.out.println(” ”); System.out.println(person.GetName()); System.out.println(person.GetAddress()); }

RHS – SOC 52 Restructuring Restructuring works on several levels –Inside methods –Among methods, inside classes –Among classes Restructuring is often referred to as refactoring, an important – and fairly large – topic in itself ( NetBeans offers some help in refactoring Best tools are experience and skill

RHS – SOC 53 Assignment Try to solve Programming Exercise P4.14 in Big Java (page 178) Follow the steps in the STREAM process when solving the exercise – make notes as you proceed When done, examine the code for any opportunities for restructuring of the code