The Ugly Truth - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

The Ugly Truth

Description:

The history of CS is saw-toothed' we keep throwing away the big ugly mess we made ... #include 'sparrow.h' #include 'hawk.h' Draw a graph of dependencies to be sure: ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 12
Provided by: vana
Category:
Tags: sparrow | truth | ugly

less

Transcript and Presenter's Notes

Title: The Ugly Truth


1
The Ugly Truth
  • Computer programming languages, tools make
    tinkering and elaboration too easy!
  • RESULT Simple programs grow endlessly, become
    unreliable, ugly, confusing, impossible to
    understand and improve.EXAMPLE MS Office2000
    1,182 files, 104 folders, 193.65 MB, MS Word
    2.0 40 files, 8 folders, 0.78 MB!
  • The history of CS is saw-toothed we keep
    throwing away the big ugly mess we made and
    starting all over again!

2
Modular Design
  • ...Is our best hope for reliable programs.
    IdeaEven HUGE programs are kept simple
  • Many separate modules
  • Elaborate, but step-by-step
  • Self-contained,independent,
  • Orderly, nested interfaces,
  • Details hidden, protected,
  • One task?one function
  • Separate, tested functions in separate, tested
    libraries,
  • Fix the function bodies dont change the
    interfaces!
  • One huge mess,
  • Complicated, Tangled,
  • Interdependent,
  • Disorganized, with
  • Scattered details everywhere,
  • Redundant,
  • Impossible to test,fixing part A breaks part B
  • A massive heap
  • of quick, partial fixes

3
Modular Design Core Platitudes
  • No problem is inherently complicated and
    tangledit is just poorly understood.
  • Complicated and Tangled too many details
    exposed!
  • Short , Simple, and Separate modules are Best.
    Elaborate and Simple and Separate are good/OK.
    Complicated and Tangled ones are VERY VERY BAD,
    even if the program is short!
  • Hide the Detailskeep interfaces simple
    general.
  • Solve only one problem at a time! Dont confuse
    yourself

4
Modular Design
  • (Chapter 10.1 has good pig-latin example)
  • Break problem into just a few primary steps.
  • Critically examine/refine each step
  • Re-usable parts?
  • Any surprises, any forgotten steps?
  • Any simpler way? Is your refinement confusing?
  • Need to back up, re-organize the larger problem?
  • Repeat apply above to each step you just made.

5
Modular Design
  • typedef struct int score / current score
    /gameInfoT / Structure stub / int
    main() int run gameInfoT game run
    initGame(game) if(TRUErun)
    getInput(game) computeOutput(game)
    displayOutput(game) endGame(it)
  • Write function stubs and data structure
    stubs.
  • FIRST make it run,
  • THEN make it do something.

6
Modules for Big Programs
  • Manage a 500 function program, 10 authors?
  • Answer One module per author
  • A module is a pair of files
  • A .h file (the interface) that holds
  • Function prototypes
  • include statements
  • define statements
  • A .c file (the implementation) that holds
  • Function bodies

7
Modules and Data Structures
  • Good idea
  • Organize programs to give each data structure its
    own private set of functions.
  • Put the structure its fcns in one module
  • Example birdT structure will have functions
    for different behaviorsbirdInit(), BirdHop(),
    BirdFly(), BirdFeed(), BirdPreen(), BirdChirp(),
    BirdSing(), BirdSleep()
  • Module bird.h, bird.c has birdT struct, functions

8
Nested Modules
  • One module can rely on a sub-module
  • module1.h may start with include
    module1A.h include module1B.h
  • module1a.h may start with include
    module1A1.h include module1A2.h and so
    on
  • One structure can have a smaller sub-structure
    as a member type

9
Big Caution though--
  • Though NESTING of modules is allowed, be careful
    that your modules dont have any circular
    dependencies
  • CAUTION! circular includes will not work!

/ my1.h file / include my2.h
/ my2.h file / include my1.h
10
A Good Check
Draw a graph of dependencies to be sure No
upward pointing arrows!
  • .

Myprog.c include objects.h int main()
Objects.h include bird.hinclude
tree.hinclude car.h
Tree.h include oak.hinclude
birch.h include maple.h
Bird.h include owls.hinclude
sparrow.h include hawk.h
car.h include chevy.h include ford.h
11
static Three Meanings?
  • Static local variable
  • Memory stays reserved, always undisturbed, even
    when its variable name is out-of-scope
  • memory usage similar to malloc()
  • Static function
  • Function name is valid (scope) only within the
    module
  • Cant be called/accessed by functions not in
    module
  • Static global variable (GLOBALBAD
    IDEA!!)
  • Name is valid (scope) only within the module
  • Cant be called/accessed by functions not in
    module
Write a Comment
User Comments (0)
About PowerShow.com