Your solution is quite adequate, because it costs (although it may be too large if only four steps), but whether this is the best approach in this case depends on what the purpose of CheckSomeState is actually in the context of your program
- Is it important that CheckSomeState is called exactly once between each task?
- Perhaps sometimes it also needs to be called in the middle of the task?
So, for example, if CheckSomeState actually checks the cancel flag, then it may need additional verification as part of a long-term task.
Another option available to you is an exception (for example, an OperationCancelledException). This reduces your cyclomatic complexity very low, as you can now simply:
CheckForCancel(); DoTaskOne(); CheckForCancel(); DoTaskTwo();
(The disadvantage is that some believe that this is the use of exceptions for the control flow, which is generally disapproved of).
I should also add that the goal should not be to reduce cyclic complexity, but to have code that is easy to understand and maintain. Cyclomatic complexity is a useful metric, but sometimes it unnecessarily punishes code that is pretty simple.
source share