Names for method should say what they do but not how they do it. This is the fourth in a series of posts on Code Gardening - the practice of making small improvements as you see them while you are working.
Consider this fragment of code - this is where we ended up after the last post.
Code that invokes this method might look like this:
The name of this method is superficially good - but we can easily do better.
Unless this method gains context by living in a
CourseRepository (or something similar), the
name should indicate what’s being found - a
Also, given that the names of the parameters are only visible in the implementation, and not at the call site, it would be useful to declare the kind of search. Note that we don’t want to constrain the implementation, so this should declare the behaviour of the search, not how it happens to be implemented.
Renaming the method to
FindCourseByCode we get implementation :
And now the code that invokes this method might look like this:
Using a method name that is explict about the intention of the method makes it easier to read the calling code.