How to avoid code duplication in a function?

Classes, like dc42 suggests, are another way. Slightly different but related.