Why is this an issue?
Procedures with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their position.
Sub SetCoordinates(x1 As Integer, y1 As Integer, z1 As Integer, x2 As Integer, y2 As Integer, z2 As Integer) ' Noncompliant
' ...
End Sub
The solution can be to:
- Split the procedure into smaller ones
' Each function does a part of what the original setCoordinates function was doing, so confusion risks are lower
Sub SetOrigin(x As Integer, y As Integer, z As Integer)
' ...
End Sub
Sub SetSize(width As Integer, height As Integer, depth As Integer)
' ...
End Sub
- Find a better data structure for the parameters that group data in a way that makes sense for the specific application domain
Class Point ' In geometry, Point is a logical structure to group data
Public x As Integer
Public y As Integer
Public z As Integer
End Class
Sub SetCoordinates(p1 As Point, p2 As Point)
' ...
End Sub
This rule raises an issue when a procedure has more parameters than the provided threshold.
Exceptions
The rule does not count the parameters intended for a base class constructor.
With a maximum number of 4 parameters:
Class BaseClass
Sub New(Param1 As Integer)
' ...
End Sub
End Class
Class DerivedClass
Inherits BaseClass
Public Sub New(Param1 As Integer, Param2 As Integer, Param3 As Integer, Param4 As Integer, Param5 As Long)
' Compliant by exception: Param1 is used in the base class constructor, so it does not count in the sum of the parameter list.
MyBase.New(Param1)
' ...
End Sub
End Class
The rule does not count a parameter that a framework, a dependency injection container, or a runtime binding supplies, because it is not part of the argument list a caller writes. Such a parameter is annotated with one of the following attributes, or with an attribute deriving from one of them:
- ASP.NET Core:
Microsoft.AspNetCore.Mvc.FromServicesAttribute,Microsoft.AspNetCore.Mvc.FromStateAttribute - Dependency injection:
Microsoft.Extensions.DependencyInjection.FromKeyedServicesAttribute - Azure Functions input bindings:
Microsoft.Azure.WebJobs.BlobAttribute,Microsoft.Azure.WebJobs.CosmosDBAttribute - Orleans:
Orleans.Runtime.PersistentStateAttribute - Hot Chocolate:
HotChocolate.ParentAttribute,HotChocolate.ServiceAttribute,HotChocolate.SchemaServiceAttribute,HotChocolate.ScopedServiceAttribute,HotChocolate.EventMessageAttribute,HotChocolate.GlobalStateAttribute,HotChocolate.ScopedStateAttribute,HotChocolate.LocalStateAttribute
The remaining parameters are still counted, so an issue is raised when they alone exceed the threshold. With a maximum number of 4 parameters:
Public Class OrderController
Inherits ControllerBase
' Compliant by exception: Service is not counted
Public Function Create(<FromServices> Service As IOrderService, Param1 As Integer, Param2 As Integer, Param3 As Integer, Param4 As Long) As IActionResult
' ...
End Function
' Noncompliant: 5 parameters are supplied by the caller
Public Function Update(<FromServices> Service As IOrderService, Param1 As Integer, Param2 As Integer, Param3 As Integer, Param4 As Long, Param5 As String) As IActionResult
' ...
End Function
End Class
The rule does not raise on the constructor of a type that a dependency injection container or a framework instantiates, because a caller never writes its argument list.
This is the case for a constructor marked with Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute. Only
the marked constructor is exempt, and the attribute is not inherited by derived types.
Still with a maximum number of 4 parameters:
Public Class Service
<ActivatorUtilitiesConstructor>
Public Sub New(Param1 As Integer, Param2 As Integer, Param3 As Integer, Param4 As Integer, Param5 As Long) ' Compliant by exception
' ...
End Sub
End Class
It is also the case for every constructor of a type annotated with Microsoft.AspNetCore.Mvc.ApiControllerAttribute or
Microsoft.AspNetCore.Mvc.ControllerAttribute, and of a type deriving from or implementing one of the following:
- ASP.NET Core:
Microsoft.AspNetCore.Mvc.ControllerBase,Microsoft.AspNetCore.Mvc.Controller,Microsoft.AspNetCore.Mvc.ViewComponent,Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata,Microsoft.AspNetCore.Mvc.RazorPages.PageModel,Microsoft.AspNetCore.Razor.TagHelpers.TagHelper,Microsoft.AspNetCore.Razor.TagHelpers.ITagHelper,Microsoft.AspNetCore.Http.IMiddleware,Microsoft.AspNetCore.Authorization.AuthorizationHandler(Of TRequirement),Microsoft.AspNetCore.Authorization.IAuthorizationHandler - SignalR:
Microsoft.AspNetCore.SignalR.Hub,Microsoft.AspNetCore.SignalR.Hub(Of T),Microsoft.AspNet.SignalR.Hub - Hosting:
Microsoft.Extensions.Hosting.BackgroundService,Microsoft.Extensions.Hosting.IHostedService - ASP.NET:
System.Web.Mvc.Controller,System.Web.Http.ApiController - MediatR:
MediatR.IRequestHandler(Of TRequest),MediatR.IRequestHandler(Of TRequest, TResponse),MediatR.INotificationHandler(Of TNotification),MediatR.IPipelineBehavior(Of TRequest, TResponse) - MassTransit:
MassTransit.IConsumer
Still with a maximum number of 4 parameters:
Public Class HomeController
Inherits Controller
Public Sub New(Param1 As Integer, Param2 As Integer, Param3 As Integer, Param4 As Integer, Param5 As Long) ' Compliant by exception
' ...
End Sub
End Class
Two cases fall outside this exception:
- Attribute types, because their arguments are written at every usage site. MVC filter attributes such as
Microsoft.AspNetCore.Mvc.Filters.ActionFilterAttributeimplementIFilterMetadata, yet the rule still raises on their constructors unless the constructor is marked withActivatorUtilitiesConstructorAttribute. - Types annotated with
Microsoft.AspNetCore.Mvc.NonControllerAttribute, which excludes a type and its derived types from MVC controller discovery.