Transcript Lecture 2

CIT 590
Intro to Programming
Lecture 2
Agenda
• ‘import’
• Abstraction
• Functions
• Testing
• The concept of scope
One big giant python file ….
• Python is full of modules
• import math
• from math import *
• the same logic can be used for your own function
• Example areaFuncs.py and usageOfFuncs.py
Abstraction
• A good program will have layers of abstraction
• More often that not you only care about the input and
output and do not really want or even need to know the
internal workings
• A well written function is like a blackbox converting input
into meaningful output
• math.sqrt() - how is implemented
• Who cares!
Modular programming
• Software design technique that emphasizes separating
the functionality of a program into independent,
interchangeable modules
• each module contains everything necessary to execute
only one aspect of the desired functionality.
• You’ll also hear this referred to as top-down design and
stepwise refinement
functions
• In accordance with the concept of abstraction
• Divide a program up into its ‘functional’ portions
• Testing becomes much easier
• Test each component really well – more on this later …
• Think about a few integration tests
• vowelCounter.py
Information hiding
• David Parnas first introduced the concept of information
•
•
•
•
hiding around 1972.
hiding "difficult design decisions or design decisions which
are likely to change."
Hiding information isolates clients from requiring intimate
knowledge of the design to use a module, and from the
effects of changing those decisions.
Modular programming goes hand in hand with information
hiding
Whoever used your function is just relying on getting the
right output for their input. So we can change the internal
implementation safely
Software reuse
• Do not reinvent the wheel
• The reuse of programming code is a common technique
which attempts to save time and energy by reducing
redundant work.
• Software libaries/modules - good example of code reuse
• In general if you are writing a program and you find
yourself copy pasting code it is probably time to write a
function.
Good program design
• Separate your program into the building blocks
• Write a function for each of the building blocks
• Simple methods of separation
• Separate functions for the calculations
• Separate function for the input
• Separate function for the output
The coke machine example
• Cokemachine1 and cokemachine2.py
• One of the examples modularizes code
More modular programming
• Using romanNumeral.py as an example
• Is there a pattern that repeats itself when we are working
with roman numerals
• If you see a common pattern/repeated usage of the same
code – convert that into a function!
Scoping
• local variables versus global variables
• changing the value of a global variable within a function
using the global keyword
• Once you use the global keyword, you are changing the
global variable as opposed to the variable that is just
scoped to the function
Summary
• Understanding the importance of modular programming
• Functions
• Testing a function and code walkthroughs
• Variable scope