Bug? Warnings for unused routines

LucaMs

Expert
Licensed User
Longtime User
This time i'm sure :D

If you specify the scope of a routine (I tried a code module routine) and you don't use that routine, you don't get a warning. If you use only "Sub" then you get the message.

B4X:
' No warnings
Public Sub UnusedRoutine
End Sub

' Warning
Sub UnusedRoutine
End Sub
 

Ed Brown

Active Member
Licensed User
Longtime User
That makes sense to me. If it's Public then it can be used/called outside of the class/module. Private, on the other hand, can only be used within the Class/Module and therefore if it's not used you will get a notification about it not being used. By default a sub is 'Private'.
 

LucaMs

Expert
Licensed User
Longtime User
That makes sense to me. If it's Public then it can be used/called outside of the class/module. Private, on the other hand, can only be used within the Class/Module and therefore if it's not used you will get a notification about it not being used. By default a sub is 'Private'.

If you write only:
Sub RoutineName

in a code module, it is Public as default.
 

Ed Brown

Active Member
Licensed User
Longtime User
I stand corrected re: 'By default a sub is 'Private''.

I've just tried it using B4J 3.70 and I'm getting the warning message. Maybe it's specific to B4A only??
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not a bug, it is by design.

1. The purpose of the warnings are to help developers. The compiler tries to avoid from making incorrect warnings.

2. Subs are public by default (mainly because of historic reasons).

3. If you explicitly set the visibility of a sub to Public then it will not warn if it is not used. This happens a lot with reusable classes.

4. If you haven't explicitly set the visibility to public and the sub is unused then in most cases it is a proper warning.
 

stevel05

Expert
Licensed User
Longtime User
If you are creating a Library with many methods, the are not all going to be used at one time, setting them to Public will correctly stop the warning appearing. That makes more sense to me than adding an ignore to each method, or even a global ignore on the page.
 

LucaMs

Expert
Licensed User
Longtime User
Then if I create a library with public routines declared with: "Public Sub" I'll get many warnings (adding this library to a project)?

If so I agree, of course :)

Tried. It is not so.

Library routines:
B4X:
Sub NotSpecified1
    Log("NotSpecified1")
End Sub

Sub NotSpecified2
    Log("NotSpecified2")
End Sub

Public Sub Specified1
    Log("Specified1")
End Sub

Public Sub Specified2
    Log("Specified2")
End Sub

I can avoid using any of those routines without getting warnings.
 

stevel05

Expert
Licensed User
Longtime User
No, it's the other way round. If you just declare Sub Test, you'll get a warning if it's not used. If you declare Public Sub Test, you won't
 

LucaMs

Expert
Licensed User
Longtime User
No, it's the other way round. If you just declare Sub Test, you'll get a warning if it's not used. If you declare Public Sub Test, you won't

If you are talking about code modules, this is why I opened this thread.
If you are talking about libraries, you don't get warnings anyway (in the project that uses that library), with or without "Public" .
 

stevel05

Expert
Licensed User
Longtime User
By Library I meant a Class that can be compiled to a library. So there will be many methods unused. But it's the same for both Code Modules and Classes.
 

LucaMs

Expert
Licensed User
Longtime User
By Library I meant a Class that can be compiled to a library. So there will be many methods unused. But it's the same for both Code Modules and Classes.

No, it is not the same (Steve, learn Italian, please, I could explain better :D)


If you have a module or class in you project, which contains routines declared without specifying the "visibility" (Public), you get warnings.
If you compile that class as Library, then if you don't use some of its routines, you don't get warnings, regardless of the type of declaration.

B4X:
'Class module
Sub Class_Globals

End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub


Sub NotSpecified1
    Log("NotSpecified1")
End Sub

Sub NotSpecified2
    Log("NotSpecified2")
End Sub

Public Sub Specified1
    Log("Specified1")
End Sub

Public Sub Specified2
    Log("Specified2")
End Sub

Using this class directly (not compiled as Library) for each routine declared with "Sub" only you get a warning (I think it would be better to get warnings even for unused routines declared with "Public Sub").
If you compile that Class as Library and you don't use its routines in your project, you don't get warnings at all (and this is the right behaviour).
 

stevel05

Expert
Licensed User
Longtime User
You are correct in your observations, But, if you've compiled the Class to a library and added that compiled library to the project, you shouldn't have the original class in the project as well.

I for one, like the way Erel has currently implemented this.
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is not it better to warn for all unused routines? Eventually, the programmer can add 'ignore.
There is no right or wrong here. It is a matter of balance.

As I see it forcing the developer to add 'ignore comments to all explicitly public subs makes the warning engine less useful and more annoying.

As I wrote in my previous post, the warning engine should help the developer. The developer doesn't need to "fight" with it.
 

LucaMs

Expert
Licensed User
Longtime User
As I wrote in my previous post, the warning engine should help the developer. The developer doesn't need to "fight" with it.
I'm fighting every day :)

the warning engine should help the developer
I almost agree :D


I think that the compiler should always warn; then, if you know that a "warned routine" must still be in your project anyway (for example it is in a shared utility module) you add the ignore.

However, now I know how and when to explicit declare a routine with "Public" ;)


Thank you
 
Top