Skip to content

Latest commit

 

History

History
52 lines (37 loc) · 2.25 KB

File metadata and controls

52 lines (37 loc) · 2.25 KB

PH2138: Avoid returning void

Property Value
Package Philips.CodeAnalysis.MaintainabilityAnalyzers
Diagnostic ID PH2138
Category Functional Programming
Analyzer AvoidVoidReturnAnalyzer
CodeFix No
Severity Error
Enabled By Default No

Introduction

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.

How to solve

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).

Example

Code that triggers 2 diagnostics:

        class MyClass
        {
            public void ProduceMeasurement(String measurementField) {}
        }

And the corrected code:

        class MyClass
        {
            public Measurement ProduceMeasurement(String measurementField) {}
        }

Exceptions

Overridden methods, i.e., those having the override keyword, are exempt since they are beyond the developers control.

Configuration

This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.