| Property | Value |
|---|---|
| Package | Philips.CodeAnalysis.MaintainabilityAnalyzers |
| Diagnostic ID | PH2138 |
| Category | Functional Programming |
| Analyzer | AvoidVoidReturnAnalyzer |
| CodeFix | No |
| Severity | Error |
| Enabled By Default | No |
This analyzer warns about methods or functions that have no return value. Whenever the return value is void it means that the purpose of the method is not directly observable. It either modifies some external state (file, database, etc), or silently modifies an input parameter.
This makes the method hard to test as you will need to mock either the external state or peak at the original input. Here is a dzone article with more details.
Consider returning a value instead and have the external state modified in as few locations as possible.
Return an object, instead of running an action. Consider returning an object you can use in a unit test.
Ultimately we will need to execute changes onto the code's environment. For these (later) situations here is a more thorough explanation of extracting file io operations for testing (without an actual file system).
Code that triggers 2 diagnostics:
class MyClass
{
public void ProduceMeasurement(String measurementField) {}
}And the corrected code:
class MyClass
{
public Measurement ProduceMeasurement(String measurementField) {}
}Overridden methods, i.e., those having the override keyword, are exempt since they are beyond the developers control.
This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.