Software Construction #1: Creating High-Quality Code

Size: px
Start display at page:

Download "Software Construction #1: Creating High-Quality Code"

Transcription

1 Software Construction #1: Creating High-Quality Code Minsoo Ryu Hanyang University

2 Software construction Software construction is mostly coding and debugging But also involves detailed design, construction planning, unit testing, integration, integration testing, and other activities 2 2

3 Design in construction On small projects, a lot of (detailed) design is done while the programmer sits at the keyboard Design might be just writing a class interface in pseudocode before writing the details It might be drawing diagrams of a few class relationships before coding them It might be asking another programmer which design pattern seems like a better choice Design is a wicked problem Horst Rittel and Melvin Webber defined a wicked problem as one that could be clearly defined only by solving it, or by solving part of it (1973) This paradox implies, essentially, that you have to solve the problem once in order to clearly define it and then solve it again to create a solution that works 3 3

4 Outline Working Classes High-Quality Routines Defensive Programming The Pseudocode Programming Process 4 4

5 Working Classes

6 Abstractions Base classes are abstractions that allow you to focus on common attributes of a set of derived classes and ignore the details of the specific classes while you re working on the base class A good class interface is an abstraction that allows you to focus on the interface without needing to worry about the internal workings of the class From a complexity point of view, the principal benefit of abstraction is that it allows you to ignore irrelevant details 6 6

7 Encapsulation and information hiding Abstraction says, You re allowed to look at an object at a high level of detail Encapsulation says, Furthermore, you aren t allowed to look at an object at any other level of detail 7 7

8 Poor abstraction Poor interface It contains a collection of miscellaneous functions 8 8

9 Good interface Good abstraction Some of the original routines were moved to other, more appropriate classes Some were converted to private routines used by InitializeUserInterface() and the other routines 9 9

10 Present a consistent level of abstraction Mixed levels of abstraction Inheriting from ListContainer is convenient because it supports polymorphism, allowing an external search or sort function that takes a List Container object But, the class presents two ADTs Employee and ListContainer 10 10

11 Present a consistent level of abstraction Consistent levels of abstraction The class now presents one ADT: Employee The ListContainer is hidden as a private member 11 11

12 Beware of erosion of abstraction under modification There s no logical connection between employees and routines that check ZIP Codes, phone numbers, or job classifications The routines that expose SQL query details are at a much lower level of abstraction than the Employee class, and they break the Employee abstraction 12 12

13 Avoid putting private implementation details into a class s interface Including private declarations in the class header file might seem like a small transgression, but it encourages other programmers to examine the implementation details 13 13

14 Avoid putting private implementation details into a class s interface Scott Meyers describes a common way to address this issue in Item 34 of Effective C++, 2d ed. (Meyers 1998) You separate the class interface from the class implementation Within the class declaration, include a pointer to the class s implementation but don t include any other implementation details 14 14

15 Be wary of semantic violations of encapsulation Not calling Class A s InitializeOperations() routine because you know that Class A s PerformFirstOperation() routine calls it automatically Using a pointer or reference to ObjectB created by ObjectA even after ObjectA has gone out of scope, because you know that ObjectA keeps ObjectB in static storage and ObjectB will still be valid Using Class B s MAXIMUM_ELEMENTS constant instead of using ClassA.MAXIMUM_ELEMENTS, because you know that they re both equal to the same value 15 15

16 Be wary of semantic violations of encapsulation The problem is that the client code depends not on the class s public interface, but on its private implementation Program to the interface Anytime you find yourself looking at a class s implementation to figure out how to use the class, you re not programming to the interface You re programming through the interface to the implementation If you re programming through the interface, encapsulation is broken Once encapsulation starts to break down, abstraction won t be far behind 16 16

17 Be wary of semantic violations of encapsulation Make interfaces programmatic rather than semantic Each interface consists of a programmatic part and a semantic part The programmatic part consists of the data types and other attributes of the interface that can be enforced by the compiler The semantic part of the interface consists of the assumptions about how the interface will be used, which cannot be enforced by the compiler Look for ways to convert semantic interface elements to programmatic interface elements by using Asserts or other techniques The semantic interface can be documented in comments, but try to keep interfaces minimally dependent on documentation 17 17

18 Don t make assumptions about the class s users A class should be designed and implemented to adhere to the contract implied by the class interface It shouldn t make any assumptions about how that interface will or won t be used, other than what s documented in the interface Comments like the following one are an indication that a class is more aware of its users than it should be: -- initialize x, y, and z to 1.0 because DerivedClass blows -- up if they're initialized to

19 Containment ( has a Relationships) Implement has a through containment Implement has a through private inheritance as a last resort With private inheritance, public and protected member of the base class become private members of the derived class A class does inherit the implementation with private inheritance "Private inheritance means nothing during software design, only during software implementation, by Scott Meyers The private inheritance can introduce unnecessary multiple inheritance 19 19

20 Inheritance ( is a Relationships) Implement is a through public inheritance When a programmer decides to create a new class by inheriting from an existing class, that programmer is saying that the new class is a more specialized version of the older class Design and document for inheritance or prohibit it Inheritance adds complexity to a program, and, as such, it s a dangerous technique Adhere to the Liskov Substitution Principle (LSP) 20 20

21 Prefer polymorphism to extensive type checking The calls to shape.drawcircle() and shape.drawsquare() should be replaced by a single routine named shape.draw(), which can be called regardless of whether the shape is a circle or a square 21 21

22 Prefer polymorphism to extensive type checking On the other hand, sometimes case statements are used to separate truly different kinds of objects or behavior 22 22

23 Multiple inheritance Multiple inheritance can be incredibly useful when used with care, but it s dangerous in the hands of someone who doesn t observe proper precautions Multiple inheritance is useful primarily for defining mixins, simple classes that are used to add a set of properties to an object Mixins allow properties to be mixed in to derived classes 23 23

24 More guidelines Be critical of classes that contain more than about seven data members The number 7±2 has been found to be a number of discrete items a person can remember while performing other tasks (Miller 1956) If a class contains more than about seven data members, consider whether the class should be decomposed into multiple smaller classes (Riel 1996) Move unrelated information to another class In some cases, you ll find that half a class s routines work with half the class s data and half the routines work with the other half of the data In such a case, you really have two classes masquerading as one Break them up! 24 24

25 More guidelines Favor read-time convenience to write-time convenience Be suspicious of classes of which there is only one instance Be suspicious of base classes of which there is only one derived class Avoid deep inheritance trees Provide services in pairs with their opposites 25 25

26 High-Quality Routines

27 What is a routine? Routine A routine is an individual method or procedure invocable for a single purpose A function returns a value A procedure does not return a value, but usually performs an operation on an object Examples include a function in C++, a method in Java, a function or sub procedure in Microsoft Visual Basic What is a high-quality routine? That s a harder question Perhaps the easiest answer is to show what a high-quality routine is not 27 27

28 Example of a low-quality code 28 28

29 Example of a low-quality code The routine has a bad name HandleStuff() tells you nothing about what the routine does The routine isn t documented The routine has a bad layout It gives few hints about its logical organization The routine s input variable is changed (inputrec) If it s an input variable, its value should not be modified (and in C++ it should be declared const) The routine reads and writes global variables it reads from corpexpense and writes to profit 29 29

30 Example of a low-quality code The routine doesn t have a single purpose It initializes some variables, writes to a database, does some calculations none of which seem to be related to each other in any way The routine doesn t defend itself against bad data If crntqtr equals 0, the expression ytdrevenue * 4.0 / (double) crntqtr causes a divide-by-zero error The routine uses several magic numbers: 100, 4.0, 12, 2, and 3 Some of the routine s parameters are unused screenx and screeny are not referenced within the routine One of the routine s parameters is passed incorrectly prevcolor is labeled as a reference parameter (&) even though it isn t assigned a value within the routine 30 30

31 Example of a low-quality code The routine has too many parameters The upper limit for an understandable number of parameters is about 7; this routine has 11 The parameters are laid out in such an unreadable way that most people wouldn t try to examine them closely or even count them The routine s parameters are poorly ordered and are not documented 31 31

32 Good routine names Avoid meaningless, vague, or wishy-washy verbs Some verbs are elastic, stretched to cover just about any meaning Routine names like HandleCalculation(), PerformServices(), OutputUser(), ProcessInput(), and DealWithOutput() don t tell you what the routines do The exception would be when the verb handle was used in the specific technical sense of handling an event Don t differentiate routine names solely by number 32 32

33 Good routine names Describe everything the routine does Describe all the outputs and side effects If a routine computes report totals and opens an output file, ComputeReportTotals() is not an adequate name for the routine ComputeReportTotalsAndOpenOutputFile() is an adequate name but is too long and silly If you have routines with side effects, you ll have many long, silly names The cure is not to use less-descriptive routine names; the cure is to program so that you cause things to happen directly rather than with side effects Make names of routines as long as necessary Research shows that the optimum average length for a variable name is 9 to 15 characters 33 33

34 Good routine names For a function, use a description of the return value For example, cos(), customerid.next(), printer.isready(), and pen.currentcolor() are all good function names that indicate precisely what the functions return For a procedure, use a strong verb followed by an object The name should reflect what the procedure does, and an operation on an object implies a verb-plus object name PrintDocument(), CalcMonthlyRevenues(), CheckOrderlnfo(), and RepaginateDocument() are samples of good procedure names 34 34

35 Use opposites precisely Good routine names Using naming conventions for opposites helps consistency, which helps readability Opposite-pairs like first/last are commonly understood Opposite-pairs like FileOpen() and _lclose() are not symmetrical and are confusing Here are some common opposites: add/remove, increment/decrement, open/close begin/end, insert/delete, show/hide, create/destroy lock/unlock, source/target, first/last, min/max, start/stop get/put, next/previous, up/down, get/set, old/new 35 35

36 Routine parameters Put parameters in input-modify-output order Do not order parameters randomly or alphabetically List the parameters that are input-only first, input-and-output second, and output-only third 36 36

37 Routine parameters Consider creating your own in and out keywords Some modern languages don t support the in and out keywords like Ada does In those languages, you might still be able to use the preprocessor to create your own in and out keywords: 37 37

38 Macro routines Fully parenthesize macro expressions Cube( x+1 ) expands to x+1 * x + 1 * x Cube( x+1 ) expands to ++(x + 1) * (x + 1) * (x + 1) ++Cube( x+1 ) expands to ++((x + 1) * (x + 1) * (x + 1)) 38 38

39 More guidelines The theoretical best maximum length is often described as one screen or one or two pages of program listing, approximately 50 to 150 lines Use all the parameters If you pass a parameter to a routine, use it In one study, 46 percent of routines with no unused variables had no errors, and only 17 to 29 percent of routines with more than one unreferenced variable had no errors (Card, Church, and Agresti 1986) Limit the number of a routine s parameters to about seven Seven is a magic number for people s comprehension Psychological research has found that people generally cannot keep track of more than about seven chunks of information at once (Miller 1956) 39 39

40 Defensive Programming

41 Protecting the program from invalid inputs No garbage in, garbage out A good program never puts out garbage, regardless of what it takes in A good program uses garbage in, nothing out, garbage in, error message out, or no garbage allowed in Three general ways to handle garbage in Check the values of all data from external sources Check the values of all routine input parameters Decide how to handle bad inputs 41 41

42 Assertions An assertion is code that s used during development usually a routine or macro that allows a program to check itself as it runs Assertions are normally compiled into the code at development time and compiled out of the code for production An assertion usually takes two arguments: a boolean expression that describes the assumption that s supposed to be true, and a message to display if it isn t 42 42

43 Building your own assertion mechanism Many languages have built-in support for assertions, including C++, Java, and Microsoft Visual Basic If your language doesn t directly support assertion routines, they are easy to write The standard C++ assert macro doesn t provide for text messages Here s an example of an improved ASSERT implemented as a C++ macro 43 43

44 Guidelines for using assertions Use error-handling code for conditions you expect to occur; use assertions for conditions that should never occur Error-handling code checks for off-nominal circumstances that might not occur very often, but that have been anticipated by the programmer who wrote the code and that need to be handled by the production code Error handling typically checks for bad input data; assertions check for bugs in the code A good way to think of assertions is as executable documentation You can t rely on them to make the code work, but they can document assumptions more actively than program-language comments can 44 44

45 Guidelines for using assertions Use assertions to document and verify preconditions and postconditions Part of an approach to program design and development known as design by contract (Meyer 1997) 45 45

46 Error handling techniques How do you handle errors that you do expect to occur? Options for error handling Return a neutral value A numeric computation might return 0 A string operation might return an empty string, or a pointer operation might return an empty pointer Substitute the next piece of valid data Return the same answer as the previous time Substitute the closest legal value Log a warning message to a file Return an error code Display an error message wherever the error is encountered Shut down 46 46

47 Barricade the program to contain the damage caused by errors 47 47

48 Pseudocode Programming Process

49 Pseudocode for pros The term pseudocode refers to an informal, English-like notation for describing how an algorithm, a routine, a class, or a program will work The Pseudocode Programming Process defines a specific approach to using pseudocode to streamline the creation of code within routines Guidelines Use English-like statements that precisely describe specific operations Avoid syntactic elements from the target programming language Write pseudocode at the level of intent Write pseudocode at a low enough level that generating code from it will be nearly automatic 49 49

50 Bad pseudocode It includes target language coding details, such as *hrsrcptr (in specific C-language pointer notation) and malloc() (a specific C-language function) This pseudocode block focuses on how the code will be written rather than on the meaning of the design It gets into coding details whether the routine returns a 1 or a

51 Good pseudocode It s written entirely in English It is also written at the level of intent 51 51

52 Code the routine 52 52

53 Start with pseudocode 53 53

54 Write the routine declaration Write the routine interface statement the function declaration in C++ or method declaration in Java Turn the original header comment into a programminglanguage comment 54 54

55 Turn the pseudocode into high-level comments 55 55

56 Fill in the code below each comment 56 56

CS 370 High-Quality Routines D R. M I C H A E L J. R E A L E F A L L

CS 370 High-Quality Routines D R. M I C H A E L J. R E A L E F A L L CS 370 High-Quality Routines D R. M I C H A E L J. R E A L E F A L L 2 0 1 5 Introduction Routine An individual method or procedure invocable for a single purpose Examples: function in C++, method in Java,

More information

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

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 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 2 0 1 5 Introduction At this point, you are ready to beginning programming at a lower level How do you actually write your

More information

Software Implementation (Writing Quality Code)

Software Implementation (Writing Quality Code) Software Implementation (Writing Quality Code) void HandleStuff( CORP_DATA & inputrec, int crntqtr, EMP_DATA emprec, float & estimrevenue, float ytdrevenue, int screenx, int screeny, COLOR_TYPE & newcolor,

More information

CS 240 Final Exam Review

CS 240 Final Exam Review CS 240 Final Exam Review Linux I/O redirection Pipelines Standard commands C++ Pointers How to declare How to use Pointer arithmetic new, delete Memory leaks C++ Parameter Passing modes value pointer reference

More information

Building Routines. Quality Routines. Quality Routines. Quality Routines. Quality Routines. Routine. What makes a quality routine

Building Routines. Quality Routines. Quality Routines. Quality Routines. Quality Routines. Routine. What makes a quality routine Building Routines Routine individual function or procedure invocable for a single purpose What makes a quality routine easier to see a low quality routine low quality routine coming up Procedure HandleStuff(

More information

Code Complete. Steve McConnell

Code Complete. Steve McConnell Code Complete Steve McConnell 6. Working Classes In the dawn of computing, programmers thought about programming in terms of statements. Throughout the 1970s and 1980s, programmers began thinking about

More information

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module Basic Class Design Goal of OOP: Reduce complexity of software development by keeping details, and especially changes to details, from spreading throughout the entire program. Actually, the same goal as

More information

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas What is this class about? While this class is called Design Patterns, there are many other items of critical

More information

1: Introduction to Object (1)

1: Introduction to Object (1) 1: Introduction to Object (1) 김동원 2003.01.20 Overview (1) The progress of abstraction Smalltalk Class & Object Interface The hidden implementation Reusing the implementation Inheritance: Reusing the interface

More information

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

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

The compiler is spewing error messages.

The compiler is spewing error messages. Appendix B Debugging There are a few different kinds of errors that can occur in a program, and it is useful to distinguish between them in order to track them down more quickly. Compile-time errors are

More information

Software Engineering /48

Software Engineering /48 Software Engineering 1 /48 Topics 1. The Compilation Process and You 2. Polymorphism and Composition 3. Small Functions 4. Comments 2 /48 The Compilation Process and You 3 / 48 1. Intro - How do you turn

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

CSE 12 Abstract Syntax Trees

CSE 12 Abstract Syntax Trees CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures

More information

C++ (Non for C Programmer) (BT307) 40 Hours

C++ (Non for C Programmer) (BT307) 40 Hours C++ (Non for C Programmer) (BT307) 40 Hours Overview C++ is undoubtedly one of the most widely used programming language for implementing object-oriented systems. The C++ language is based on the popular

More information

Intro to: Design Principles

Intro to: Design Principles Intro to: Design Principles Pragmatic Programmer: Eliminate Effects Between Unrelated Things design components that are: self-contained, independent, and have a single, well-defined purpose Software Design

More information

Classes, interfaces, & documentation. Review of basic building blocks

Classes, interfaces, & documentation. Review of basic building blocks Classes, interfaces, & documentation Review of basic building blocks Objects Data structures literally, storage containers for data constitute object knowledge or state Operations an object can perform

More information

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

EXAMINING THE CODE. 1. Examining the Design and Code 2. Formal Review: 3. Coding Standards and Guidelines: 4. Generic Code Review Checklist: EXAMINING THE CODE CONTENTS I. Static White Box Testing II. 1. Examining the Design and Code 2. Formal Review: 3. Coding Standards and Guidelines: 4. Generic Code Review Checklist: Dynamic White Box Testing

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

Lecture 4 CSE July 1992

Lecture 4 CSE July 1992 Lecture 4 CSE 110 6 July 1992 1 More Operators C has many operators. Some of them, like +, are binary, which means that they require two operands, as in 4 + 5. Others are unary, which means they require

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey. EXPLORATION 36 Virtual Functions Deriving classes is fun, but there s not a lot you can do with them at least, not yet. The next step is to see how C++ implements type polymorphism, and this Exploration

More information

Racket Style Guide Fall 2017

Racket Style Guide Fall 2017 CS17 Integrated Introduction to Computer Science Hughes Racket Style Guide Fall 2017 Contents 1 Introduction 1 2 Naming 1 3 Formatting 1 4 Equality 3 5 Conditionals 4 5.1 Prefer Cond to If......................................

More information

Lecture 7: Data Abstractions

Lecture 7: Data Abstractions Lecture 7: Data Abstractions Abstract Data Types Data Abstractions How to define them Implementation issues Abstraction functions and invariants Adequacy (and some requirements analysis) Towards Object

More information

Import Statements, Instance Members, and the Default Constructor

Import Statements, Instance Members, and the Default Constructor Import Statements, Instance Members, and the Default Constructor Introduction In this article from my free Java 8 course, I will be discussing import statements, instance members, and the default constructor.

More information

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn Problem Write and test a Scheme program to compute how many days are spanned by two given days. The program will include a procedure

More information

The great primary-key debate

The great primary-key debate http://builder.com.com/5100-6388-1045050.html Página 1 de 3 16/11/05 Log in Join now Help SEARCH: Builder.com GO Home : Architect : Database : The great primary-key debate Resources Newsletters Discussion

More information

Code Complete. Steve McConnell

Code Complete. Steve McConnell Code Complete Steve McConnell 8. Defensive Programming DEFENSIVE PROGRAMMING DOESN T MEAN being defensive about your programming It does so work! The idea is based on defensive driving. In defensive driving,

More information

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017 CS 426 Fall 2017 1 Machine Problem 1 Machine Problem 1 CS 426 Compiler Construction Fall Semester 2017 Handed Out: September 6, 2017. Due: September 21, 2017, 5:00 p.m. The machine problems for this semester

More information

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

QUIZ How do we implement run-time constants and. compile-time constants inside classes? QUIZ How do we implement run-time constants and compile-time constants inside classes? Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of

More information

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm) CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, 2017 11:30pm) This assignment focuses on recursive backtracking. Turn in the following files using the link on the course

More information

QUIZ. Source:

QUIZ. Source: QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That

More information

D Programming Language

D Programming Language Group 14 Muazam Ali Anil Ozdemir D Programming Language Introduction and Why D? It doesn t come with a religion this is written somewhere along the overview of D programming language. If you actually take

More information

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute. Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute. Abelson & Sussman Use a good set of coding conventions, such as the ones given in the

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 I. Logic 101 In logic, a statement or proposition is a sentence that can either be true or false. A predicate is a sentence in

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides 1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

Lecture 10 Notes Linked Lists

Lecture 10 Notes Linked Lists Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to

More information

COURSE 2 DESIGN PATTERNS

COURSE 2 DESIGN PATTERNS COURSE 2 DESIGN PATTERNS CONTENT Fundamental principles of OOP Encapsulation Inheritance Abstractisation Polymorphism [Exception Handling] Fundamental Patterns Inheritance Delegation Interface Abstract

More information

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 10: building large projects, beginning C++, C++ and structs CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

More information

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

Assertions. Assertions - Example

Assertions. Assertions - Example References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 11/13/2003 1 Assertions Statements about input to a routine or state of a class Have two primary roles As documentation,

More information

This book is about using Visual Basic for Applications (VBA), which is a

This book is about using Visual Basic for Applications (VBA), which is a In This Chapter Describing Access Discovering VBA Seeing where VBA lurks Understanding how VBA works Chapter 1 Where VBA Fits In This book is about using Visual Basic for Applications (VBA), which is a

More information

Design by Contract in Eiffel

Design by Contract in Eiffel Design by Contract in Eiffel 2002/04/15 ctchen@canthink.com.com.tw.tw Reference & Resource Bertrand Meyer, Object-Oriented Oriented Software Construction 2nd,, 1997, PH. Bertrand Meyer, Eiffel: The Language,,

More information

a correct statement? You need to know what the statement is supposed to do.

a correct statement? You need to know what the statement is supposed to do. Using assertions for correctness How can we know that software is correct? It is only correct if it does what it is supposed to do. But how do we know what it is supposed to do? We need a specification.

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

Repetition Through Recursion

Repetition Through Recursion Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something

More information

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

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

Module 10 Inheritance, Virtual Functions, and Polymorphism

Module 10 Inheritance, Virtual Functions, and Polymorphism Module 10 Inheritance, Virtual Functions, and Polymorphism Table of Contents CRITICAL SKILL 10.1: Inheritance Fundamentals... 2 CRITICAL SKILL 10.2: Base Class Access Control... 7 CRITICAL SKILL 10.3:

More information

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance 1 ECE 3574: Dynamic Polymorphism using Inheritance Changwoo Min 2 Administrivia Survey on class will be out tonight or tomorrow night Please, let me share your idea to improve the class! 3 Meeting 10:

More information

COSC345 Software Engineering. Documentation

COSC345 Software Engineering. Documentation COSC345 Software Engineering Documentation Documentation Self-documenting code Comments Outline Documentation And Comments McConnell Most developers like writing documentation If the standards aren t unreasonable

More information

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych CS125 : Introduction to Computer Science Lecture Notes #11 Procedural Composition and Abstraction c 2005, 2004 Jason Zych 1 Lecture 11 : Procedural Composition and Abstraction Solving a problem...with

More information

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

Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012 Supplemental Handout: Exceptions CS 1070, Spring 2012 Thursday, 23 Feb 2012 1 Objective To understand why exceptions are useful and why Visual Basic has them To gain experience with exceptions and exception

More information

Reliable programming

Reliable programming Reliable programming How to write programs that work Think about reliability during design and implementation Test systematically When things break, fix them correctly Make sure everything stays fixed

More information

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

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

More information

This is a book about using Visual Basic for Applications (VBA), which is a

This is a book about using Visual Basic for Applications (VBA), which is a 01b_574116 ch01.qxd 7/27/04 9:04 PM Page 9 Chapter 1 Where VBA Fits In In This Chapter Describing Access Discovering VBA Seeing where VBA lurks Understanding how VBA works This is a book about using Visual

More information

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22 COSC 2P91 Bringing it all together... Week 4b Brock University Brock University (Week 4b) Bringing it all together... 1 / 22 A note on practicality and program design... Writing a single, monolithic source

More information

Chapter 6: The C Preprocessor

Chapter 6: The C Preprocessor C: Chapter6 Page 1 of 5 C Tutorial.......... The C preprocessor Chapter 6: The C Preprocessor AIDS TO CLEAR PROGRAMMING The preprocessor is a program that is executed just prior to the execution of the

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Errors. Lecture 6. Hartmut Kaiser  hkaiser/fall_2011/csc1254.html Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2011/csc1254.html 2 Abstract When we program, we have to deal with errors. Our most basic aim is correctness, but we must deal with

More information

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle

11/2/09. Code Critique. What goal are we designing to? What is the typical fix for code smells? Refactoring Liskov Substitution Principle Code Critique Identifying smells Refactoring Liskov Substitution Principle What goal are we designing to? What is the typical fix for code smells? What is a limitation of those fixes? How do we address

More information

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtyping Composition:

More information

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Chapter 12 OOP: Creating Object-Oriented Programs McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter Objectives - 1 Use object-oriented terminology correctly Create a two-tier

More information

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli Identify and overcome the difficulties encountered by students when learning how to program List and explain the software development roles played by students List and explain the phases of the tight spiral

More information

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance Handout written by Julie Zelenski, updated by Jerry. Inheritance is a language property most gracefully supported by the object-oriented

More information

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות 2 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static,

More information

Excel programmers develop two basic types of spreadsheets: spreadsheets

Excel programmers develop two basic types of spreadsheets: spreadsheets Bonus Chapter 1 Creating Excel Applications for Others In This Chapter Developing spreadsheets for yourself and for other people Knowing what makes a good spreadsheet application Using guidelines for developing

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

SML Style Guide. Last Revised: 31st August 2011

SML Style Guide. Last Revised: 31st August 2011 SML Style Guide Last Revised: 31st August 2011 It is an old observation that the best writers sometimes disregard the rules of rhetoric. When they do so, however, the reader will usually find in the sentence

More information

Chapter 1: Principles of Programming and Software Engineering

Chapter 1: Principles of Programming and Software Engineering Chapter 1: Principles of Programming and Software Engineering Data Abstraction & Problem Solving with C++ Fifth Edition by Frank M. Carrano Software Engineering and Object-Oriented Design Coding without

More information

Proofwriting Checklist

Proofwriting Checklist CS103 Winter 2019 Proofwriting Checklist Cynthia Lee Keith Schwarz Over the years, we ve found many common proofwriting errors that can easily be spotted once you know how to look for them. In this handout,

More information

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern Think of drawing/diagramming editors ECE450 Software Engineering II Drawing/diagramming editors let users build complex diagrams out of simple components The user can group components to form larger components......which

More information

ADTs & Classes. An introduction

ADTs & Classes. An introduction ADTs & Classes An introduction Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object

More information

the NXT-G programming environment

the NXT-G programming environment 2 the NXT-G programming environment This chapter takes a close look at the NXT-G programming environment and presents a few simple programs. The NXT-G programming environment is fairly complex, with lots

More information

Outline. Data Definitions and Templates Syntax and Semantics Defensive Programming

Outline. Data Definitions and Templates Syntax and Semantics Defensive Programming Outline Data Definitions and Templates Syntax and Semantics Defensive Programming 1 Data Definitions Question 1: Are both of the following data definitions ok? ; A w-grade is either ; - num ; - posn ;

More information

Software Development. Modular Design and Algorithm Analysis

Software Development. Modular Design and Algorithm Analysis Software Development Modular Design and Algorithm Analysis Precondition and Postcondition To create a good algorithm, a programmer must be able to analyse a precondition (starting state) and a postcondition

More information

Animations involving numbers

Animations involving numbers 136 Chapter 8 Animations involving numbers 8.1 Model and view The examples of Chapter 6 all compute the next picture in the animation from the previous picture. This turns out to be a rather restrictive

More information

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון 22 Roadmap Lectures 4 and 5 present two sides of OOP: Lecture 4 discusses the static, compile time representation of object-oriented

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Table of Laplace Transforms

Table of Laplace Transforms Table of Laplace Transforms 1 1 2 3 4, p > -1 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 Heaviside Function 27 28. Dirac Delta Function 29 30. 31 32. 1 33 34. 35 36. 37 Laplace Transforms

More information

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

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design Computer Science 4U Unit 1 Programming Concepts and Skills Modular Design Modular Design Reusable Code Object-oriented programming (OOP) is a programming style that represents the concept of "objects"

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 4 Date:

More information

Laboratorio di Tecnologie dell'informazione

Laboratorio di Tecnologie dell'informazione Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Coding style guidelines Good code is its own best documentation. - Steve McConnell

More information

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4 CS 220: Introduction to Parallel Computing Arrays Lecture 4 Note: Windows I updated the VM image on the website It now includes: Sublime text Gitkraken (a nice git GUI) And the git command line tools 1/30/18

More information

Construction: High quality code for programming in the large

Construction: High quality code for programming in the large Construction: High quality code for programming in the large Paul Jackson School of Informatics University of Edinburgh What is high quality code? High quality code does what it is supposed to do......

More information

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring

Evolving Software. CMSC 433 Programming Language Technologies and Paradigms Spring Example. Some Motivations for This Refactoring CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Refactoring April 24, 2007 Lots of material taken from Fowler, Refactoring: Improving the Design of Existing Code 1 Evolving Software

More information

Component-Level Design

Component-Level Design Component-Level Design Minsoo Ryu Hanyang University Contents 1. Design Model 2. Fundamental Design Concepts 3. Component-Level Design 4. Object-Oriented Design Techniques 2 2 Data/class design Four Design

More information

COEN244: Class & function templates

COEN244: Class & function templates COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library

More information

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

double d0, d1, d2, d3; double * dp = new double[4]; double da[4]; All multiple choice questions are equally weighted. You can generally assume that code shown in the questions is intended to be syntactically correct, unless something in the question or one of the answers

More information

Vector and Free Store (Pointers and Memory Allocation)

Vector and Free Store (Pointers and Memory Allocation) DM560 Introduction to Programming in C++ Vector and Free Store (Pointers and Memory Allocation) Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides

More information

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I Review from Lecture 2 CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I Vectors are dynamically-sized arrays Vectors, strings and other containers should be: passed by reference when they are to

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004

Administrivia. Programming Language Fall Example. Evolving Software. Project 3 coming out Midterm October 28. Refactoring October 14, 2004 CMSC 433 Programming Language Fall 2004 Project 3 coming out Midterm October 28 Administrivia Refactoring October 14, 2004 Lots of material taken from Fowler, Refactoring: Improving the Design of Existing

More information

HOUR 4 Understanding Events

HOUR 4 Understanding Events HOUR 4 Understanding Events It s fairly easy to produce an attractive interface for an application using Visual Basic.NET s integrated design tools. You can create beautiful forms that have buttons to

More information