Tuesday, July 16, 2013

Decomposing Functions

When to decompose functions?

  1. Function composition:  A function should be split into a function composition when it performs two or more distinct and complete operations on the consumed data.
    • define arrange-images as a function composition of sort-images and layout-images helper functions.
  2. Operating on a list: When an expression must operate on a list -- and go arbitrarily far into that list -- then it should call a helper function to do that.
    • use the insert-images helper function so that the (first loi) expression can operate on the (rest loi) list, going arbitrarily far into that list as needed.
  3. Domain knowledge shift: When the body of a function must shift to a new knowledge domain, it should call a helper function to do the work in the new domain.
    • use the (larger? img1 img2) helper function when shifting from the domain knowledge of inserting images to domain knowledge of determining size of images.

No comments:

Post a Comment