Create helper methods to capture key concepts, even if they are only one line. Giving these concepts a meaningful name helps people reason about the rest of the code.
Consider this fragment of code, particularly the if test inside the loop:
While it’s easy to make the case that the test is easy to read, there are multiple issues to consider:
Any developer reader will need to reverse engineer intent from the code.
The policy of using
StringComparison.OrdinalIgnoreCasefor case comparison is widely repeated, opening the door for subtle defects due to inconsistencies.
If the policy of case-insensitive code comparison ever changes, every occurrence of this will need to be changed.
To address this, introduce a helper method
HasCode() to do the test:
This localizes the policy of code insensitivity in one place, ensuring consistency across every use and giving one place for a change if ever required.
With this helper method, you can rewrite the original loop like this:
Now the code reveals the intent more clearly - there is no need for a developer reading the code to reverse engineer what it means.