Separating Intent from Implementation

An implementation is simply one way of many to accomplish an intent. If you have a clear understanding of the boundary between implementation and intent, you have a better mental model with which to write and test your software. Not infrequently, the implementation strongly resembles the intent. For example, an intent of “reward winning players” may implement as “query for winning users, iterate over them, and add a badge to each of their accomplishment lists.” The implementation language closely corresponds to the statement of intent, but it’s not the same thing.

Clearly delineating intent and implementation helps your testing efforts scale with your software. The more you can test the intent without incorporating elements of the implementation, the less you couple your tests to your code. Less coupling means that changes in implementation do not force you to update or rewrite your tests. Fewer changes to tests mean that less of your effort is spent on tests, and it increases the likelihood that the tests remain correct. All of this results in lower costs to verify, maintain, and extend your software, especially over the long term.

You can also separate the intent of your code from the function of your code. The distinction here divides the way the code was meant to work from the way it actually behaves. When you need to test implementation, you should test what the code is supposed to do. Testing that it behaves the way it was written provides a false sense of security if it was not written correctly. A passing test tells you something about the quality of the code and its fitness for purpose. A test that passes when it should not lies about those attributes.

When writing your code, use the features of your languages and frameworks to most clearly express your intent. Declaring a variable final or private in Java, const in C++, my in Perl, or var in JavaScript says something about the intended usage of the variable. In dynamic languages with weak parameter requirements such as Perl and JavaScript, passing hashes [PBP] or objects [JTGP], respectively, that name the parameter values can serve to document the intention more clearly internally.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39