CSC 226

CSC 226 logo

Software Design and Implementation


Modular Programming, Scope. and Side effects

Objectives

  • Repair code which utilizes "bad practices"
  • Reflect on best practices in the use of variables and functions

Code Responsibly!!

global variables are badThe scope of an variable refers to where the variable can be used.Local scope refers to variables declared within a function.  Global scope refers to all the variables which can be used everywhere Global variables are often called "dangerous" or  even"evil" because they can cause many unintentional results called side effects. Google "global variables are evil" if you don't believe it!

"Evil"  by Langston Hughes

It looks like what drives me crazy
Don't have no effect on you--
But I'm gonna keep on at it
Till it drives you crazy too.

A function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world.  A function's input/output should be completely defined by its function name, parameters, doc string and return statement. One can think of a function as a black-box with inputs and outputs. Hence, modifying a global variable in a function is considered a poor programming practice. It is considered best practice to send a variable to a function as a parameter or to have it be returned in the return statement.In general, side effects are to be minimized or avoided all together because they can cause many hours of pain in debugging.

Modular programming is a design technique which emphasizes separating a program into independent, interchangeable functions, such that each contains everything necessary to execute only one aspect of the desired functionality. Modular programming serves to minimize side effects by using parameters and return values as intended.


Assignment Specifics

This assignment is to be designed together in pairs in class. One of you will serve as debugging driver and scribe navigator and the other will serve as debugging navigator and scribe driver.

Open p8.broken_code.py which will be used both to guide your answers to a series of questions and which you will repair. Then create a Word document called YourUserName(s)-p8.docx to contain your reflections. When you are finished writing and testing your assignment, you will submit BOTH your source code, YourUserName(s)-p8.py and your Word document YourUserName(s)-p8.docx.

Run the p8.broken_code.py program.

  1. In a paragraph, compare and contrast the functions get_max_number() and find_max_number(), identifying all the ways they are similar and different.
  2. don't repeat yourself"Don't repeat yourself" is such an important principle of software development that it has an acronym, DRY. Given this, explain why both of these functions get_max_number() and find_max_number() are NOT REALLY needed if you follow DRY principles. If you had to choose only one of them, explain which of these functions would be more useful and why.
  3. The unit tests for get_max_number() passed. Fix or write an equivalent unit test which passes for find_max_number(), or explain in your Word document why this is not possible.
  4. Look at rotate_flag() and the unit tests for that function. Tracing the code, explain why ONLY the second unit test failed even though the fourth test is exactly the same as the first two. Then fix this problem in the code by removing the global variable which is causing the problem. In your Word document, explain why this is the preferred fix.
  5. Look at make_older() and the unit tests for that function. Tracing the code, explain why the second and fourth unit tests for make_older() failed. In particular, what is the variable person used for when the function is called in the main part of your program? What relationship does this variable have with person that exists inside the function? When two instances of a variable exist (one outside and one defined inside), the one inside the function is shadowing the other one. Shadowing a variable presents all sorts of problems, and this use here is no exception. Explain in your Word document what this function is supposed to do and how shadowing the variable is preventing the function from working as it should.
    Fix the make_older() function and modify its docstring so that the entire tuple information is returned and the person variable in the main part of the program is updated correctly. In your Word document, explain why this is the preferred fix (as opposed to making a local variable global, for example).
When you are finished writing and testing your assignment, drop BOTH your source code, YourUserName(s)-p8.py and your Word document YourUserName(s)-p8.docx into the p8 dropbox on Moodle.

Copyright © 2014 | http://cs.berea.edu/courses/csc226/