Modular Programming Modular Programming 16 n Modular programming

  • Slides: 7
Download presentation
Modular Programming

Modular Programming

Modular Programming (1/6) n Modular programming q q q n n Goes hand-in-hand with

Modular Programming (1/6) n Modular programming q q q n n Goes hand-in-hand with stepwise refinement and incremental development Makes the code easier to develop, test and debug Promotes reusability of codes In general a problem is solved in 3 steps: input computation output. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 2

Modular Programming (2/6) n n n Write a separate module to perform the computation

Modular Programming (2/6) n n n Write a separate module to perform the computation step. If the computation is complex, it should be further split into smaller steps and each step performed by a module. A ‘module’ q q Should be well-defined Should do one task © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 3

Modular Programming (3/6) n A well-defined module q q Has a good name (for

Modular Programming (3/6) n A well-defined module q q Has a good name (for readability and documentation) Has a clear interface (what parameters does it take? ) May pass back to its caller no result or a single result (what value does it return? ) Example: set. Colour(int c) void set. Colour(int c) { int colour = c; } q Takes in integer c. Does not return any value (void). Example: get. Colour() int get. Colour() { return colour; } © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 4 Takes in no parameter. Returns an integer

Modular Programming (4/6) n Advantages of modular programming: q Easy to replace v q

Modular Programming (4/6) n Advantages of modular programming: q Easy to replace v q E. g. : When you discover a better algorithm for is. Prime(int), you just replace that method without affecting any other parts of the program or other programs. Easy to reuse v v E. g. : Suppose you want to write a program to count the number of prime numbers between two integers a and b. Compare how you would need to modify © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 5

Modular Programming (5/6) n Reusability of code q q If is. Prime(int) is a

Modular Programming (5/6) n Reusability of code q q If is. Prime(int) is a very commonly used method, we could even go a step further… v Ie. It is so short and sweet! Any other application that requires the is. Prime(int) method can use the method in a similar fashion. © CS 1101 (AY 2009 -2010 Semester 1) Week 6 - 6

Advantages of Modular Programming • Extension of function modularity: Build, test, debug in isolation

Advantages of Modular Programming • Extension of function modularity: Build, test, debug in isolation from other modules. • Prevent access to private functions and data – prevent misuse. • Hide difficult algorithms behind a set of interfaces. • Hide non-portable code behind a portable interface. Can change internals without changing client code. • Enable teams to work on same program. Each works on a different module. • Modules are often reusable – libraries. 7