Skip to Main Content
Prefactoring
book

Prefactoring

by Ken Pugh
September 2005
Intermediate to advanced content levelIntermediate to advanced
240 pages
6h 28m
English
O'Reilly Media, Inc.
Content preview from Prefactoring

6.5. One Little Job

The framework of Unix pipes and filters is elegant. It is easier to create a program that does one simple job (a filter) than a program that does multiple jobs. For example, the word count program (wc) counts lines, words, and characters in the input file. The directory-listing program (ls) lists the files in a directory, one per line. To count how many files are in a directory, you pipe the output of the ls program into the input of the wc program and count the number of lines (e.g., ls | wc).[*]

[*] Unix commands represent another approach to the naming issue. The names are short. (Some people refer to them as cryptic.) Since these commands are typed over and over, the short names are reasonable in that contact.

Each class should capture one concept. Likewise, each method within a class should do one little job well. The Proxy pattern works in this way. Each proxy class can perform one operation (e.g., encryption or checking for security access violations). Once a proxy method has finished its task, it calls the corresponding method in another proxy object to perform further operations.

Multilevel delegation works in a similar manner. The lowest-level class performs simple operations. Higher-level classes perform more complicated operations, which are based on the lower-level operations. The lowest-level classes get used more often, and the higher-level classes are used less often, since they are designed for specific purposes.

For example, here is a File ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Understanding Unittest.Mock

Understanding Unittest.Mock

Mario Corchero
Java™ Performance

Java™ Performance

Charlie Hunt, Binu John

Publisher Resources

ISBN: 0596008740Supplemental ContentCatalog PageErrata