Curly's Guide To Software
In the movie City Slickers, weathered cowboy Curly told the clueless ranch noobs that he’d found the secret of life. He held up his index finger, and announced “One Thing.”
I suspect that Curly had a mighty good idea, that you should be able to do one thing supremely well instead of doing and being all things. For all our units of software, One is a very friendly number, indeed.
First we look at lines of code. A line of code has ONE statement on it. That doesn’t mean that I can’t write
if x: do yas a single line. That’s one-thing-y enough if the Y and the X are simple.
Secondly, a statement has one effect. I don’t have a single expression that increments counters and assigns values. Software in the old style of C such as
*dest++ = *src++;doesn’t get much play in my world. Avoiding side-effects is pretty widely regarded as a good idea, and I think no less so in individual statements. I will make exception for the forstatement, which is a special case. I always avoid the for loop when an iterator loop such as a for each is available.
Next we move to functions. I am increasingly fond of Meyers’ Query/Command Separation principle, and of course the python philosophy that a function should either do something or tell you something. I find that the focus on clear naming for functions helps me here, because a function whose name begs for an “and” is an ill-conceived function. Whether it’s a static or a member function or whatever, it needs to do ONE thing.
Then we move to classes. We have expounded on the Single Responsibility Principle and class normalization enough that I don’t need to flog that horse here, but you know what I’m talking about. A class that has multiple purposes or knows too much is a class that will cause problems. It is unfocused, and unstable (in that Positional Stability sense). It’s trouble. Better to do one thing very well.
Packages can easily accrue more features than they should have. We have our various principles of package dependency management to ensure that a package doesn’t know too much or do too much. Again, each should have focus. One is important.
Finally, through my life (mostly in Unixen, including Linux) I have learned the value of programs that do only one thing (perhaps with a variety of parameters and tools). I don’t like them to be both a floor wax and a dessert topping. When an application does one thing, and then starts to accrue features that really are different, I generally find great dissatisfaction. I don’t edit html in word or openoffice. I don’t use my word processor to create spreadsheets, and I don’ t do my programming in my email system. That seems more obvious than it really is sometimes, but it is yet another application of One Thing.
