{"id":12,"date":"2008-08-30T12:11:00","date_gmt":"2008-08-30T12:11:00","guid":{"rendered":"http:\/\/www.andypalmer.com\/blog\/?p=12"},"modified":"2009-05-22T13:08:45","modified_gmt":"2009-05-22T13:08:45","slug":"what-should-my-code-tell-me","status":"publish","type":"post","link":"https:\/\/andypalmer.com\/2008\/08\/what-should-my-code-tell-me\/","title":{"rendered":"What should my code tell me?"},"content":{"rendered":"
When I’m reading code, there are a number of things that make my job much easier.
The most important (in my opinion) is good naming, but how can we choose suitable names?
This is the pattern that I am starting to see (and recommend), and I think it is pretty effective.<\/p>\n
If we think of the code as a medium of communication, what questions should the code be able to answer?<\/p>\n
What are we trying to achieve?<\/b> How do we do this?<\/b> Who is doing the work?<\/b> Why are we doing this?<\/b> JavaDoc can also be used to tell your users why<\/i> they might need to pay special attention. If the code is well named, you are unlikely to need JavaDoc for all classes. <\/p>\n <\/p>\n <\/ul>\n This is still a work in progress, but the results that I have seen so far have been encouraging. Please let me know in the comments if you have any suggestions for refinements or improvements.<\/p>\n","protected":false},"excerpt":{"rendered":" When I’m reading code, there are a number of things that make my job much easier.The most important (in my opinion) is good naming, but how can we choose suitable names?This is the pattern that I am starting to see (and recommend), and I think it is pretty effective. What? How? Who? Why? If we […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,19,17],"_links":{"self":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/12"}],"collection":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":1,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/12\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
The method name should be a summary of what we are trying to achieve. For example, public void selectTheCustomerNamed(String customerName) {...}<\/code>
It is (hopefully) clear from the method name what will happen when we call it, and what we need to pass in in order for the code to work.<\/p>\n
The body of the method should be the steps needed to achieve what it is we said we would do in the method name. The steps should be at the same level of abstraction. (See Composed Method<\/a>)
public void selectTheCustomerNamed(String customerName) {
theUser.clicksOn(Application.SEARCH_BUTTON);
theUser.types(customerName,into(SearchPage.CUSTOMER_NAME));
theUser.clicks(SearchPage.BEGIN_SEARCH);
theUser.clicks(ResultsPage.FIRST_RESULT);
}
<\/code><\/p>\n
In the example above, by naming the receiver of the messages after the actor who is likely to perform these steps, it helps focus the mind, and can help with appropriate method naming. For example, we can ask ourselves “What does theUser need to do next?”<\/p>\n
At one time or another, you will probably come across a comment like this:
\/\/ Add one to foo
foo++;
<\/code>
This kind of comment is irrelevant at best, it is telling us what<\/i> the next line is doing. If the methods are well named, or the code is idiomatic, this is unnecessary. One negative consequence may be a kind of “comment blindness” where your mind filters the majority of comments as “noise”.
A better use of comments is to explain why<\/i> we are doing things in a certain way, especially if it seems unusual. Martin Fowler<\/a> briefly mentions this in the “Code Smells” chapter in Refactoring<\/a>.
An example of this type of comment is shown below. This represents a hard-won piece of knowledge, and will hopefully prevent a well-meaning programmer from replacing the code with the getFooByQuery<\/code> variant without checking the caching behaviour.
\/\/ We query the id first before trying to retrieve the object, because if we try to
\/\/ retrieve the object directly (with getFooByQuery), it always bypasses
\/\/ the cache (see @link{...})
Integer fooId = findFooIdByQuery(query);
return getFooById(fooId);
<\/code><\/p>\n
Here are some examples of where you might like to use JavaDoc:<\/p>\n<\/p>\n