1 producing an oo system from code steps: 1. build executable 2. test 3. debug usually you: yuse...

27
1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: Use multiple source files Split interface/implementation into .h/.c [.cpp] Reuse other libraries, objects Section 5.2:

Upload: polly-carpenter

Post on 31-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

1

Producing an OO System from CodeSteps:

1. Build executable2. Test3. Debug

Usually you: Use multiple source files Split interface/implementation into .h/.c [.cpp] Reuse other libraries, objects

Section 5.2:

Page 2: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

2

Comparing .h and .cpp Files

Frame.h file:class Frame {private: wxCanvas canvas;public: void Clear ();

}

Frame.cpp File:#include Frame.h

void Frame::Clear() {canvas->Clear();

}

Page 3: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

3

Definition: “Dependent”

File A is dependent on B if changing B can invalidate A

Questions:If I change a .h file, what must I

recompile?

Page 4: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

4

Examples of Dependencies

Dependent

File

Depends On Tool to

Regenerate

Object file Source files

(code and header files)

compiler

library Object files Librarian Or

archiver

executable Object files and

libraries

linker

Page 5: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

5

Example of Class Dependencies#include File.hclass FileChooser {public: FileChooser(char* path, char* filter); File AskUser(); ...};#include FileChooser.h, Directory.h, Selector.hFile FileChooser::AskUser() { Directory directory(thePath, theFilter); Selector selector(thePath); …}FileChooser.h --> File.hFileChooser.cc --> FileChooser.h Directory.h Selector.hFileChooser.o --> FileChooser.cc

Page 6: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

6

An Association of Objects

Page 7: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

7

Using Dependencies to Control Recompilation

for all A in SourceFiles { if (A has changed) { recompile A; for all B in SourceFiles { if B depends on A then recompile B } } } link object files;

Page 8: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

8

Conditional Compilation

Today, want cross-platform code(platform=OS + Hardware)

#ifdef _WINDOWS_95_... code to include for Windows95

platform#endif

#ifdef _UNIX_...code to include for Unix platform

#endif

Page 9: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

9

Defining Cond. Compilation Variables1) Defined in header file:// This is the file Platform.h #ifndef _PLATFORM_ #define _PLATFORM_ #define _WIN_95_ // use W'95 platform ... #endif

2) Defined via compiler flag:gcc -D_WIN_95_ foo.c //sets _WIN95_=1

Page 10: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

10

Pros/Cons of Incremental Development

+ Easier testing/debugging+ Better risk control+ Better team organization+ Concrete measurability+ Psychological benefits- “Stresses” of new fcts contort original

architecture(You may need to take time out to re-architect system)

Page 11: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

11

Incremental Development

Incremental - small steps Progressive - each step moves towards

the final goal Each step can be (and is) tested and

evaluated Regression testing

Page 12: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

12

Table 5-2 An Incremental Development Plan

1. draw a single rectangle at a fixed location with a fixed color; no moving, resizing or grouping is allowed

2. draw a single rectangle at a user-selected location with a fixed color; no moving, resizing or grouping is allowed

3. draw a single instance of each basic shape at user-selected locations each with a fixed color; no moving, resizing or grouping is allowed

4. draw a single rectangle at a fixed location with a user-selected color; no moving, resizing or grouping is allowed

5. draw a single rectangle at a user-selected location with a user-selected color; no moving, resizing or grouping is allowed

6. (combine steps 3 and 5) draw a single instance of each basic shape at user-selected locations each with a user-selected color; no moving, resizing or grouping is allowed

Page 13: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

13

Tools to Work with Source Code

Two categories:Toolkits

Collection of utilities invoked from command line

Examples: emacs, make, gcc, gdb

Integrated development environments (IDEs) Example: Visual Studio (Visual C++)

Page 14: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

14

Advantages of ToolKits

Easy to add new tools (just another command)

Easy to upgrade one tool

User can reuse old knowledge Use your favorite editor, whether you use gcc

or javac

Page 15: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

15

Disadvantages of ToolKits

User interfaces of tools may be inconsistent and difficult to use.

Tools usually don’t communicate with each other: Suppose gdb traps program crash. Gdb reports source file/line# where program

stopped You must manually find the line# in text editor

Page 16: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

16

Why Developers Like EmacsEmacs is an extensible text editor

You add new functions by writing mlisp

Loading a file loads a per-language mode Nice key bindings; one keystroke to recompile, run

Emacs offers many advantages of IDE Emacs acts as “front end” for all tools Tools can sync: gdb pops up source in other window It’s easy to add new tools

Page 17: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

17

Advantages of IDE Approach

IDE user interface is graphical offers multiple (synchronized) windows is usually consistent (across languages)

Information loss among the pieces of the environment is minimized.

Page 18: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

18

Disadvantages of IDE Approach

IDE is closed environment Hard to add a new tool Impossible to “fix” an irritating feature

Example: I can choose from several author’s java modes in emacs to find one to my liking, but I’m “stuck” with Visual Studio’s poor indentation algorithm for Java

If you learn to use one IDE, you probably know nothing if you change to another IDE

Page 19: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

19

Debugging Terms

Failure: Something goes wrong when you run program

(Example: computer crashes)

Fault: Segment of code that produced failure

(Example: division by zero)

Error: What programmer did wrong that produced fault

(Example: “I didn’t expect the user to input zero!”)

Section 5.5:

Page 20: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

20

Steps in Debugging a System

Page 21: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

21

Common Debugging Situations

space

time

Page 22: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

22

Using the Assert Macro

#include <assert.h> Result Class::Method(int a[], int size, int i) {... assert(i >= 0 && i < size && a[i] > 0); ... }

Page 23: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

23

The System Heap

Page 24: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

24

The Run-Time Stack

Page 25: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

25

Debugging Strategies

Debugging Strategies

Proactive Reactive

Incremental development Scope restriction (e.g.,

encapsulation)

Fault isolation (commentout code until problemvanishes)

Deductive reasoning(play dedective)

Trap setting (setbreakpoint or watchcondition

Model testing (constructscaled-down version ofsystem)

Page 26: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

26

Debug Philosophies that Work for Me

Devise test data before you codeDon’t assume that a condition can’t happenDesk-check your code before you run it and

especially before you start using a debugger If you observe a failure, you have 2 options:

Bad: hack around with debugger until you find bug Good: reread source/desk check again to find bug

(If you can find one bug, then your code probably has more)

Set up a regression test suite to be sure old bugs don’t resurface

Page 27: 1 Producing an OO System from Code Steps: 1. Build executable 2. Test 3. Debug Usually you: yUse multiple source files ySplit interface/implementation

27

Suggestions About DebuggingFind the fault that caused failureA fault may remain undiscovered for long

time. Look for the possibility of a long-dormant fault

Avoid hopeful corrections; don’t “randomly” change code hoping to fix it

Avoid frustrationGet help! Ron and Didem love you and

want you to come to their office hours