Docsity

Assignment 2 - Recurrence Relation - Data Structure and Algorithm | EECS 281, Assignments of Algorithms and Programming

Partial preview of the text

Download Assignment 2 - Recurrence Relation - Data Structure and Algorithm | EECS 281 and more Assignments Algorithms and Programming in PDF only on Docsity!

Discussion slides for week of September 25, 2007

The University of Michigan

Outline^ โ—

Recurrence relations โ—^

Recursive functions โ—^

HW

โ—^

Unit testing โ—^

Refactoring โ—^

Pair Programming โ—

Extreme Programming

Recurrence relations โ€“ closed forms^ โ—

Sometimes recurrence relations can be expressed inclosed form.

S(0) = 0S(n) = n + S(n - 1)S'(n) = n(n + 1) / 2

Recurrence relations โ€“ Fibonacci^ โ—

How would we express the Fibonacci sequence as arecurrence relation? โ— What about the tribonacci sequence?

HW2^ โ—

Questions 1 and 2 deal with recurrence relations andrecursive functions โ— Any questions?

Unit testing^ โ—

Unit testing is a methodology by which you test thesoftware of your program. โ— Tests are

unit

tests because they usually operate on a

small, specific part of the system.

โ—^

They are organized in classes.

โ—^

We can implement an extremely simple version of unittesting with

assert.

Unit testing

PowerRaiser::PowerRaiser(

unsigned

int

base

)

:

base_( base ) { } unsigned

int

PowerRaiser::getBase()

const

{

return base_; } unsigned

int

PowerRaiser::raise(

unsigned

int power ) const {

if

(

0

==

number

)

{

return

1;

} else {

return

base_

raise(

power

1

);

PowerRaiser.cpp }

Unit testing

class

PowerRaiserTest

{

public:

void

runAllTests(); void

testGetBase(); void

testGetPower(); ...

PowerRaiserTest.h };

Unit testing^ โ—

Each time we make a change to the code base, we runall unit tests to make sure that all of the functionality isstill there. โ— If an error occurs, it signals a bug. We can figure outwhere it is with our tests, identify it immediately, andcorrect it. โ— Or, if the bug cannot be resolved, we can

revert

our

code (using SVN or CVS, for example) to the priorstate.

โ—^

Thus, code repositories play a big part in unit testing.

โ—^

Tutorial for CVS:^ โ€“

http://www.cs.hmc.edu/qref/cvs.html

Unit testing^ โ—

This

assert-

based unit testing is all right, but it would

be nice to have a unit testing facility that

didn't break

on the first error that occured.

โ—^

There are lots of unit testing frameworks for C++; forexample CxxTest, CPPUnit, Unit++, Boost TestLibraries, QuickTest...

โ—^

Lots for other programming languages, as well.

โ—^

It's a good practice.

It will save you from a lot of

headaches, lower the cost of software development,and โ€“ if you know and do it โ€“ it will make you a lotmore appealing to potential employers!

Refactoring^ โ—

Refactoring is a practice of making your code less risky. โ— No functionality of your code will change after arefactoring, but it will be a lot cleaner and safer. โ— The idea is to make sure all

unit tests

pass before

refactoring,

then refactor

, then make sure all unit

tests pass

afterwards

. (Otherwise revert the

refactoring).

โ—^

Refactoring is driven by

code smells

.^

If something

smells bad in the code, then we need to refactor.

17

Code duplication โ€“ pre-refactor (

bad)

void

myFunction(

int*

ary,

unsigned

int

arySize

for

unsigned

int

i

0; i

arySize;

++i

ary[

i

]

do

some

stuff

here

for

unsigned

int

i

0; i

arySize;

++i

ary[

i

]

do

some

stuff

here

if

myBool

for

unsigned

int

i

i

arySize

++i

ary[

i

]

}^

else

for

unsigned

int

i

arySize

i <

arySize;

++i

ary[

i

]

Code duplication โ€“ post-refactor 2^ void doIterativeOp(

int*

ary, unsigned

int

startIdx,

unsigned

int

endIdx,

int operand ) {

for

unsigned

int

i

startIdx;

i

endIdx;

++i

ary[

i

]

operand;

} void

myFunction(

int*

ary,

unsigned

int

arySize

doIterativeOp(

ary,

arySize,

...doIterativeOp(

ary, 0,

arySize,

...doIterativeOp(

ary,

arySize

myBool

Pair Programming^ โ—

A discipline in which people program in pairs.

โ€“^

Two sets of eyes on the code instead of one reduces[costly] software errors โ€“ according to one study, byup to 30%!

โ€“^

Good way to transfer knowledge

โ€“^

Also a good way to take the edge off of thesometimes boring task of programming.