12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For more insight, try modifying the repetition counts at the top of this script, or seethe newer timeit module, which automates timing of code, and finesses some platform-specificissues (on some platforms, for instance, time.time is preferred overtime.clock). Also, see the profile standard library module for a complete sourcecode profiler tool.Function Design ConceptsWhen you start using functions, you’re faced with choices about how to glue componentstogether—for instance, how to decompose a task into purposeful functions(resulting in cohesion), how your functions should communicate (coupling), and soon. You need to take into account concepts such as cohesion, coupling, and the sizeof the functions—some of this falls into the category of structured analysis anddesign. We introduced some ideas related to function and module coupling in theprior chapter, but here is a review of a few general guidelines for <strong>Python</strong> beginners:• Coupling: use arguments for inputs and return for outputs. Generally, youshould strive to make a function independent of things outside of it. Argumentsand return statements are often the best ways to isolate external dependencies toa small number of well-known places in your code.• Coupling: use global variables only when truly necessary. Global variables (i.e.,names in the enclosing module) are usually a poor way for functions to communicate.They can create dependencies and timing issues that make programsdifficult to debug and change.• Coupling: don’t change mutable arguments unless the caller expects it. Functionscan change parts of passed-in mutable objects, but as with global variables, thisimplies lots of coupling between the caller and callee, which can make a functiontoo specific and brittle.• Cohesion: each function should have a single, unified purpose. When designedwell, each of your functions should do one thing—something you can summarizein a simple declarative sentence. If that sentence is very broad (e.g., “thisfunction implements my whole program”), or contains lots of conjunctions (e.g.,“this function gives employee raises and submits a pizza order”), you might wantto think about splitting it into separate and simpler functions. Otherwise, thereis no way to reuse the code behind the steps mixed together in the function.• Size: each function should be relatively small. This naturally follows from thepreceding goal, but if your functions start spanning multiple pages on your display,it’s probably time to split them. Especially given that <strong>Python</strong> code is soconcise to begin with, a long or deeply nested function is often a symptom ofdesign problems. Keep it simple, and keep it short.Function Design Concepts | 369

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!