Prevent pitfall with direct calls to Methods with no parameters

Why is there an issue?
Changing a functions signature might not result in problems during compilation while resulting in a crash lateron. As calling a function which doesn’t have parameters doen’t need paranthesis, so paranthesis after that function call might be interpreted as Array Index Operation.
VB Pitfall
So changing a function with signature Function functionname(x As Integer) As String to Function functionname() As String dramatically changes behavior for calls like this: Dim text As String = functionname(5).

What could be its impact?
You not only retrieve an unexpected result, in case of the string beeing to short it will fail with ArrayOutOfBounds.

How to prevent this pitfall?

Always use parenthesis for function calls to functions without parameter.
And add another parenthesis afterwards in case of wanted array usage.

Non-Compliant
Function test() As String
Return "Test"
End Function

...
Dim text As String = test(5)
Dim text2 As String = test

Compliant
Function test() As String
Return "Test"
End Function

...
// Use additional parenthesis
Dim text As String = test()
// In Case of wanted Array Index
Dim text2 As String = test()(5)

Type

In my opinion this is a Code-Smell which could result in serious crashes and problems.

Thanks and best regards

Thiemo Hoffmann

I like this idea. It would not make it to a SonarWay profile, because it doesn’t match general VB style and could cause a lot of noise, but it’s a valuable code quality rule.

The only reason .NET does not require paranthesis, is because of a (horrible) design decision, made when creation Visual Basic, where it was allowed. The idea about VB.NET has always been that you could run VB6 and previous code without any issues. However, skipping paranthesis for parameterless functions/subs is not the preferred way. I would advocate to build it, include a fix implementation, and make it the SonarWay, as it is a bad practice.